aspose file tools*
The moose likes Beginning Java and the fly likes Expressing unsigned integers and longs Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Expressing unsigned integers and longs" Watch "Expressing unsigned integers and longs" New topic
Author

Expressing unsigned integers and longs

Roger F. Gay
Ranch Hand

Joined: Feb 16, 2007
Posts: 349
I'm not posting any code (except the first one) because nothing I've tried works. I keep googling and reading and have the feeling there's a simple answer, but never find one.

I have to produce message headers that tell the recipient, among other things, the number of bytes in the message being sent. The format of this information is specific. I first need to find the length and it can be very long, so the length is first calculated and stored in a long. Now here's how I have to provide the information.

For lengths less than 126 bytes, send a single byte. (I've actually done this successfully, starting with the long byteLength by conversion: Byte.parseByte(Long.toString(byteLength))).

If the length is between 126 and integer max, it must be expressed in 2 bytes interpreted as a 16 bit unsigned integer.

If it's bigger, it must be expressed in 8 bytes interpreted as a 64-bit unsigned integer (the most significant bit MUST be 0).

Multibyte length quantities are expressed in network byte order.


Correlation does not prove causality.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 13875
    
  10

Java does not have unsigned integer types like C and C++ have, but it's unlikely that that's going to be a real problem to solve this.

The specifications that you quoted are a little strange however, because I think they are ambiguous. Suppose the receiver gets a message that starts with a few bytes with the value: 0x04 0x28 ... With the specifications you gave, it is impossible for the receiver to know if only the first byte is the length (0x04 = 4 bytes) or if the first two bytes are the length (0x0428 = 1064 bytes).

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36501
    
  16
. . . and you appear to have two different representations for 125

I presume between 125 and means between 126 and.
Roger F. Gay
Ranch Hand

Joined: Feb 16, 2007
Posts: 349
OK, I corrected the 125/126 thing. Sorry about that. It's not a problem for the receiver to figure out whether the length is given as one byte, 16 bits unsigned, or 64 bits unsigned. They read the first byte. If its value is less than 126, then they're done. That byte represents the data length. If it's 126, they need the next 2 bytes interpreted as an unsigned integer. If it's 127, they need the next 8 bytes, interpreted as a 64 bit unsigned long. (network byte order)

The problem I'm posting about is getting the data into the form required. I'm thinking the last step on each is something.getBytes(). How do I get to "something"?
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 13875
    
  10

For 16-bit (two byte) lengths you can just use an int to store the length internally. To get the two length bytes, you just get bits 15-8 and bits 7-0 of the int using bitwise operators. For example:


For 64-bit lengths:
Roger F. Gay wrote:If it's bigger, it must be expressed in 8 bytes interpreted as a 64-bit unsigned integer (the most significant bit MUST be 0).

If the most significant bit must be 0, then it will fit in a Java long.

You can get the 8 bytes of a long with bitwise operators, similar to the example above.
Roger F. Gay
Ranch Hand

Joined: Feb 16, 2007
Posts: 349
Jesper de Jong wrote:For 16-bit (two byte) lengths you can just use an int to store the length internally. To get the two length bytes, you just get bits 15-8 and bits 7-0 of the int using bitwise operators. For example:


For 64-bit lengths:
Roger F. Gay wrote:If it's bigger, it must be expressed in 8 bytes interpreted as a 64-bit unsigned integer (the most significant bit MUST be 0).

If the most significant bit must be 0, then it will fit in a Java long.

You can get the 8 bytes of a long with bitwise operators, similar to the example above.


Looks good. Thanks. Just the kind of answer I was hoping for. I've been googling for a week, and true to my luck, think I just now found something finally, now that I've posted the question. It generally explains and I was hoping to get that complemented with at least an example specific to my question. Perfect!
http://darksleep.com/player/JavaAndUnsignedTypes.html
Roger F. Gay
Ranch Hand

Joined: Feb 16, 2007
Posts: 349
Solved;

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Expressing unsigned integers and longs
 
Similar Threads
How to Pack/Unpack message?
Help required to read binary file
does char data type allows signed values
DataOutputStream / WAVE steganography
Java endian and bit order and signed and unsigned and python and more confusion than I can deal with