Solved

Hex to byte converter

Posted on 2011-03-11
3
1,081 Views
Last Modified: 2012-06-21
Ive written a simple line which converts a byte array to a hex string:-
          urlSafe = BitConverter.ToString(byt2Convert).Replace("-", "")

However I have no idea how to convert back to a byte array? Does anyone?

I cant seem to find any example code as simple as converting it from byte to string.

Any ideas?

Thank you
0
Comment
Question by:tonelm54
3 Comments
 
LVL 7

Expert Comment

by:deadlyDev
ID: 35107977
Straight from http://stackoverflow.com/questions/321370/convert-hex-string-to-byte-array
public static byte[] StringToByteArray(string hex) {
    return Enumerable.Range(0, hex.Length).
           Where(x => 0 == x % 2).
           Select(x => Convert.ToByte(hex.Substring(x, 2), 16)).
           ToArray();
}

Open in new window

0
 
LVL 41

Expert Comment

by:graye
ID: 35109185
Here is another similar technique that doesn't use lamba expressions


' There's no easy way to convert a hex byte string back into
            ' a byte array, so we have parse each byte
            ReDim in_buf((message.Length \ 2) - 1)
            For i As Integer = 0 To in_buf.Length - 1
                in_buf(i) = Byte.Parse(message.Substring(i * 2, 2), Globalization.NumberStyles.HexNumber)
            Next

Open in new window

0
 
LVL 3

Accepted Solution

by:
1303gun earned 500 total points
ID: 35116609
You can also try this alternative. Just call Hex2Byte and specify the hex string as parameter.
Function Hex2Byte(ByVal Hex As String) As Byte()
        Dim bytes As New List(Of Byte)
        For i As Integer = 0 To Hex.Length - 1 Step 2
            bytes.Add(Byte.Parse(Hex.Substring(i, 2), Globalization.NumberStyles.HexNumber))
        Next
        Return bytes.ToArray
    End Function

Open in new window

0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now