Free Scan: Update Your PC's Outdated Drivers to Optimize Performance
September 21st, 2008, 01:09 PM
|
#1 (permalink)
| | Junior Member
Join Date: Sep 2008
Posts: 3
| Java: Problem displaying 0's in binary
Good day sirs.
I am creating a program to convert between decimal, binary and hexadecimal, and it works fine.
The problem is that I need binary outputs to display eight digits.
For example:
Decimal input of 5 yields "101" in binary but I need the output to show "00000101".
If it helps, I've been using Integer.toBinaryString(int), Integer.toHexString(int) to display output values.
Thanks in advance. |
| |
September 21st, 2008, 07:15 PM
|
#2 (permalink)
| | Go back to sleep
Join Date: Jul 2002 Location: Switzerland
Posts: 6,052
|
one way would be checking the length of the binary string and if the size is smaller than (in your example) 8 just add the remaining zeros so you get a string with 8 chars
i could give it a try...
Creatures
__________________
Knowledge Is Power
|
| |
September 21st, 2008, 07:25 PM
|
#3 (permalink)
| | Go back to sleep
Join Date: Jul 2002 Location: Switzerland
Posts: 6,052
|
ah well that was a quick one ;p Code: }
int x = 5;
String str = Integer.toBinaryString(x); //int to bin string
for(int q = 9;q >= str.length();q--) {
str = "0" + str; //adding 0's until string.length() = 8
}
System.out.println(str);
} you can use while, for, do while, whatever you prefer ;p
Creatures |
| |
September 23rd, 2008, 03:29 AM
|
#4 (permalink)
| | Junior Member
Join Date: Sep 2008
Posts: 3
|
There are still problems in the number of 0's being appended.
Here are sample outputs:
4 = 0000100 (7 digits)
8 = 0001000 (7 digits)
16 = 00010000 (8 digits which is ok)
32 = 00100000 (ok again with 8 digits)
64= 001000000 (9 digits)
128 = 010000000 (9 digits)
255 = 011111111 (9 digits)
Last edited by dchips13 : September 23rd, 2008 at 04:25 AM.
|
| |
September 23rd, 2008, 04:47 AM
|
#5 (permalink)
| | Junior Member
Join Date: Sep 2008
Posts: 3
|
Oh I got it to work by changing the for loop a bit:
int x = 5;
String str=Integer.toBinaryString(x);
for(int q=str.length();q<8;q++ ) {
str = "0" + str;
}
System.out.println(str); |
| |
September 28th, 2008, 10:55 AM
|
#6 (permalink)
| | Ultimate Member
Join Date: Mar 2005 Location: Out of my mind
Posts: 2,742
|
Glad you got...another method would have been to set an 8-byte array to all zeroes.
Then loop through storing remainder till number to convert is 0; then display array; display it backwards though (e.g. 128 bit value to 1 bit value). In C++: Code: #include <cstdlib>
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
int array[8] = {0,0,0,0,0,0,0,0};
int i=0;
int y;
cout << "Enter number to convert (0 to 255): " ;
cin >> y;
while(y != 0) {
array[i] = y % 2;
i=i+1;
y=y/2;
}
for(int i=7;i>=0;i--)
cout << array[i];
cout << endl;
system("PAUSE");
return EXIT_SUCCESS;
}
Last edited by Rootstonian : September 28th, 2008 at 11:29 AM.
|
| | |
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | | |
Posting Rules
| You may post new threads You may post replies You may not post attachments You may not edit your posts HTML code is Off | | | | Most Active Discussions | | | | | Recent Discussions  | | | | | |