I am in trouble.
I need to convert a signed 16-bit Hexadecimal no to short.
Say I have a string s = "f2f7"
Which is a negative number and lies in the range -32768 to 32767 so is the range of type short.
But when I do

short num ;
num = Short.parseShort(s, 16) ;

it throws an exception complaining about out of range value for short.
Actually it treats it as Integer and convert f2f7 to 62199 and then tries to assign it to num which is short.
I have views the source of parseShort and it calls Integer.parseInteger(s, i) inside it.

How can I get actual signed value of "f1f7" and assign it to my short (num) ??
it works fine for values in the range "0000" to "7fff".

Dani AI

Generated

the problem is that Short.parseShort(hex, 16) expects a signed number in text (like -3337), not a two's-complement bit pattern. So "f2f7" is parsed as the positive integer 62199, which is out of range for short, and you get an exception. Your intuition and 's math are right: 0xF2F7 represents -3337 as a 16-bit two's-complement value. , that value does fit in a short; it just needs to be interpreted correctly.

Use an int to parse the hex, mask to 16 bits, then narrow to short so Java reinterprets the low 16 bits with the proper sign:

static short hex16ToShort(String s) {
    String h = (s.startsWith("0x") || s.startsWith("0X")) ? s.substring(2) : s;
    if (h.length() == 0 || h.length() > 4) throw new IllegalArgumentException("1-4 hex digits");
    int bits = Integer.parseInt(h, 16) & 0xFFFF;  // treat as unsigned 16-bit
    return (short) bits;                           // reinterpret as signed short
}

// examples:
short a = hex16ToShort("f2f7"); // -3337
short b = hex16ToShort("8000"); // -32768
short c = hex16ToShort("7fff"); //  32767
short d = hex16ToShort("ffff"); //     -1

If your VM encodes 16-bit offsets in little-endian order (common in instruction streams), swap the bytes after parsing:

short be = hex16ToShort("f2f7");        // big-endian interpretation
short le = Short.reverseBytes(be);      // little-endian interpretation

Implementation tip for your 64 KB address space: keep addresses in an int and mask with 0xFFFF during arithmetic, only casting to short when you specifically need a signed value. That avoids surprises when adding offsets that cross 0x7FFF/0x8000. 's quick check with shortValue() shows the same idea in practice; the mask-and-cast approach above is safe and easy to reuse.

Recommended Answers

All 8 Replies

You can't your trying to represent a number that is out of a short's range.

Highest you can go is :

short s = 0x7fff; //2^15-1

A bit of game (and there are better ways, to be sure) but, parse it as an int (I know it's the wrong value, but bear with me), get the binary value of that int, lop of the left most bit (the string will only contain enough bits to represent the value) and then parse that as a short with a leading minus sign.

;-)

What would f2f7 be, anyway? Is the smallest negative short in hexadecimal 8000 (corresponds to -2^15) and the largest negative short in hexadecimal ffff (corresponds to -1), so f2f7 is equal to:

f2f7
0d08 (flip bits)

Add 1
0d09

Convert to decimal and add a minus sign

-3337 in decimal?

Is that the right answer? Or does 8000 equal -1 and ffff = -2^15? My negative hexadecimal representation is pretty rusty.

Yes, 8000 is -MAX_VALUE and ffff is -1.

The conversion to decimal is not be necessary (and I screwed up on the "binary" possibility, as you need to flip the bits there, too ;-) ).

System.out.println(Integer.valueOf("-0d09", 16));

Thanks alot.. Got the solution..
I am actually implementing a Virtual Machine Instruction set which read memory offset in 16 bit HEX, then I need to translate it to some value and add offset into it.. My memory is 64KB. and as it is 16 bit thus have to use Short in order to address it.
ANyhow thanks alot.. it is solved now.

Would Please tell, is there any algorithm for encrypting and decrypting the media file such as audio and video files. thanks in advance.

Would Please tell, is there any algorithm for encrypting and decrypting the media file such as audio and video files. thanks in advance.

Start your own thread, and google is a great resource.

System.out.println(Integer.valueOf("FFFF", 16).shortValue());
System.out.println(Integer.valueOf("8000", 16).shortValue());
System.out.println(Integer.valueOf("7FFF", 16).shortValue());
System.out.println(Short.reverseBytes((Integer.valueOf("7FFF", 16)).shortValue()));

-1
-32768
32767
-129

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.