Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / August 2006

Tip: Looking for answers? Try searching our database.

Unsigned byte values

Thread view: 
Brian Bagnall - 30 Jul 2006 06:55 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)

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.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.