Application Scenario 1:
textbox input str = "200" (string type), must be converted to 16 hex hex = c8 (byte, char, int type)
Solution:
.. using Convert.ToByte (str) / ToChar (str ) / ToInt16 ( .. str) / ToInt32 (str ) / ToInt64 (str)
or
byte / char / int.TryParse (str, out hex); automatically converted to binary data 16
Example:
string mytime_str = time.Text;//textbox名为time
byte mytime_b;
byte.TryParse(mytime_str, out mytime_b);
//mytime_b = Convert.ToByte(mytime_str);
Application Scenario 2:
textbox input str = "010203040506" (string type), must be converted to hexadecimal array (byte [], char [] , int [] types)
Solution:
Method a:
firstly str.Substring (specified character position, specified length) pair off the string, then according Convert.To or .TryParse can be directly converted into a hexadecimal character string characteristics byte / int / char corresponding to the converted character string twenty-two hexadecimal byte / int / char
method two:
first, the character string into a string type byte [] / char [] / int [] array type (ASCII, non-hex) - using Encoding.Default.GetBytes (str) / GetChars (. str)
or
str.ToCharArray ();
then converted according to the conversion table ASCII hexadecimal
Note:
. 1, Encoding.Default.GetBytes (str) / the GetChars (str) and str.ToCharArray () to convert the string to ASCII table. the hexadecimal byte / char array, rather than the actual corresponding hexadecimal numbers, the string is not found directly becomes a function of the hexadecimal array
2, str.Replace ( "#", "") can be Alternatively independent strings
Example:
a method (recommended)
private static byte[] strToToHexBytes(string hexString)
{
hexString = hexString.Replace(" ", "");
if ((hexString.Length % 2) != 0)
hexString += " ";
byte[] returnBytes = new byte[hexString.Length / 2];
for (int i = 0; i < returnBytes.Length; i++)
returnBytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2), 16);
return returnBytes;
}
Method Two
public static byte[] StringToHexBytes(String s)
{
s = s.Replace(" ", "");
s = s.Replace("#", "");
byte[] b_ASCII = Encoding.Default.GetBytes(s);
byte[] b_Hex;
if (b_ASCII.Length%2==0)//偶数两个数字为一个字节
{
b_Hex = new byte[b_ASCII.Length / 2];
for (int j = 0; j < b_ASCII.Length; j++)
{
if ((b_ASCII[j] & 0xf0) == 0x40 | (b_ASCII[j] & 0xf0) == 0x60)
{
b_ASCII[j] = (byte)((b_ASCII[j] & 0x0f) - 0x01 + 0x0a);
}
else if ((b_ASCII[j] & 0xf0) == 0x30)
{
b_ASCII[j] = b_ASCII[j];
}
}
for (int i = 0; i < b_ASCII.Length / 2; i++)
{
b_Hex[i] = (byte)((b_ASCII[2 * i] << 4) | (b_ASCII[2 * i + 1] & 0x0f));
}
}
else//奇数一个数字为一个字节
{
b_Hex = new byte[b_ASCII.Length];
for (int j = 0; j < b_ASCII.Length; j++)
{
if ((b_ASCII[j] & 0xf0) == 0x40 | (b_ASCII[j] & 0xf0) == 0x60)
{
b_Hex[j] = (byte)((b_ASCII[j] & 0x0f) - 0x01 + 0x0a);
}
else if ((b_ASCII[j] & 0xf0) == 0x30)
{
b_Hex[j] = (byte)(b_ASCII[j] & 0x0f);
}
}
}
return b_Hex; }
Application Scenario 3:
Byte / char / int / string type of the received data is displayed on the output window vs
Solution: Using Console.WriteLine () / Write () or Debug.WriteLine () / Write ()
Note:
. 1, .WriteLine ( ) wrap, .Write () does not wrap
2, byte / char / int data type, the default output decimal, hexadecimal display when the press with scene 4
embodiment
string strFilter="00000000";
Console.WriteLine(strFilter);//显示"00000000"
//Debug.WriteLine(strFilter);
Application Scenario 4:
Received byte / char / int array data in the output window vs by hexadecimal display
Solution:
A method (only for byte array data): using BitConverter.ToString (byte array data) function to convert the byte array data directly a hexadecimal string
method 2: first byte / char / int array data using .ToString ( "X2") becomes a function-by-character string obtained by adding another type string, through the Console.WriteLine () / Write ( ) or Debug.WriteLine () / Write () output.
Note:
.. 1, .WriteLine () / the Write () function is not directly output array data, only output a single output data and the default decimal
2, has not yet found the direct function array becomes hexadecimal strings
embodiment
method (applies only to data byte array)
Console.WriteLine(BitConverter.ToString(new byte[] { 1, 13, 54 }).Replace("-", ""));
Method Two
public static string byteToHexStr(byte[] bytes)
{
string returnStr = "";
if (bytes != null)
{
for (int i = 0; i < bytes.Length; i++)
{
returnStr += bytes[i].ToString("X2");
}
//foreach (byte b in bytes)
//{
//Console.WriteLine(b.ToString("X2")+" ");
//}
}
return returnStr;
}
The main function is used: (@@ refers byte, char, int)
Convert.To @@ (), Convert.ToString ()
@@ .TryParse (string type value, out @@ type value); (only the string type value is converted to binary data 16, not converted to hexadecimal array)
String type value .substring ();
Encoding.Default.GetBytes (type value String) / .GetChars (string type value)
String type value .ToCharArray (); (corresponding to only converted to ASCII hexadecimal array data)
Console.WriteLine () / the Write ().
Debug.WriteLine () / the Write ();. (only base 10 @@ output data and the character string, not @@ array data output)
. BitConverter.ToString (new new byte [] {. 1, 13 is, 54 is}) the Replace ( "-", "")
@@ .ToString ();
reference:
multiple output: .NET (C #) the method of the byte array (a scene method 4)
C # 16 and hexadecimal strings, byte array conversion between
C # display output byte data (scene 4 method B) console application
Console.WriteLine () /.Write () output data format
Tostring string formatted output full solution