I have this hex code and i want to convert to binary.

242465313037382c3031343536373536343335363738322c5858582c3134303632363033343033375f43314530312e6a70672c32332c302cc3bfc398c3bfc39be2809e13200e100e0c13100f10151413161c2f1e1c1a1a1c39292b222f44c2ab62e2809cc387243d03c592c2a9c387434c0e38064628c39f794e202d3101c2a4c2a601452534c593c39201282002c5bec2abc3b8c5bec39401c2a505c2a948c382e2809a15c39bc2ab3535c5a0c38648690139c3aa5ac2b4e280b921c2a1c28d2065210e7d4d371de280a127c39054c38b5654741de280b0c3986120c380c3b734c3a16f70c3b804c386e282ac761cc393c3a615e280a1c3bde28098c388c3834ec398c3bf6540c2a3c3acc5a00fc38d70c398c3bac5a0571ecb863cc2ab45c3ab213fc28d01c2ad13c2a47bc2bec2abc5a149e282acc2be7c23c3aec383c3ba203ec3947fe280a0151f5a2f7013c3ad52c3ba20c3bc2920c384c387c3b8c388c3ba2041713cc3997fc3a7c2abc3be746e73c395c39bc3b33420c3a1c38fc2ad1408514b400b566c57c3bd6b7d050345e280b010c2bc6ce282acc3a3771401e280b03de2809a01c3bac39058c3bc53c3a219e2809cc3b0c2a4c3981fc3bfc390c3abc2b6c5937bc393c3820e6801c3a146314b4c02c5a0282002c5a06b20742a4706c2b3c39e36195ec592290114e2809a5946646520c3bdc395c3afc3b5c2a8192a3741

but I don't know how to convert this kind of data.

Thank you in advance.

I found this solution on internet and it seems to work.

public static String hexToBin(String s) {
    return new BigInteger(s, 16).toString(2);
}

Why this use BigInteger, because your number is to big to fit in Integer so you need to use bigger container.
If you have hex format and you need to convert it to string just add String s = binary + "";
Then call this method, I hope it will work, in my test cases it works fine.
Regards.

Edited 2 Years Ago by milil

Do you mean a string like "100110001010100..." or an internal binary format such as a array of bytes?
If it's a byte[] then you can use a variant of the previous post...
New BigInteger(s, 16). toByteArray()

@milil,I think your solution is not what I want.,I tried to run it but it shows like something to this "1010101010101".

@jamescherill,

it is incomaptible type.

public static String hextoString(String s){
        return new BigInteger(s, 16).toByteArray();
    }

@jamescherill,
how do you print it.

    public static byte[] hextoString(String s){
       return new BigInteger(s, 16).toByteArray();
    }


//print output

  System.out.print(hextoString(hex));

I use this

 System.out.print(Arrays.toString(hextoString(hex)));

but the output is unexpected...

