The Artima Developer Community
Sponsored Link

Java Answers Forum
Code for converting binary to hexidecimal

4 replies on 1 page. Most recent reply: Feb 6, 2003 2:31 PM by John C

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 4 replies on 1 page
gydiwon gydiwon

Posts: 1
Nickname: gydiwon
Registered: Jan, 2003

Code for converting binary to hexidecimal Posted: Jan 30, 2003 9:38 PM
Reply to this message Reply
Advertisement
I'm trying to create a method that will convert binary numbers into hexidecimal.

Ok, so I decided the best way to do this is to have the user input the binary number as a String in groups of four. So they would put in something like this "0001 0110 1110".
I'm using the StringTokenizer class to take each group of four and convert into its appropriate Hexidecimal. My problem is the tokens aren't being recognized.

Here's an abridged version of my code.

public void BinToHex(String s) {
StringBuffer buffer = new StringBuffer(1000);

/*I create the tokenizer and set the white space as
the delimeter */

StringTokenizer bin = new StringTokenizer(s," ");

while (bin.hasMoreTokens()) {
String c = bin.nextToken();
if (c == "0000")
buffer.append('0');
else if (c == "0001")
buffer.append('1');
else if (c == "0010")
buffer.append('2');
else if (c == "0011")
buffer.append('3');

/*I keep going with these if statements
for the rest of the while loop */
}

System.out.println(buffer.reverse());
}

So all of this and I just end up with an empty stringbuffer. My suspicion is that the actual tokens aren't matching up with the predicted ones. So bin.nextToken() doesn't actually equal "0011". What then, does it equal??? Are there hidden characters or something? Is there a better way to do this code?


Mike Penner

Posts: 16
Nickname: mikepenner
Registered: Jan, 2003

Re: Code for converting binary to hexidecimal Posted: Jan 31, 2003 2:56 PM
Reply to this message Reply
The first thing I'd try is adding a debug line after your call to bin.nextToken(). Maybe something like:

System.out.println("Found token:" + c);

Also, it wouldn't hurt to dump the number of tokens up front, by adding the following line of code right after you declare your StringTokenizer:

System.out.println("Found " + bin.countTokens() + " tokens");

This debug information might help you get a handle on what's going wrong.

Don Hill

Posts: 70
Nickname: ssswdon
Registered: Jul, 2002

Re: Code for converting binary to hexidecimal Posted: Jan 31, 2003 7:47 PM
Reply to this message Reply
heres something to get you in the right direction
private static void printBinary(byte b) {
for (int sh = 7; sh >= 0; sh--) {
System.out.print((b >> sh) & 1);
}

System.out.println();
}

private static void printBinary(int i) {
for (int sh = 31; sh >= 0; sh--) {
System.out.print((i >> sh) & 1);
}

System.out.println();
}

tengtium

Posts: 3
Nickname: tengtium
Registered: Feb, 2003

Re: Code for converting binary to hexidecimal Posted: Feb 4, 2003 10:45 PM
Reply to this message Reply
try this program:

public class NumberSystem {
public static void main(String[] s) {
int num = 15;
// convert decimal to hex string
System.out.println(Integer.toHexString(num));
// convert decimal to binary string
System.out.println(Integer.toBinaryString(num));
// convert decimal to octal string
System.out.println(Integer.toOctalString(num));

// convert octal string to decimal integer
System.out.println(Integer.parseInt("17", 8));
// convert hex string to decimal integer
System.out.println(Integer.parseInt("F", 16));
// convert binary string to decimal integer
System.out.println(Integer.parseInt("1111",2));
// convert decimal string to decimal integer
System.out.println(Integer.parseInt("15",10));

// convert binary to decimal integer
System.out.println(Integer.valueOf("1111", 2));
// convert octal to decimal
System.out.println(Integer.valueOf("17", 8));
// convert hex to decimal
System.out.println(Integer.valueOf("F", 16));
// convert decimal to decimal
System.out.println(Integer.valueOf("15", 10));

System.out.println(Integer.decode("F#"));
}
}

John C

Posts: 1
Nickname: cordwriter
Registered: Feb, 2003

Re: Code for converting binary to hexidecimal Posted: Feb 6, 2003 2:31 PM
Reply to this message Reply
Strings are objects, so checking for equality (==) checks for equal memory locations (as well as contents). It's not likely that a token will have the same memory location as a String object "0000," for example. That's why your "==" tests are failing.

Rather, to compare strings like you're wanting to do, use the equals() method.

So, instead of:
  while (bin.hasMoreTokens()) {
String c = bin.nextToken();
if (c == "0000")
buffer.append('0');
else if (c == "0001") ... }
try this:
  while (bin.hasMoreTokens()) {
String c = bin.nextToken();
if (c.equals("0000"))
buffer.append('0');
else if (c.equals("0001")) ... }
and so forth.

Hope this gets you going in the right direction.

Flat View: This topic has 4 replies on 1 page
Topic: View J2EE-based forum at www.jiris.com Previous Topic   Next Topic Topic: Bit flags vs Boolean

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use