3

This is a duplicate question, my apologies everyone!

Firstly, I apologize if this is a simple question, I have been searching for a very long time, and either an answer about this does not exist, the answer I am looking for has been buried under answers to questions about how to convert a string to a byte array, or I'm not searching with the right terminology. I have also found a few answers on converting a single hex value into a byte but applying those methods to work with what I want to do doesn't really seem to work very well.

What I am looking for is not how to convert "string" to a byte array, rather, I am trying to convert an already in bytes value from a textbox, into something my application will recognize as a byte array. I'll try to explain better with an example:

textBox.Text = 019F314A
I want byte[] bytes to equal { 0x01, 0x9F, 0x31, 0x4A }

Hopefully that makes sense. Thanks to anyone who can offer any assistance!

Matt
  • 621
  • 1
  • 6
  • 24
  • @ScottChamberlain Seems to work, I knew there had to be an answer for this somewhere, calling it a hex string only occurred to me right before I posted the question. Thank you – Matt Nov 17 '13 at 07:05

1 Answers1

6

I believe you can use Convert.ToByte(), you might have to slice your string in pairs and loop through it.

If you do a quick search there are many topics on this already on stackoverflow

How do you convert Byte Array to Hexadecimal String, and vice versa?

You can also look at this MS example, it is to convert to int, but the idea is the same. http://msdn.microsoft.com/en-us/library/bb311038.aspx

Community
  • 1
  • 1
raf
  • 267
  • 1
  • 5
  • Yeah, this is exactly what I was looking for. Sorry for the duplicate question, like I said above I didn't think to call it a hex string until just before I posted the question. Thank you! – Matt Nov 17 '13 at 07:10