views:

505

answers:

2

I have a hexadecimal value

07A5953EE7592CE8871EE287F9C0A5FBC2BB43695589D95E76A4A9D37019C8

which I want to convert to a byte array.

Is there a built in function in .NET 3.5 that will get the job done or will I need to write a function to loop through each pair in the string and convert it to its 8-bit integer equivalent?

+1  A: 

There is no built-in function that will do this. You will unfortunately have to code one up :(

Public Function ToHexList(ByVal str As String) As List(Of Byte) 
  Dim list As New List(Of Byte)
  For i = 0 to str.Length-1 Step 2
    list.Add(Byte.Parse(str.SubString(i,2), Globalization.NumberStyles.HexNumber))
  Next
  Return list
End Function

EDIT

Qualified the NumberStyles enumeration with the Globalization namespace qualifier. Another option is to import that namespace and remove the qualifier.

JaredPar
Thank you for posting that code. Might be a noob question, but I get NumberStyles is not declared? I know it would be Dim NumberStyles as ? Thank you
shaiss
@shaiss Try importing System.Globalization in that file.
JaredPar
Now I get 'Hex' is not a member of 'System.Globalization.NumberStyles'
shaiss
NumberStyles.HexNumber seems to work instead of NumberStyles.Hex
shaiss
@shaiss, my bad, you're correct. HexNumber is the value I should have put up there.
JaredPar
A: 

I think that you'll find what you are looking for here (codeproject.com)

MarkL
I looked at this one as well, I was hoping for something built in to .net
shaiss