shaiss
shaiss

Reputation: 2010

Hexadecimal to 8-bit unsigned array in VB.NET

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?

Upvotes: 2

Views: 2283

Answers (2)

JaredPar
JaredPar

Reputation: 755317

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.

Upvotes: 2

MarkL
MarkL

Reputation: 1771

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

Upvotes: 0

Related Questions