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.

## All 7 Replies

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.

ooh thanks! appreciate it Much

Hi everyone,

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

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

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

``````import java.io.*;

public class HexToByteConverter{

public static void main(String[] args) throws IOException{

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String line = null;
while (!(line = br.readLine()).equals("")){
for (int i = 0,j = line.length(); i < j; i++){

System.out.print(Integer.toBinaryString(Character.digit(line.charAt(i),16)));
}
System.out.println();
}
}
}``````

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.

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.

Thanks for the help

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.