• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

byteValue() method of Integer Class

 
Isuru Sampath
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for reading my post.

The description provided in the Java doc for the method is:
Returns the value of this Integer as a byte.


It is obvious that one byte can only represent upto a value of 255. If the value contained is larger than 255, there would be a loss of data.

My question is why the Java API developers did not provide something like a byte array rather than a byte as the return type. Am I missing someting here?
[ October 31, 2006: Message edited by: Isuru Sampath ]
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java byte varies from -128 to 127, not 0 to 255.

All the wrapper classes for numeric primitives implement Number interface, which allows conversions between the types. Where perfect conversion is not possible, some data is lost, as you say.

You can get each byte of a short, int or long, using bitwise operations (right-shift then AND).

You can get the bits of a double using Double.toLongBits(), which gives you a long. If you need the individual bytes, use the above technique to get them.

Similar thing applies for float and Float.toIntBits().
 
Isuru Sampath
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Peter. U clarified a lots of doubts I had.

Meanwhile, I found this code snippet on sun java forum which can easily convert an Int to a byte arry. Re-posting it for the advantage of others. Credit goes to the original writer of the code.



Nifty ha?
 
Bauke Scholtz
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nifty .. ?



That's all you need.

EDIT:

This was the way to convert Integer to byte[]. If you want to convert int (the primitive) to byte[], then just put the int in an Integer:

[ October 31, 2006: Message edited by: Bauke Scholtz ]
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15480
43
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But Bauke, your code does something entirely different from what Isuru's code does.

Bauke's code converts the int to a string of characters (text), and then converts the string in to an array of bytes that represent the digits in whatever character encoding is the default on the platform you run it on. Suppose the encoding is ASCII or UTF-8, an int with the value 342876 would be converted to a byte array of 6 bytes: 51, 52, 50, 56, 55, 54

What Isuru means is that he wants the 32 bits of the int converted into 4 bytes: 0, 5, 59, 92 (in big-endian order).

Isuru: OK, the code with the ByteArrayOutputStream works but it seems a bit overkill to me. You could just do some bit manipulations:

That's faster, doesn't involve creating ByteArrayOutputStream and DataOutputStream objects, and also avoids the need for users of the method to deal with the IOException.

Nifty ha?
[ October 31, 2006: Message edited by: Jesper Young ]
 
Bauke Scholtz
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jesper Young:
What Isuru means is that he wants the 32 bits of the int converted into 4 bytes: 0, 5, 59, 92 (in big-endian order).
Where can I read this?
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15480
43
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Bauke Scholtz:
Where can I read this?

That's what the code he posted above with the ByteArrayOutputStream and DataOutputStream does.
 
Isuru Sampath
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Very many thanks to Jesper and Bauke.

Yes Jesper, what I wanted was the binary representation of the int in Big Endean order. And the code snippet you provided is really nifty and guess it is really fast since bit shifting is the fastest operations we can get (my belief :roll: ).

But the reason I like the previous code is that it does not have to have the byte array length hard coded. It will create a byte array to hold the input integer whatever it's size. I 100% agree with you that it is kind of overkill if the integer size is limited or known. On the other hand if the integer size is dynamic, the previous code snippet is ideal. Guess the choice should be based on the requirement.

All this... pretty nifty and interesting stuff.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15480
43
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In Java, an int is always 32 bits, doesn't matter what system you're running it on. Even if you're running it on a 16-bit or 64-bit processor or operating system, and int is 32 bits (unlike C/C++, where the size of an int is system-dependent).

So writing code that's independent of the size of an int is irrelevant in Java...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic