Go Back   AnandTech Forums > Software > Programming

Forums
· Hardware and Technology
· CPUs and Overclocking
· Motherboards
· Video Cards and Graphics
· Memory and Storage
· Power Supplies
· Cases & Cooling
· SFF, Notebooks, Pre-Built/Barebones PCs
· Networking
· Peripherals
· General Hardware
· Highly Technical
· Computer Help
· Home Theater PCs
· Consumer Electronics
· Digital and Video Cameras
· Mobile Devices & Gadgets
· Audio/Video & Home Theater
· Software
· Software for Windows
· All Things Apple
· *nix Software
· Operating Systems
· Programming
· PC Gaming
· Console Gaming
· Distributed Computing
· Security
· Social
· Off Topic
· Politics and News
· Discussion Club
· Love and Relationships
· The Garage
· Health and Fitness
· Merchandise and Shopping
· For Sale/Trade
· Hot Deals with Free Stuff/Contests
· Black Friday 2013
· Forum Issues
· Technical Forum Issues
· Personal Forum Issues
· Suggestion Box
· Moderator Resources
· Moderator Discussions
   

Reply
 
Thread Tools
Old 10-16-2007, 03:40 PM   #1
Avalon
Diamond Member
 
Avalon's Avatar
 
Join Date: Jul 2001
Posts: 7,344
Default toBinaryString in Java removes leading 0...help?

Does anyone know of a way to keep the leading 0 when a decimal is converted into a binary?
Say I'm converting 121 into binary, which would be 01111001. Instead, it hacks off the 0 and gives me 1111001, which is also correct, but I require that leading 0 so my program doesn't lose its place. Is there any way around that, or should I be using a different method?
__________________
On a mission to be the first to drink 500 unique beers at World of Beer UCF. Check out my progress and read tons of reviews at www.beeruken.com

Progress: 500/500 as of 10/18/12

Completed the 500 beer challenge!!!
Avalon is offline   Reply With Quote
Old 10-16-2007, 04:09 PM   #2
IHateMyJob2004
Lifer
 
IHateMyJob2004's Avatar
 
Join Date: Sep 2004
Posts: 15,887
Default toBinaryString in Java removes leading 0...help?

EWither Java 1.5 or 6 has formatted strings.

Much like the printf commands in C.

I've never used the new output functionality though.

I hope you are referring to displaying the value.





If you are talking about how it is stored in memmory, the leading 0 is not needed so give up.
__________________
Look at the Blackberry BB10 phones with an open mind. They are actually good!
FREE MUSIC - via torrent:
http://bt.etree.org/
or just download some free music:
http://www.archive.org/audio/etree-band-showall.php 100% LEGIT!
IHateMyJob2004 is offline   Reply With Quote
Old 10-16-2007, 04:40 PM   #3
Avalon
Diamond Member
 
Avalon's Avatar
 
Join Date: Jul 2001
Posts: 7,344
Default toBinaryString in Java removes leading 0...help?

Well, the reason I need it is because i have to XOR two values in my 8 bit binary string. Say my program says I XOR bit 1 and bit 8 of every 8 bit binary string. In my above case, I'd be XOR'ing 0 and 1, to get 1. However, since the leading 0 is being discarded, my program would XOR 1 and nothing, and then error out.

I think I figured it out though, by just inputting the binary value into a character array and adding leading 0's for however many characters I was short of 8 bits.
__________________
On a mission to be the first to drink 500 unique beers at World of Beer UCF. Check out my progress and read tons of reviews at www.beeruken.com

Progress: 500/500 as of 10/18/12

Completed the 500 beer challenge!!!
Avalon is offline   Reply With Quote
Old 10-16-2007, 10:16 PM   #4
statik213
Golden Member
 
Join Date: Oct 2004
Posts: 1,654
Default toBinaryString in Java removes leading 0...help?

You should not be doing xors using string operations... Use the btiwise operators for your bit level mainpulation.

All you need to do is something like this:

String num1 = "42";
String num2 = "137";

int result = Integer.parseInt(num1) ^ Integer.parseInt(num2);

String binary = Integer.toBinaryString(result);
String formatted = String.format("%8s", binary);
__________________
Finally a Programming forum!
statik213 is offline   Reply With Quote
Reply

Thread Tools

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -5. The time now is 05:15 AM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.