Home arrow static arrow Java Programming [Archive] - Integer.toHexString, what about bytes?
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - Integer.toHexString, what about bytes?
This topic has 19 replies on 2 pages.    1 | 2 | Next »

Posts:343
Registered: 7/13/04
Integer.toHexString, what about bytes?  
Jul 19, 2004 11:33 AM



 
i am trying to make this byte value turn into a hex value but it doesn't let me because its not an int, is there some way to do this with bytes?
 

Posts:466
Registered: 6/5/01
Re: Integer.toHexString, what about bytes?  
Jul 19, 2004 11:38 AM (reply 1 of 19)



 
Sure... cast it to an int.
 

Posts:7,258
Registered: 1/31/02
Re: Integer.toHexString, what about bytes?  
Jul 19, 2004 11:39 AM (reply 2 of 19)



 
So you are trying to print out the Hex representation of a given byte? I am assuming when you say "it doesn't let you" what you really mean is that you get a message like "possible loss of precision found : int required: byte". If so, just use an explicit cast - for instance, the following code:
public class WackPoetry {        public static void main(String[] args) {        byte b = (byte)124;        System.out.println(Integer.toHexString(b));            }}
produces the following output:

$ java WackPoetry
4a


Make sense?
 

Posts:3,055
Registered: 18/06/98
Re: Integer.toHexString, what about bytes?  
Jul 19, 2004 11:41 AM (reply 3 of 19)



 
Fr. Murderess,

A byte is only a very small int (range: -128 to +127), so you can use Integer.toHexString().

- but beware -

Probably you suppose that "bytes" go from +0 to +255 (0xFF), so you can use two small tricks here:
a) Convert your byte value to an int using the expression (b & 0xFF). Then your byte values will go from +0 to +255.
b) If the value of the int is from 0 to 15, Integer.toHexString() will return you just a single character. So you will need to add a character '0' to the left. You can simply use some "ifs" and string concatenations, but I'm very lazy. So I "OR" the byte with the value 0x100 - you will have a value that Integer.toHexString will always convert to a three-character string; and I remove the first digit, that is always 1.

So you can use:

byte b = (byte) 0xAB;System.out.println (Integer.toHexString((b & 0xFF) | 0x100).substring(1,3));

 

Posts:343
Registered: 7/13/04
Re: Integer.toHexString, what about bytes?  
Jul 19, 2004 11:41 AM (reply 4 of 19)



 
hehehe, the reason i asked is because i have this code and am not sure if it will do what i want:

for (int i = 0; (stuff[0]) != 0xa6 || stuff[1] != 0xb1; i++ )

will it use the hex value of stuff[0] to make that comparison?
or will it take the decimal value? (which will obviously result in a logical mismatch)
 

Posts:37,103
Registered: 3/30/99
Re: Integer.toHexString, what about bytes?  
Jul 19, 2004 11:46 AM (reply 5 of 19)



 
hehehe, the reason i asked is because i have this code
and am not sure if it will do what i want:

for (int i = 0; (stuff[0]) != 0xa6 || stuff[1] !=
0xb1; i++ )

will it use the hex value of stuff[0] to make that
comparison?
or will it take the decimal value? (which will
obviously result in a logical mismatch)

Assuming stuff is an array of int, there is no "hex value vs. decimal value" issue here. If you set stuff[0] to 0xa6, or to 166 (if I did my math right) you'll get the same results. 0xa6 IS 166.

If you've got the code and you're not sure how it will behave, then just write some test code.

 

Posts:343
Registered: 7/13/04
Re: Integer.toHexString, what about bytes?  
Jul 19, 2004 11:47 AM (reply 6 of 19)



 
stuff is an array of byte (since the .read method only reads bytes)
 

Posts:7,258
Registered: 1/31/02
Re: Integer.toHexString, what about bytes?  
Jul 19, 2004 11:47 AM (reply 7 of 19)



 
What's the difference between the "hex value" of something and the "decimal value" of something? I mean, if it's 10 decimal, 0xa hex, 012 octal, 1010 binary, 31 trianry, 22 base 4, (sorry no time to go look at that chart that's around here somewhere) - it's all the same thing, all equal...
 

Posts:7,258
Registered: 1/31/02
Re: Integer.toHexString, what about bytes?  
Jul 19, 2004 11:49 AM (reply 8 of 19)



 
(woops - make that 201 trinary, sorry for the confusion)
 

Posts:343
Registered: 7/13/04
Re: Integer.toHexString, what about bytes?  
Jul 19, 2004 11:55 AM (reply 9 of 19)



 
oh ok, i get it Lee thnx, i thought that you have to always convert everything into the type of value that you are working with, like in my example how i'm looking for A6B1 in hex i thought i have to convert the values into hex to make that comparison, but now i understand that to the computer its all the same.
 

Posts:37,103
Registered: 3/30/99
Re: Integer.toHexString, what about bytes?  
Jul 19, 2004 11:56 AM (reply 10 of 19)



 
stuff is an array of byte (since the .read method only
reads bytes)

Okay. Same thing applies though. As I think somebody stated in an earlier reply, a byte is just a smaller int, just as a short is, and just as an int is a smaller long.

You need to be worried about size, for instance--e.g. if you try to stuff the number 9999 into a byte it won't work--but that doesn't seem to be an issue.

Sign can be an issue--e.g. is 0xFF -1 or 255? That would matter if you were doing something like
if (x == 255) 
but from the sample code you provided it doesn't seem to be an issue.
 

Posts:27,518
Registered: 11/3/97
Re: Integer.toHexString, what about bytes?  
Jul 19, 2004 12:03 PM (reply 11 of 19)



 
What's the difference between the "hex value" of
something and the "decimal value" of something? I
mean, if it's 10 decimal, 0xa hex, 012 octal, 1010
binary, 31 trianry, 22 base 4, (sorry no time to go
look at that chart that's around here somewhere) -
it's all the same thing, all equal...

I am guessing the 'somthing' part could be relevant....

String s1 = "10";
String s2 = "0a";

If (s1.equals(s2)) ...
 

Posts:3,055
Registered: 18/06/98
Re: Integer.toHexString, what about bytes?  
Jul 19, 2004 12:05 PM (reply 12 of 19)



 
 for (int i = 0; (stuff[0]) != 0xa6 || stuff[1] != 0xb1; i++ )


Your code will not give the expected result, because 0xA6 is an integer, and the byte value in stuff[0] is converted to an integer and then compared. If the byte value in stuff[0] is (byte)0xA6, it is converted to the integer value whose hexadecimal, unsigned representation is 0xFFFFFFA6 due to "sign extension". So the test will always succeed (0xA6 is never equal to 0xFFFFFFA6).

You need to compare apple to apple, not orange to (orange)apple. The apple, converted to an orange, can give strange results. Try:

 for (int i = 0; stuff[0] != (byte)0xa6 || stuff[1] != (byte)0xb1; i++ )

 

Posts:37,103
Registered: 3/30/99
Re: Integer.toHexString, what about bytes?  
Jul 19, 2004 12:09 PM (reply 13 of 19)



 
Good catch, edsonw. I should've engaged my brain before answering.
 

Posts:343
Registered: 7/13/04
Re: Integer.toHexString, what about bytes?  
Jul 19, 2004 12:10 PM (reply 14 of 19)



 
whoa thank you! that was another problem with my code, i always got a value like "FFFFFFA6" i was going CRAZY! i didn't know what the problem was. thank you everyone.
 
This topic has 19 replies on 2 pages.    1 | 2 | Next »