Hex String To Byte Array C#

92,703

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

Share:
92,703
Matt
Author by

Matt

I'm a self-taught C# developer who has spent a lot of time learning the language over the years. I love to program and develop software and I'm hoping I can finally start to bring some of my knowledge here to help out beginners! I love Javascript, Node, Mongo, Python, SQL, web development and everything in between

Updated on November 17, 2020

Comments

  • Matt
    Matt over 3 years

    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
    Matt over 10 years
    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!