Convert json decimal to short with hex value

I have JSONObjectone that contains a string with a decimal value like this:

private static RegisterIn ParseRegisterIn(JSONObject object)
{
    RegisterIn toReturn = new RegisterIn();
    try {
        toReturn.setUsername(object.getString("username"));
        toReturn.setCertificate(new Short(Integer.toHexString(object.get("certificate"))));
    } catch (JSONException e) {
        e.printStackTrace();
    }
    return toReturn;
}

The value of the object is certificate 15879 , which corresponds to 3E07 in hexadecimal format. I want to restore it from a JSONObject and save it in an attribute short. And that should be so.

I tried to access the parameter and restore it as above, but I get the following exception:

java.lang.NumberFormatException: invalid int: "3e07"

How can I get this decimal value, convert it to hexadecimal and keep it short?

NOTE:

toReturn.setCertificate(...)

- short.

+4
source share
2

Short.parseShort(object.getString("certificate"), 16);
+1

Short.parseShort(object.getString("certificate"), 16);

+1

Source: https://habr.com/ru/post/1599361/


All Articles