3

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.

15
  • 2
    Why are the Encoding classes not suitable? What problem are you trying to solve? Commented Aug 16, 2011 at 14:52
  • 1
    You want to transform a string to a byte array but you don't want to specify the encoding. How would you possible decode it back to a string without knowing how it was encoded? Commented Aug 16, 2011 at 14:52
  • 1
    How did you encode the data when you where writing it to your database? You will need this when reading it from the db. Commented Aug 16, 2011 at 14:52
  • 3
    Here is a simple solution that I made: pastie.org/2380850 Commented Aug 16, 2011 at 15:06
  • 3
    @Anthony: While I agree with you that this is a problem, in this case, the problem is that you simply didn't state what you really wanted. Commented Aug 16, 2011 at 15:32

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.