Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Binary to Hexadecimal Conversion

Reply
Thread Tools

Binary to Hexadecimal Conversion

 
 
lei
Guest
Posts: n/a
 
      12-30-2006
Hello! I made this program of converting binary to hexadecimal, there
are few errors which are new to me. Please check it out. Thanks!

<code>

import java.lang.*;
import java.io.*;

class BinaryDecoder{
public static void main(String args[]) throws IOException{
InputStreamReader stdin = new InputStreamReader(System.in);
BufferedReader console = new BufferedReader(stdin);
System.out.print("Enter a number in binary: ");
String input = console.readLine();


int decimal=0;
for(int counter=input.length()-1; counter>=0; counter--){
if(input.charAt(counter)=='1'){
int exp=input.length()-1-counter;
decimal+=Math.pow(2,exp);
}
}

int hexadecimal=0;
int powerOfTen=1;
int number=decimal;
int counter=0;
int[] hex = new int[20];

while(number>0){
int remainder=number%16;
hex[counter] = remainder;
counter++;
number/=16;
}

System.out.print("Hexadecimal: ");
for(int count=hex.length; count>=0; count--){
if(hex[count]==10)
System.out.print("A");
else if(hex[count]==11)
System.out.print("B");
else if(hex[count]==12)
System.out.print("C");
else if(hex[count]==13)
System.out.print("D");
else if(hex[count]==14)
System.out.print("E");
else if(hex[count]==15)
System.out.print("F");
else
System.out.print(hex[count]);
}
}
}

</code>

 
Reply With Quote
 
 
 
 
lei
Guest
Posts: n/a
 
      12-30-2006
you can also suggest for improvements..thanks!

 
Reply With Quote
 
 
 
 
Brandon McCombs
Guest
Posts: n/a
 
      12-30-2006
lei wrote:
> Hello! I made this program of converting binary to hexadecimal, there
> are few errors which are new to me. Please check it out. Thanks!
>
> <code>
>
> import java.lang.*;
> import java.io.*;
>
> class BinaryDecoder{
> public static void main(String args[]) throws IOException{
> InputStreamReader stdin = new InputStreamReader(System.in);
> BufferedReader console = new BufferedReader(stdin);
> System.out.print("Enter a number in binary: ");
> String input = console.readLine();
>
>
> int decimal=0;
> for(int counter=input.length()-1; counter>=0; counter--){
> if(input.charAt(counter)=='1'){
> int exp=input.length()-1-counter;
> decimal+=Math.pow(2,exp);
> }
> }
>
> int hexadecimal=0;
> int powerOfTen=1;
> int number=decimal;
> int counter=0;
> int[] hex = new int[20];
>
> while(number>0){
> int remainder=number%16;
> hex[counter] = remainder;
> counter++;
> number/=16;
> }
>
> System.out.print("Hexadecimal: ");
> for(int count=hex.length; count>=0; count--){
> if(hex[count]==10)
> System.out.print("A");
> else if(hex[count]==11)
> System.out.print("B");
> else if(hex[count]==12)
> System.out.print("C");
> else if(hex[count]==13)
> System.out.print("D");
> else if(hex[count]==14)
> System.out.print("E");
> else if(hex[count]==15)
> System.out.print("F");
> else
> System.out.print(hex[count]);
> }
> }
> }
>
> </code>
>


How about you tell us the errors you have found so far instead of
relying on us to scan your code and/or compile it to generate the errors?
 
Reply With Quote
 
Thomas Schodt
Guest
Posts: n/a
 
      12-30-2006
lei wrote:
> I made this program of converting binary to hexadecimal,
> there are few errors which are new to me.


