user5473706
user5473706

Reputation: 29

Convert hexadecimal to binary

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

Upvotes: 2

Views: 5688

Answers (6)

RARE Kpop Manifesto
RARE Kpop Manifesto

Reputation: 2807

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.

Upvotes: 0

Name Surname
Name Surname

Reputation: 379

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;
}   

}

Upvotes: 0

Matthew Spencer
Matthew Spencer

Reputation: 2295

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!

Upvotes: 0

Steven Hansen
Steven Hansen

Reputation: 3239

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

Upvotes: 0

John Sensebe
John Sensebe

Reputation: 1396

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.

Upvotes: 1

Rhuarc13
Rhuarc13

Reputation: 135

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

Upvotes: 1

Related Questions