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".

## Recommended Answers

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 …

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 …``````

## All 7 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.

Be a part of the DaniWeb community

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