Click here to Skip to main content
15,890,897 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi

I asked the question a while back on how to convert a byte[] to a string looking something like 0x3130353030353032.

The solution was

C#
myTextBox.Text="0x";
foreach(byte b in array)
{
  myTextBox.Text += b.ToString("X2");
}

now I would please like to know how would I convert that string back to a byte[]

Thanks in advanced
Posted
Updated 25-Aug-11 21:03pm
v2
Comments
Prerak Patel 26-Aug-11 3:04am    
Use code block for code segment.

1 solution

Two hints:

1) As you should know, a byte takes two hexadecimal digits to be presented as a hexadecimal string. Split your string into the array of two-digit strings; the length of this array will give you the length of the byte[] array you should initialize.

2) To parse a string into byte, use byte.Parse(string, NumberStyles), use System.Globalization.NumberStyles.HexNumber.

See:
http://msdn.microsoft.com/en-us/library/4eszwye3.aspx[^],
http://msdn.microsoft.com/en-us/library/system.globalization.numberstyles.aspx[^].

—SA
 
Share this answer
 
Comments
CPallini 26-Aug-11 3:13am    
5.Or do it manually, it is a good exercise.
Sergey Alexandrovich Kryukov 26-Aug-11 3:25am    
Thank you. But... how manually?..
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900