I'm writing some code that passes values from a Java program to a C++
program using an output stream. The C++ program requires a UBYTE, which is
an unsigned byte (values 0 to 255).
Java doesn't have unsigned bytes - only signed bytes (-127 to +127)
So if I want to pass a byte like 128 (0x80) to the C program, how do I do
it?
Java objects to the following code:
byte val = 0x80;
And this cuts off part of the number I believe:
byte val = (byte)0x80;
- Brian
Oliver Wong - 31 Jul 2006 16:18 GMT
> I'm writing some code that passes values from a Java program to a C++
> program using an output stream. The C++ program requires a UBYTE, which is
[quoted text clipped - 12 lines]
>
> byte val = (byte)0x80;
What are you using to communicate between Java and C++? Sockets? files?
named pipes? JNI?
- Oliver
Ian Shef - 31 Jul 2006 20:51 GMT
"Brian Bagnall" <bbagnall@mts.net> wrote in news:oqZyg.6$RK5.2
@newsfe21.lga:
> I'm writing some code that passes values from a Java program to a C++
> program using an output stream. The C++ program requires a UBYTE, which is
[quoted text clipped - 12 lines]
>
> byte val = (byte)0x80;
<snip>
I suggest that you take a look at
http://www.mindprod.com/jgloss/unsigned.html
It may give you some ideas.
Remember that 0x80 really represents a 32 bit integer (int):
0x00000080
You were on the right track with your second example.
byte val = (byte)0x80;
System.out.println("val = " + val);
System.out.println("val = " + Integer.toHexString(0x000000ff & val) +
" hex");
prints:
val = -128
val = 80 hex
which is exactly what you would expect from the Java interpretation of an
unsigned byte (if Java had such a thing) containing 128.
Thus, you can keep your values of 0 to 255 in an int. Convert to a byte
(as in: byte val = (byte)int_containing_a_positive_value; )
just before you pass the byte to your output stream.
I don't know what led you you to believe that this was cutting off part of
your number.
Best wishes!

Signature
Ian Shef 805/F6 * These are my personal opinions
Raytheon Company * and not those of my employer.
PO Box 11337 *
Tucson, AZ 85734-1337 *
Soren Kuula - 13 Aug 2006 23:17 GMT
> I'm writing some code that passes values from a Java program to a C++
> program using an output stream. The C++ program requires a UBYTE, which is
> an unsigned byte (values 0 to 255).
>
> Java doesn't have unsigned bytes - only signed bytes (-127 to +127)
Right.
> So if I want to pass a byte like 128 (0x80) to the C program, how do I do
> it?
byte intToPseudoUnsignedByte(int n) {
if (n < 128) return n;
return n - 256;
}
> Java objects to the following code:
>
> byte val = 0x80;
byte val = intToPseudoUnsignedByte(128);
What happens is that Java uses 2's complement signed values. A negative
number n is represented as:
~(-n) + 1 (Reverse the sign back to positive, flip all bits, add 1)
(examples:)
So, -1 is in 2's complement is:
~(-(-1)1) + 1 = ~(00000001b) + 1 = 11111110b + 1 = 11111111b. That is
the bit pattern meaning 255 in unsigned. Try to stuff 255 into my
intToPseudoUnsignedByte function, and you get -1 --- which, again, has
the binary representation that you want for 255.
intToPseudoUnsignedByte(128) is -128. In 2's complement, that is
~(-(-128)) + 1 = 01111111b + 1 = 1000000, which is the bit pattern
meaning 128 in unsigned.
Hop this helped.
Soren.