944,028 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 4770
  • Java RSS
Jan 15th, 2005
0

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

Expand Post »
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.
Similar Threads
Reputation Points: 13
Solved Threads: 0
Junior Poster in Training
kharri5 is offline Offline
56 posts
since Jan 2005
Jan 15th, 2005
0

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

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.
Reputation Points: 113
Solved Threads: 19
Postaholic
server_crash is offline Offline
2,108 posts
since Jun 2004
Jan 15th, 2005
0

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

ooh thanks! appreciate it Much
Reputation Points: 13
Solved Threads: 0
Junior Poster in Training
kharri5 is offline Offline
56 posts
since Jan 2005
Jan 16th, 2005
0

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

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
Reputation Points: 25
Solved Threads: 10
Practically a Master Poster
freesoft_2000 is offline Offline
623 posts
since Jun 2004
Jan 16th, 2005
0

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

What about this? It's probably not exactly what you want, but it will get you on your way.

Java Syntax (Toggle Plain Text)
  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. }
Reputation Points: 113
Solved Threads: 19
Postaholic
server_crash is offline Offline
2,108 posts
since Jun 2004
Jan 16th, 2005
0

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

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.
Reputation Points: 13
Solved Threads: 0
Junior Poster in Training
kharri5 is offline Offline
56 posts
since Jan 2005
Jan 16th, 2005
0

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

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.
Team Colleague
Reputation Points: 1658
Solved Threads: 331
duckman
jwenting is offline Offline
7,719 posts
since Nov 2004
Jan 17th, 2005
0

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

Thanks for the help
Reputation Points: 13
Solved Threads: 0
Junior Poster in Training
kharri5 is offline Offline
56 posts
since Jan 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Scanf Equivalent
Next Thread in Java Forum Timeline: Adjusting screen size





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC