TCM
TCM

Reputation: 16900

How can I convert a string of hex to a byte[]

I do not want to encode a string to a byte[]. I want to turn a string of hex numbers to a byte[]. How can I do that?

Note: I again repeat I do not want to use Encoding.UTF8.GetBytes() or any other encoding.

A sample string is detailed below:

0x42A2C6A046057454C2D1AB2CE5A0147ACF1E728E1888367CF3218A1D513C72E582DBDC7F8C4674777CA148E4EFA0B4944BB4998F446724D4F56D96B507EAE619

How can I convert this string to a byte[] of the numbers in the string.

Upvotes: 3

Views: 233

Answers (1)

Daniel Hilgarth
Daniel Hilgarth

Reputation: 174329

There is no unambiguous way to convert a string to a byte array, that's why you need to use the Encoding class. In your case, you can use Encoding.ASCII.GetBytes(), because you only have characters from the ASCII charset.

Upvotes: 2

Related Questions