[36, 36, 72, 54, 51, 54, 44, 48, 49, 51, 50, 50, 55, 48, 48, 53, 53, 48, 52, 51, 55, 50, 44, 68, 48, 48, 44, 49, 52, 48, 54, 50, 54, 48, 51, 52, 48, 51, 55, 95, 67, 49, 69, 48, 49, 46, 106, 112, 103, 44, 50, 51, 44, 50, 50, 44, -64, 74, 41, 0, 81, 64, 13, 118, -2, 17, -99, -57, -83, 82, -107, -9, 29, -85, -9, 23, -96, -2, -76, -64, 101, 20, -64, 40, -96, 2, -116, -46, 1, 115, 69, 48, 22, -118, 0, 40, -96, 2, -118, 0, 40, -96, 2, -118, 0, 41, 40, 0, -94, -128, 10, 40, 0, -94, -128, 10, 40, 0, -94, -128, 10, 74, 0, 40, -96, 2, -118, 0, -1, -48, -49, -51, 38, 105, 0, -108, -103, -92, 1, -102, 76, -45, 1, 51, 73, -102, 0, 56, -92, -96, 3, 52, 82, 1, 40, -96, 4, 52, 80, 1, -102, 74, 0, 51, 69, 0, 37, 20, 0, 82, 80, 1, 73, -38, -128, 18, -111, -113, 106, 0, 20, 119, -89, 80, 1, 73, 64, 5, 20, 0, -108, 80, 1, 73, 64, 5, 20, 0, 82, 103, 3, 62, -108, -64, -85, 60, -104, -36, -65, -60, -33, 123, -38, -85, -45, 0, -94, -128, 10, 40, 0, -91, -92, 1, 74, 7, 52, -64, -112, 10, 82, 7, -91, 0, 68, 79, 52, -93, -102, 0, 8, -59, 3, -98, -108, 0, 81, 64, 5, 20, 0, 81, 64, 9, 69, 0, 20, 80, 1, 73, 72, 2, -118, 96, 20, 80, 1, 69, 0, 20, 80, 1, 69, 0, 127, -1, -47, -51, -93, 53, 32, 38, 105, 40, 1, 40, -96, 4, -92, -96, 2, -109, 52, 0, 81, -102, 0, 76, -47, -102, 0, 74, 51, 76, 2, -110, -112, 5, 20, -64, 40, -92, 2, 81, 64, 8, 104, -90, 3, 73, -59, 32, 25, 52, -128, 120, -94, -128, 10, 74, 0, 41, 40, 0, -94, -128, 18, -118, 0, 41, 40, 0, -88, -82, 37, -14, -41, 3, -87, -23, 76, 10, 84, 83, 0, -94, -128, 10, 41, 0, 82, -45, 0, -89, 32, -55, 52, 1, 40, 28, 83, 29, -128, -32, 80, 3, 20, 110, 108, 84, -92, 0, 40, 2, 54, -53, 54, -33, 122, -102, 49, -98, 79, 69, 24, 20, 0, -92, 15, 74, 105, 65, -118, 64, 70, -63, 71, 122, 102, 105, -128, -32, 9, -23, 70, 15, -91, 0, 37, 25, -96, 2, -118, 0, 40, -96, 2, -110, -128, 10, 41, 0, 81, 64, 5, 20, 0, 81, 76, 15, -1, -46, -52, -50, 122, -102, 74, -112, 19, 52, 102, -128, 19, 52, -122, -128, 10, 74, 0, 41, 40, 0, -94, -128, 18, -113, -62, -128, 10, 74, 0, 41, 40, 0, -94, -128, 10, 41, -128, -108, 80, 2, 80, 79, 20, -128, 102, 115, 79, 3, 2, -128, 22, -109, 52, 0, 82, 80, 1, 69, 0, 37, 20, 0, 81, 64, 9, 69, 48, 27, 36, -126, 56, -53, 17, -109, -48, 15, 122, -96, -52, 89, -117, 55, 83, 66, 1, 40, -90, 1, 69, 0, 20, 80, 1, 69, 32, 22, -89, -116, 109, 65, 76, 1, -50, 5, 64, 78, 77, 0, 75, 16, -62, -25, -42, -119, 27, 28, 126, 116, 0, -44, 83, -127, -45, -26, -29, -23, 83, -77, 42, 40, 4, -48, 4, 38, 111, 65, -7, -45, 50, -17, -22, 126, -108, 0, 20, 96, -69, -120, 32, 116, -91, 68, -35, 64, 19, 1, -127, -127, -48, 82, -112, 40, 1, -116, 5, 48, -127, 64, 9, -74, -115, -76, 0, 109, 52, 109, 52, 0, -104, 52, -108, 0, 81, 64, 5, 20, 0, 81, 72, 2, -118, 96, 127, -1, -39, 42, 48, 66]

This is not the actual data

Edited 2 Years Ago by ezekel

@jamescherill,

can you please post your output.

Thank you in advance.

