2

I am converting the hexadecimal number 0XA85D10 to binary. However, I am told you can convert this number without using paper or a calculator.

Is this possible?

Thanks

6 Answers 6

1

The easiest way to convert from hex to binary is to split each digit in the hex into an 4 digit binary number. (i.e. D matches to 1110)

Using your example

A     8     5     D     1    0

1010  1000  0101  1110  0001 0000
2
  • I'd have preferred you just showed how to do this, instead of giving the exact answer, which may just be homework. Commented Jan 21, 2016 at 0:04
  • @StevenHansen Sorry for that. Next time include a made up example or explain that you don't want the exact answer. It was just the most readily available example.
    – Rhuarc13
    Commented Jan 21, 2016 at 0:06
1

Yes. It is possible. One hexadecimal digit is exactly four binary digits.

A = 1010
8 = 1000

...and so on.

If the digit is greater or equal to 8, then subtract 8 from the digit the first binary digit is 1, otherwise it is zero.

If the digit is now greater or equal to 4, then subtract 4 and the next digit out is 1, otherwise the next digit out is 0.

If the digit is now greater or equal to 2, then subtract 2 and the next digit out is 1, otherwise the next digit out is 0.

Whatever is left, 0 or 1, is the remaining digit.

0

Easily. Each digit in a hex number translates to 4 digits in a binary number, so you only need to know the binary numbers from 0 to f, or 0000 to 1111.

As an example:

0xc3e2

c = 12 decimal = 1100
3 =              0011
e = 14 decimal = 1110
2 =              0010

Then just string them together.

0xc3e2 = 1100001111100010 binary
0

You can convert Hexadecimal to Binary using the following (Hex -> Binary):

0 = 0000
1 = 0001
2 = 0010
3 = 0011
4 = 0100
5 = 0101
6 = 0110
7 = 0111
8 = 1000
9 = 1001
A = 1010
B = 1011
C = 1100
D = 1101
E = 1110
F = 1111

Hope this helps!

0
public with sharing class HexToBin {
private String code;
public String result;
Map<String, String> hexToBinMap = new Map<String, String>{'0'=>'0000','1'=>'0001','2'=>'0010','3'=>'0011','4'=>'0100','5'=>'0101','6'=>'0110','7'=>'0111','8'=>'1000','9'=>'1001','A'=>'1010','B'=>'1011','C'=>'1100','D'=>'1101','E'=>'1110','F'=>'1111'}; 
public HexToBin(String code) {
    this.code = code;
    result = getResult(code);
}

private String hexToBin(String hex){
    if(hex.length()==1){
        return hexToBinMap.get(hex);
    }
    return '';
}

private String getResult (String code) {
    String res ='';
    for(Integer i=0; i<code.length(); i++){         
        res =  res + hexToBin(code.substring(i,i+1));
    }
    return res;
}   

}

0

depends on how long is the hex. If it's 32 hex digits or fewer, one at a time via lookup table is fine. otherwise, consider using high speed regex substitution :

  • pre-pad all 0s and 1s with 000
  • keep searching for the 1st hex digit that's not 0 or 1 until no more exists, each round using s///g or gsub() (or equivalents) to replace that character with its 4 digit binary string
    • mixed-case hex, while uncommon, is still valid hex

the same approach could easily be adapted to convert any base that's an integer power of the output base.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.