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.

Jodrell
  • 31,518
  • 3
  • 75
  • 114
TCM
  • 16,242
  • 43
  • 150
  • 245
  • 2
    Why are the `Encoding` classes not suitable? What problem are you trying to solve? – Oded Aug 16 '11 at 14:52
  • see this http://stackoverflow.com/questions/472906/net-string-to-byte-array-c – Haris Hasan Aug 16 '11 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? – asawyer Aug 16 '11 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. – thekip Aug 16 '11 at 14:52
  • So, you have a string that represents an encoded byte array, like you example and you want to convert it into a byte array? – Jodrell Aug 16 '11 at 14:53
  • That byte sample does not look like Unicode (to few 0x00) so I'm quite sure you do _not_ want to cast and that in fact you do need an Encoding. Maybe ASCII instead of UTF-8. – Henk Holterman Aug 16 '11 at 14:58
  • 1
    he wants the hex-string converted to the byte[] equivalent. So instead of making 0x42A string equal to the byte[] = {0x34,0x32,0x41} he wants it to be byte[] = {0x4, 0x2, 0xA} therefore he can't use the GetBytes()? – Daan Timmer Aug 16 '11 at 15:00
  • 3
    Here is a simple solution that I made: http://pastie.org/2380850 – jgauffin Aug 16 '11 at 15:06
  • @jgauffin: Exactly what I wanted. Please post it as answer. I need to accept it. My question was unncessarily closed without understanding what I wanted to say. Thankfully it is not downvoted..fingers cross!! – TCM Aug 16 '11 at 15:10
  • http://stackoverflow.com/questions/321370/convert-hex-string-to-byte-array and all the duplicates of that – Jodrell Aug 16 '11 at 15:10
  • That was exactly what I was thinking as well @jgauffin. (except that I took a nibble instead of the byte, my mistake), upvoted for the sake of a mistaken closing! – Daan Timmer Aug 16 '11 at 15:11
  • @Anthony, just upvote the answer of one of the preexisting questions, like http://stackoverflow.com/questions/321370/convert-hex-string-to-byte-array – Jodrell Aug 16 '11 at 15:12
  • 1
    People get so overly enthusiastic here. Just few misunderstandings in question and people come from nowhere and heavily downvote you or close your question. – TCM Aug 16 '11 at 15:15
  • 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. – Daniel Hilgarth Aug 16 '11 at 15:32
  • 1
    @Daniel: I didn't know how to express this. I expressed best what I knew. – TCM Aug 16 '11 at 15:34

1 Answers1

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.

Daniel Hilgarth
  • 159,901
  • 39
  • 297
  • 411