> for(int count=hex.length; count>=0; count--){
> if(hex[count]==10)


You are addressing the array 'hex' beyond the end.
 
Reply With Quote
 
lei
Guest
Posts: n/a
 
      12-30-2006

Thomas Schodt wrote:
> lei wrote:
> > I made this program of converting binary to hexadecimal,
> > there are few errors which are new to me.

>
> > for(int count=hex.length; count>=0; count--){
> > if(hex[count]==10)

>
> You are addressing the array 'hex' beyond the end.


please explain further and suggest a solution(if it's okay), thank you
so much!

 
Reply With Quote
 
lei
Guest
Posts: n/a
 
      12-30-2006

It's working now. Another problem:

Enter a number in binary: 10000011
Hexadecimal: 00000000000000000083

How can I remove the zeros?

 
Reply With Quote
 
Daniel Dyer
Guest
Posts: n/a
 
      12-30-2006
On Sat, 30 Dec 2006 09:31:44 -0000, lei <(E-Mail Removed)> wrote:

> you can also suggest for improvements..thanks!


It could be so much simpler:

http://java.sun.com/j2se/1.5.0/docs/...va.lang.String,
int)
http://java.sun.com/j2se/1.5.0/docs/...oHexString(int)

Everything after reading the input could be replaced with this one line:

Integer.toHexString(Integer.parseInt(input, 2));

Dan.

--
Daniel Dyer
https://watchmaker.dev.java.net - Evolutionary Algorithm Framework for Java
 
Reply With Quote
 
lei
Guest
Posts: n/a
 
      12-30-2006

Daniel Dyer wrote:
> On Sat, 30 Dec 2006 09:31:44 -0000, lei <(E-Mail Removed)> wrote:
>
> > you can also suggest for improvements..thanks!

>
> It could be so much simpler:
>
> http://java.sun.com/j2se/1.5.0/docs/...va.lang.String,
> int)
> http://java.sun.com/j2se/1.5.0/docs/...oHexString(int)
>
> Everything after reading the input could be replaced with this one line:
>
> Integer.toHexString(Integer.parseInt(input, 2));
>
> Dan.
>
> --
> Daniel Dyer
> https://watchmaker.dev.java.net - Evolutionary Algorithm Framework for Java


I can't use it. The idea is to create your own toHexString.

 
Reply With Quote
 
Daniel Dyer
Guest
Posts: n/a
 
      12-30-2006
On Sat, 30 Dec 2006 14:13:50 -0000, lei <(E-Mail Removed)> wrote:

>
> Daniel Dyer wrote:
>> On Sat, 30 Dec 2006 09:31:44 -0000, lei <(E-Mail Removed)> wrote:
>>
>> > you can also suggest for improvements..thanks!

>>
>> It could be so much simpler:
>>
>> http://java.sun.com/j2se/1.5.0/docs/...va.lang.String,
>> int)
>> http://java.sun.com/j2se/1.5.0/docs/...oHexString(int)
>>
>> Everything after reading the input could be replaced with this one line:
>>
>> Integer.toHexString(Integer.parseInt(input, 2));
>>
>> Dan.

>
> I can't use it. The idea is to create your own toHexString.


OK, that wasn't immediately clear.

Dan.

--
Daniel Dyer
https://watchmaker.dev.java.net - Evolutionary Algorithm Framework for Java
 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      12-30-2006
lei wrote:
> It's working now. Another problem:
>
> Enter a number in binary: 10000011
> Hexadecimal: 00000000000000000083
>
> How can I remove the zeros?


A very good question!

Take yourself off the keyboard for a few minutes, maybe actually use a
pen[cil] and paper to work through the inherent logic of the challenge.

Think along the lines of, "Hmm, what conditions pertain when a am preparing to
output a '0' that would require me to display it? What conditions would allow
me to suppress it?" (Hint: One of the conditions is that the numeral to
display at the moment is '0'.)

Then detect those conditions in your code, and either emit or decline to emit
the '0' as appropriate.

- Lew
 
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
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Binary to Hexadecimal conversion Deepu Perl Misc 5 12-17-2007 05:37 PM
(BitTorrent-related) binary "string" to hexadecimal? Shanti Braford Ruby 3 10-31-2007 02:04 AM
Convert Binary String to Hexadecimal elliotng.ee@gmail.com C Programming 7 12-27-2006 07:41 PM
Convert hexadecimal string to binary Eric Python 3 04-22-2004 06:42 PM
Hexadecimal to Binary File Conversion Utility Victor Hannak VHDL 0 02-20-2004 08:54 PM



Advertisments