How do I Take in Hexidecimal input out put binary, decimal, and octal

Reply

Join Date: Jan 2005
Posts: 56
Reputation: kharri5 is an unknown quantity at this point 
Solved Threads: 0
kharri5's Avatar
kharri5 kharri5 is offline Offline
Junior Poster in Training

How do I Take in Hexidecimal input out put binary, decimal, and octal

 
0
  #1
Jan 15th, 2005
I am trying to write a program in Java that takes in someons hexidecimal input. To make it eaiser I just want to do two digit hex. So they put in A2, and then I want the binary output for that. What I do know how to do is put a decimal to binary, so I wanted to try and say okay put A to be 10 in decimal, and 2 to be...well 2 in decimal. break the two into 4 bit arrays and do the binary for each, then comibe each togther into an 8 bit array and thats the binary, then from there I could try and do decimal, and octal by multipliying the necessary 1*2^(slot position) etc. My problem is figuring out how the hell to tell the computer that A means 10 and F means 15. I have no idea how to write this bloody thing. If anyone knows I woudl greatly appreciate, i've been trying to google it with less than satisfactory results.
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 2,108
Reputation: server_crash is on a distinguished road 
Solved Threads: 18
server_crash server_crash is offline Offline
Postaholic

Re: How do I Take in Hexidecimal input out put binary, decimal, and octal

 
0
  #2
Jan 15th, 2005
I wonder if you'll have to do any bit shifting. hmm. I'll have to do some searching and see what I can find. I know I did this in vb.net(except I took regular numbers), but I can't remember the formula. I'll get back to ya.
Reply With Quote Quick reply to this message  
Join Date: Jan 2005
Posts: 56
Reputation: kharri5 is an unknown quantity at this point 
Solved Threads: 0
kharri5's Avatar
kharri5 kharri5 is offline Offline
Junior Poster in Training

Re: How do I Take in Hexidecimal input out put binary, decimal, and octal

 
0
  #3
Jan 15th, 2005
ooh thanks! appreciate it Much
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 609
Reputation: freesoft_2000 is an unknown quantity at this point 
Solved Threads: 7
freesoft_2000 freesoft_2000 is offline Offline
Practically a Master Poster

Re: How do I Take in Hexidecimal input out put binary, decimal, and octal

 
0
  #4
Jan 16th, 2005
Hi everyone,

kharri5 you can try the below link. It really came useful to me.

http://www.janeg.ca/scjp/oper/binhex.html

You will need to use the modulus function most of time. The above link only explains the concepts but you will have to implement them in the required functions.

If you need further help post your questions here again

I hope the link helped you

Yours Sincerely

Richard West
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 2,108
Reputation: server_crash is on a distinguished road 
Solved Threads: 18
server_crash server_crash is offline Offline
Postaholic

Re: How do I Take in Hexidecimal input out put binary, decimal, and octal

 
0
  #5
Jan 16th, 2005
What about this? It's probably not exactly what you want, but it will get you on your way.

  1. import java.io.*;
  2.  
  3. public class HexToByteConverter{
  4.  
  5. public static void main(String[] args) throws IOException{
  6.  
  7. BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  8. String line = null;
  9. while (!(line = br.readLine()).equals("")){
  10. for (int i = 0,j = line.length(); i < j; i++){
  11.  
  12. System.out.print(Integer.toBinaryString(Character.digit(line.charAt(i),16)));
  13. }
  14. System.out.println();
  15. }
  16. }
  17. }
Reply With Quote Quick reply to this message  
Join Date: Jan 2005
Posts: 56
Reputation: kharri5 is an unknown quantity at this point 
Solved Threads: 0
kharri5's Avatar
kharri5 kharri5 is offline Offline
Junior Poster in Training

Re: How do I Take in Hexidecimal input out put binary, decimal, and octal

 
0
  #6
Jan 16th, 2005
I appreciate your posting. I dunno how much help it will be because I have to later convert this finally into assembly (first time doing assembly coding) so I am not sure if Java specific library functions will port over nicely, but I guess i neglected to mention before i was porting it to assembly. I thought I had...sorry about that. But thank you much for your response. I appreciate it.
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 6,143
Reputation: jwenting is just really nice jwenting is just really nice jwenting is just really nice jwenting is just really nice 
Solved Threads: 213
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: How do I Take in Hexidecimal input out put binary, decimal, and octal

 
0
  #7
Jan 16th, 2005
The classes Integer and NumberFormat have functions for converting between decimal, hex, octal, binary, and pretty much any other system you can think of.

No need to do it all yourself, it's right there in the standard API!

If you want individual digits out of a string converted, just take substrings of 1 character long and you're home (almost) free.
Reply With Quote Quick reply to this message  
Join Date: Jan 2005
Posts: 56
Reputation: kharri5 is an unknown quantity at this point 
Solved Threads: 0
kharri5's Avatar
kharri5 kharri5 is offline Offline
Junior Poster in Training

Re: How do I Take in Hexidecimal input out put binary, decimal, and octal

 
0
  #8
Jan 17th, 2005
Thanks for the help
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Java Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC