I write the bitstream for a JPEG Encoder, I’m facing this problem:
I built Pairs for the AC Cosinus Coefficients (after Quantization) in an int[]array. Each Pair: (Number of Zeros/ Category of the Coefficient after JPEG standard). Out of these two integers, I have to form one hexadecimal number, where as one integer of the pair representing one nibble. The hexadecimal Numbers have to be huffman-encoded later.
Example: My Pair is (4,5), i need it as (0x45).
How can I do that? My way is to convert the Integers to a String, append the Strings, and get them back as one Integer. However, the compiler says
Exception in thread “main” java.lang.NumberFormatException: For input string: “0fffffff5”
//Get a Pair (Numbers of Zeros, Coefficient)
int[]singlePair_ref = acPairs_ref.get(i);
//Convert to Wrapper Class Integer
Integer firstNibble_ref = ((Integer)singlePair_ref[0]);
Integer secondNibble_ref = ((Integer)singlePair_ref[1]);
//Convert to String
String firstNibbleString_ref = firstNibble_ref.toHexString(singlePair_ref[0]);
String secondNibbleString_ref = secondNibble_ref.toHexString(singlePair_ref[1]);
//Append Strings
String byteValueString_ref = firstNibbleString_ref+secondNibbleString_ref;
Integer byteValue_ref = 0;
//Convert the new formed "one" (formed out of two) Number back to Integer
byteValue_ref.parseInt(byteValueString_ref); //Line throws Exception
Hope you can help me. Thanks a lot!
Daniel
Edit:
if I do it like this
YACforHuffman_ref[i] = (singlePair_ref[0] << 4) | singlePair_ref[1];
the out put is decimal (3,6) becomes 54. If I convert the result of this equation with
String YACforHuffman_ref = Integer. Integer.toHexString((singlePair_ref[0] << 4) | singlePair_ref[1];
like Mike did above, I get trouble for a value bigger than 9 (Value A-F), now I know what you meant @Mike, sorry I didnt get it at first. How can I solve this?
I’m unsure how you would pass in values over 9 into your integer pair but assuming you can pass in 0-15 then you could potentially do something such as this: