home hardware prices news articles forums photos user reviews
Go Back   Tech Support Forums - TechIMO.com > PC Hardware and Tech > Webmastering and Programming
Join TechIMO for Free!
Register Blogs FAQ Members List Calendar Search Today's Posts Mark Forums Read
Reply Get bargains at  »  Dealighted.com
 
Thread Tools
Currently Active Users: 2685
Discussions: 186,606, Posts: 2,227,046, Members: 230,241
Free Scan: Update Your PC's Outdated Drivers to Optimize Performance
Old September 21st, 2008, 01:09 PM   Digg it!   #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.

dchips13 is offline   Reply With Quote
TechIMO.com Ads - Login or register for less ads.
How many errors does your computer have?

You no longer need to guess! This free stability scan and registry cleaner download will give you a complete diagnosis of your Windows registry, identifying errors and conflicts.

FREE instant scan


Guest, Register Free! to remove this ad and get your tech support questions answered in minutes!
Old September 21st, 2008, 07:15 PM     #2 (permalink)
Go back to sleep
 
Creatures's Avatar
 
Join Date: Jul 2002
Location: Switzerland
Posts: 6,052
Send a message via ICQ to Creatures
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

Creatures is offline   Reply With Quote
Old September 21st, 2008, 07:25 PM     #3 (permalink)
Go back to sleep
 
Creatures's Avatar
 
Join Date: Jul 2002
Location: Switzerland
Posts: 6,052
Send a message via ICQ to Creatures
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

Creatures is offline   Reply With Quote
Old 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.
dchips13 is offline   Reply With Quote
Old 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);
dchips13 is offline   Reply With Quote
Old September 28th, 2008, 10:55 AM     #6 (permalink)
Ultimate Member
 
Rootstonian's Avatar
 
Join Date: Mar 2005
Location: Out of my mind
Posts: 2,742
Send a message via AIM to Rootstonian
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.
Rootstonian is online now   Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may post new threads
You may post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
binary to decimal in java omcmb Webmastering and Programming 6 September 21st, 2007 07:20 PM
Windows displaying problem hobo_man Technical Support 2 November 3rd, 2005 12:17 AM
binary to decimal in java jgargac Webmastering and Programming 8 April 25th, 2005 01:55 AM
Mandrake 8.1 Boots with 0's and 1's problem BostnBoy328 Technical Support 13 February 12th, 2002 12:37 AM
Mandrake 8.1 Boots with 0's and 1's problem BostnBoy328 Technical Support 1 December 27th, 2001 09:22 PM

Most Active Discussions
Is It Just Me? (528)
Misery Loves Company... (1849)
Why Does the MOON Grow Bigger as It.. (18)
heatsink issue (10)
New Mobo (18)
UPGRADING C/D DRIVE TO 250GB & .. (14)
SSD's, RAID, and External Backup (7)
1 internet. 1 house. 3 computer. ho.. (13)
Is This A Compatible Gaming PC? (18)
Recent Discussions
firewall (1)
C++ compiler suggestions (4)
Official World of Warcraft Thre.. (4529)
Programming question (2)
UPGRADING C/D DRIVE TO 250GB &a.. (14)
Ubuntu or Xubuntu....anyone!!? (7)
Building my first PC and need s.. (0)
PNY vs Gainward (3)
Linksys WRT54G (2)
FS: Dell 6000 laptop, modded 36.. (2)
Apple iPod touch 16 GB $200 (4)
Six 28-Disc Cross Design Black .. (4)


All times are GMT -4. The time now is 11:53 AM.
TechIMO Copyright 2008 All Enthusiast, Inc.



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28