1 2 3 Previous Next 40 Replies Latest reply on Apr 20, 2007 9:46 PM by 807606

# Converting Integer to an 8 bit representation

Im looking to this in my program.

But not sure how to go about it.

Is there any specific function that i can call.

Is there any thread i could have a look at?
• ###### 1. Re: Converting Integer to an 8 bit representation
if you're integer is in the range of 0 to 255, then you could just cast into a char or byte to get it down to 8 bits.
• ###### 2. Re: Converting Integer to an 8 bit representation
What do you mean by 8 bit representation? The binary representation perhaps? Or the four bytes of a 32 bit int primitive?
• ###### 3. Re: Converting Integer to an 8 bit representation
Hi..

The integer range will be greater than 255.
• ###### 4. Re: Converting Integer to an 8 bit representation
if your range is greater than 255 then you can't uniquely store it as an 8 bit representation.
• ###### 5. Re: Converting Integer to an 8 bit representation
Yes the binary representation?

Does 1 byte contain 8 bits?

I need to outbut a byte to represent a character of value 256.

this 256 value would represent 2 characters.
• ###### 6. Re: Converting Integer to an 8 bit representation
What else could i do?
• ###### 7. Re: Converting Integer to an 8 bit representation
Hi..

The integer range will be greater than 255.
This perhaps?
``````int i = 12648430;
System.out.println(Integer.toString(i, 16));``````
• ###### 8. Re: Converting Integer to an 8 bit representation
can you give an example of what you mean?
• ###### 9. Re: Converting Integer to an 8 bit representation
Thanks for quick responses.

Would this work with a DataOutputStream?
• ###### 10. Re: Converting Integer to an 8 bit representation
Perhaps this:
``````    boolean[] matrix = new boolean[8];
int number = 123456;
for ( int i = 0; i < 8 ; i++ )
matrix[i] = ((int)(number/Math.pow(2, i)) == 0) ? false : true;``````
• ###### 11. Re: Converting Integer to an 8 bit representation
I understand that the character range is 0-255
I want to create new charaters like "ab" which i would give a value of 256.
I want to output the value 256 to a .dat file using DataOutputStream.

However the character that is outputted to the .dat file has a value of 1 instead of 256. So i think the 256 value is being rounded
• ###### 12. Re: Converting Integer to an 8 bit representation
Perhaps this:
``````    boolean[] matrix = new boolean[8];
int number = 123456;
for ( int i = 0; i < 8 ; i++ )
matrix[i] = ((int)(number/Math.pow(2, i)) == 0)
) ? false : true;``````
Would this work with a DataOutputStream?
I need to output the new value to .dat file
• ###### 13. Re: Converting Integer to an 8 bit representation
I understand that the character range is 0-255
Er, char is 0-64K
• ###### 14. Re: Converting Integer to an 8 bit representation
Thanks for quick responses.

Would this work with a DataOutputStream?
No, it is only for displaying purpose.
Why don't you just write the char or int primitive directly using the writeInt(...) or writeChar(...) methods of the DataOutputStream class?
1 2 3 Previous Next