Solved

Hex to byte converter

Posted on 2011-03-11
3
1,106 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

920 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

18 Experts available now in Live!

Get 1:1 Help Now