File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Integer.toBinaryString Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Integer.toBinaryString" Watch "Integer.toBinaryString" New topic
Author

Integer.toBinaryString

fred fredricks
Greenhorn

Joined: Mar 04, 2003
Posts: 16
hi ppl..
i have some code which reads a txt file, and then converts to binary. my problem is that i need it all to be 12bit. but not all of them come out like that.
how can i fill the "result" with 0's (zeros), from left to right till its 12??? i had a go at masking...but i am having trouble understanding that. thanx!!!
thankx ppl....
here is a bit of my code, i dont know if it will help understand;
for (int i = 0; i < data.length; i++)
{
instruction[i] = Integer.valueOf(data[i]).intValue();
}


//pc = addresses[0];
for (int j = 0; j < data.length; j++)
{
binary[j]= Integer.toBinaryString(instruction[j]);
System.out.println(binary[j]);
Peter den Haan
author
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
12 bits? One possible way is to make sure there is always a 13th bit:
private static final int THIRTEENTH_BIT = 1 << 12;
... instruction[j]|THIRTEENTH_BIT; ...
Then you know the bitstring starts with your 13th bit, which you can simply skip using substring():
binary[j] = Integer.toBinaryString(instruction[j]|THIRTEENTH_BIT).substring(1);
Is this the kind of thing you had in mind?
- Peter
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Integer.toBinaryString
 
Similar Threads
int to byte[]
Can I pass an array of ints to a class?
how to execute the j = ~i?
Output from a class shown in GUI?
Binary in java - guide me