I'm working with hex values and I need to retain the sign of the values. I'm wondering if someone has a creative way to retain the sign of the int without using BigInteger
? I can't use BigInteger
because it's not in my library due to size limitations; my program runs in an embedded environment.
I have a string of hex values as follows:
55844d000000000080027d801cf6380006f416c0c23d3e4000000000
String ssumI = null;
String ssumQ = null;
String sazI = null;
float isumI = 0;
float isumQ = 0;
float iazI = 0;
ssumI = data.substring(0,8);
isumI = (float)(Integer.parseInt(ssumI, 16)/Math.pow(2,bits));
//55844d00 parses fine
ssumQ = data.substring(8,16);
isumQ = (float)(Integer.parseInt(ssumQ, 16)/Math.pow(2,bits));
//00000000 parses fine
sazI = data.substring(16, 24);
iazI = (float)(Integer.parseInt(sazI, 16)/Math.pow(2,bits));
//80027d80 fails - NumberFormatException
//should be -1,036,173,760 and in the int range
Am I not understanding the failure correctly? It seems like the failure is due to the sign bit being set.
If you want to store 32 bits at a time, the obvious solution in my view would be to parse with Long.parseLong
instead:
isumI = (float)(Long.parseLong(ssumI, 16) / Math.pow(2, bits));
(Do you definitely want to hold these values as float
though? You're losing information that way...)
See more on this question at Stackoverflow