Sonhja
Sonhja

Reputation: 8458

Convert json decimal number to short with hexadecimal value

I have a JSONObject 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 certificate object value is 15879 which corresponds to 3E07 in hexadecimal. I want to recover it from the JSONObject and save it in a short attribute. And IT HAS to be like that.

I've tried to access the parameter and recover it as posted 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 save it in a short value?

NOTE: the

toReturn.setCertificate(...)

is short type.

Upvotes: 4

Views: 1264

Answers (2)

Machado
Machado

Reputation: 14499

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

Upvotes: 1

Codebender
Codebender

Reputation: 14438

You need to use,

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

Upvotes: 1

Related Questions