Solved

Hex to byte converter

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

ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

Question has a verified solution.

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

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
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.
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…

810 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