Here's the simple test case I used:

       String in = "1020fffe0100";
       BigInteger bi = new BigInteger(in, 16);
       byte[] bytes = bi.toByteArray();
       System.out.println(Arrays.toString(bytes)

and the output was

[16, 32, -1, -2, 1, 0]

exactly as expected.

@jamescherill,

Thank you for showing some snippet,...maybe I am wrong in asking the convertion I think my question for my problem is converting hexcode to string ?.I don't know if that is the right word...I apologize for my question.

OK. Maybe you can give an small example of some input, and the output that you want? You will need to explain whether the output is just a Sring, or whether it's a printable version of some binary data.

@jamescherill,how can you convert to string ?

242447313037382c3031333232373030353530343337322c4430302c3134303632363033343035395f43314530312e6a70672c32302c302cffd8ffdb008400130d0e100e0c13100f10151413161c2f1e1c1a1a1c39292b222f443c4746433c42404b546c5b4b50665140425e805f666f73797a79495a848e83758d6c767974011415151c191c371e1e37744d424d7474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474ffc000110801e0028003012100021101031101ffdd00040028ffc401a20000010501010101010100000000000000000102030405060708090a0b100002010303020403050504040000017d01020300041105122131410613516107227114328191a1082342b1c11552d1f02433627282090a161718191a25262728292a3435363738393a434445464748494a535455565758595a636465666768696a737475767778797a838485868788898a92939495969798999aa2a3a4a5a6a7a8a9aab2b3b4b5b6b7b8b9bac2c3c4c5c6c7c8c9cad2d3d4d5d6d7d8d9dae1e2e3e4e5e6e7e8e9eaf1f2f3f4f5f6f7f8f9fa0100030101010101010101010000000000000102030405060708090a0b1100020102040403040705040400010277000102031104052131061241510761711322328108144291a1b1c109233352f0156272d10a162434e125f11718191a262728292a35363738393a434445464748494a535455565758595a636465666768696a737475767778797a82838485868788898a92939495969798999aa2a3a4a5a6a7a8a9aab2b3b4b5b6b7b8b9bac2c3c4c5c6c7c8c9cad2d3d4d5d6d7d8d9dae2e3e4e5e6e7e8e9eaf2f3f4f5f6f7f8f9faffda000c03010002110311003f00e888a69e2b128723f6a578c38c8ebeb40125add95222b827fd973dfeb57ab54c4c28a621b2c6934652450ca7a835ccdfd95c695389ed9884cf0c3b7b1acaa2bea694df436b4cd452fe2e405957ef255a7976305d8c73dc0ac3697306da31eac1871f952d74dd3330a2aafa0051537021bbb58aee1314ab907a1f435c9de5ac969398a5073fc2dd88ace51ea6d4ddd58d5d1355395b4b96e7a46e4f5f635bb5ac5dd19c959851438dddc90a2980514c028a0028a0028a0028a0028a0028a0028a0028a0028a0028a0028acea0d05326963823324ac15477352b50312fb5669f2903148fb9e84d663495d115644dc631c8e2a02deb4c001078350ca8ca72bcd261722f30f7a4f339a7718e043542c79c50c4902b1f5c1156a0bfbb8d762dcc981d0139fe753643b8ada85cbb7cce3ebb46690dd5c6d3b67753ec714b910ee422eee5badccc7feda1a3ed1267972dfef7359bb2342cc3a94f08c24aea3d0722a46d4ae24e0dc37e1c509264bba226919fef316fa9a61e6a913b8c6150b0a0088f5a6934c4237dc6fa1af42b039d3ed4ff00d315fe54809eb26ff9bc63ec2819ffd0e8a90f3589630f06a447a005922a3744

Edited 1 Year Ago by ezekel

I don't understand.
That thing you just posted, that begins 24244..., is a string.
It would be helpful if you answer my previous question.

You start with a String whose value is a string representation of a sequence of hex digits. Exactly, exactly, what do you want to convert that into?

Edited 1 Year Ago by JamesCherrill

@jamescherill,

I thinks that is a hexcode.

Edited 1 Year Ago by ezekel

We are getting nowhere. I am trying to understand your problem, but you don't answer my questions.
Until you can explain exactly what you want as the result of the conversion (specify a Java data type) there's nothing more I can do to help.

ps: Please don't PM me. I don't provide support by PMs.

You might have better luck if you explain what the data is for. Do you want to break the string into 2-character hex strings and convert those to binary? If so,

From the original post:
242465313037382c303134353637...

Splitting the string into 2-character pieces would be:
24 24 65 31 30 37 38 2c 30 31 34 35 36 37...

Then convert each one to binary:
24 (hex) = 36 (decimal) = 00100100 (binary)
65 (hex) = 101 (decimal) = 01100101 (binary)

and substituting the binary equivalent of "242465" in the above string yields:
001001000010010001100101...

Hex is from 0 - 15 (or 0-F). Binary follows a pattern of 0 and 1. So for two places: 00, 01, 10, 11.

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

If I add spaces it is easier to see the pattern:

Hex - Binary

0 = 00 00
1 = 00 01
2 = 00 10
3 = 00 11

4 = 01 00
5 = 01 01
6 = 01 10
7 = 01 11

8 = 10 00
9 = 10 01
A = 10 10
B = 10 11

C = 11 00
D = 11 01
E = 11 10
F = 11 11

Now you can see the 00, 01, 10, 11 pattern in both the right two positions and the left two positions.

Note: Padding a number with 0's has no effect on the number. It's only for our readability (and sometimes used to create consistency so we know how to break it apart into a string)

I don't know your data, but these values may represent ASCII characters which may be what you're refering to as a "String".

If what I've posted is the case, then the following may be of use to you:

Java: How to split a string by a number of characters

Convert hex string to binary string

Convert Hex to ASCII and ASCII to Hex

Edited 1 Year Ago by cgeier

This article has been dead for over six months. Start a new discussion instead.