4

I have a string representing a hexidecimal value: "46-C9-08-B6-E8-F3-47-CF-53-2A-77-02-C9-19-7F"

I want to convert this to a byte array so it looks something like this: {&H46, &HC9, &H8, &HB6, &HE8, &HF3, &H47, &HCF, &H53, &H2A, &H77, &H2, &HC9, &H19, &H7F}

How do I do this?

4
  • What data type is that hex value stored as? Commented Feb 20, 2013 at 0:52
  • I am taking the string from a textbox as 46-C9-08-B6-E8-F3-47-CF-53-2A-77-02-C9-19-7F Commented Feb 20, 2013 at 0:52
  • What have you tried? Commented Feb 20, 2013 at 0:54
  • I put the code in this pastebin: pastebin.com/4zQiiYZt Commented Feb 20, 2013 at 0:56

1 Answer 1

3

Split the string and parse the hexadecimal numbers:

Dim bytes As Byte() = input.Split("-"c).Select(Function(n) Convert.ToByte(Convert.ToInt32(n, 16))).ToArray()
Sign up to request clarification or add additional context in comments.

2 Comments

If you could explain this a bit more in depth as im new to this I would appreciate it. The word "Input" gets underlined when I paste this code.
input is a string holding your value you want to convert.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.