0

how do I put hexadecimal values into a byte array??
I did the following

private byte[] buffer=new byte[] {
            (byte)0xC8, 00, 0x09, 0x4A, 32, 0x4D, 45, 76, 33, 0x2E, 30, 32, 00, 05, 00, 00
    };

but when i print them using System.out.print(...), the output is

-56 0 9 74 32 77 45 76 33 46 32 0 5 0 0 

we can see that C8==200 in decimal but it prints -56. Could anyone suggest me where I am going wrong?
Thank you.

2
Contributors
5
Replies
11
Views
3 Years
Discussion Span
Last Post by JamesCherrill
Featured Replies
  • chars are unsigned 16 bit numbers, so values 0-255 are no problem. Use them just like any other integer type. But is that really what you need? The fact that you create your byte[] with hex values seems to suggest that the numeric equivalent isn't what it's about. When you … Read More

0

Java byte variables are signed, so they are in the range -128 to +127, hence -56 rather than +200

Edited by JamesCherrill

0

thanks for the reply..how can i store values up to 255 then?? in c, i could just use char[] array for that..but i'm confused how we do that in java

1

chars are unsigned 16 bit numbers, so values 0-255 are no problem. Use them just like any other integer type.
But is that really what you need? The fact that you create your byte[] with hex values seems to suggest that the numeric equivalent isn't what it's about. When you put 0xC8 into a byte, it really does hold the value 0xC8.
Maybe giving a bit more background will held you get the most appropriate answer.

0

hey thanks..i was looking for this answer..internally the byte array still holds the 0xc8 value
it's only when printing out on the console that it shows a different value

This question has already been answered. Start a new discussion instead.
Be sure to adhere to our posting rules.