473,398 Members | 2,212 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Unsigned byte values

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
Jul 30 '06 #1
3 55563

"Brian Bagnall" <bb******@mts.netwrote in message
news:oq***********@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
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;
What are you using to communicate between Java and C++? Sockets? files?
named pipes? JNI?

- Oliver

Jul 31 '06 #2
"Brian Bagnall" <bb******@mts.netwrote 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
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;
<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!

--
Ian Shef 805/F6 * These are my personal opinions
Raytheon Company * and not those of my employer.
PO Box 11337 *
Tucson, AZ 85734-1337 *
Jul 31 '06 #3
Brian Bagnall wrote:
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.
Aug 13 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Philipp | last post by:
Hello, I would like to print (cout) the binary representation of a double. ie the values of the bytes 1-8. A typical output would be something like (in hex) ff af 12 d3 ab 9f 3c 00 Is there a...
14
by: Magix | last post by:
I received byte by byte 0x8E byte1 0x3D byte2 0x64 byte3 0x5F byte4 How can I construct to be LONG 0x645F8E3D (shift left >> 16) Thanks.
53
by: Neo | last post by:
Hi All, Is that true that size of a byte not necessarily 8-bit? What the std. says? If that true, then what will the size of an int, i mean what sizeof(int) should return? On my machine...
17
by: cpptutor2000 | last post by:
Could some C guru help me please? A byte is an unsigned char in C. How do I convert from a C string to a corresponding byte array. Any help would be greatly appreciated.
1
by: PrettySneaky | last post by:
Like the subject said. I have an byte array and want to save them in a text file with UTF8 encoding. However when an element in the byte array is above 127 the GetString method ignores the value...
33
by: Benjamin M. Stocks | last post by:
Hello all, I've heard differing opinions on this and would like a definitive answer on this once and for all. If I have an array of 4 1-byte values where index 0 is the least signficant byte of a...
0
by: Kevin | last post by:
I have been reading the libxml2 WWW site, but I am looking for the best recomendations on how to create new nodes that have double and unsigned int values. I have found the function...
4
by: Lamefif | last post by:
how can the computer tell the difference between the two? i mean a byte is 8 bit can be 1 or 0 11111111 = 255 unsigned byte 10000000 = -128 or 128 ?
5
by: phiber4591 | last post by:
Hi there, I'm trying to "convert" an unsigned int32 to an array of bytes (with a length of 4, of course). Everything i tried up to now didn't work out, so I would really appreciate any help or tip...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.