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.

Recommended Answers

All 5 Replies

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

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

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.

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

Yup. You can print the hex value in a byte with a hex format spec, eg
System.out.printf("%02X", myByteVariable);

Be a part of the DaniWeb community

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