Character and 16 conversion (II)

Source: Internet
Author: User
socket send hexadecimal string (hexadecimal string with other types of conversion)

C # Socket send hexadecimal string (hexadecimal string and other type of conversion) collection
The socket sends a hexadecimal string

The project uses the socket TCP/IP to send/accept hexadecimal strings because the C # socket sends the accepted byte[] (byte array) and now records the conversion between the tyte[and the data types

Note:

C # 's byte array byte[] is stored as integer data of 0-255
The byte keyword represents an integral type that stores values as shown in the following table:

You can declare and initialize a variable of type byte as shown in the following example:
byte MyByte = 255;
In the above declaration, integer 255 is implicitly converted from int to byte. If the integer is out of Byte range, a compilation error is generated.

Introduction:

1. String byte[]/byte[] Spin string

2.16 binary string byte[]/byte[] turn hexadecimal string

3.16 binary string and numeric type conversion

4. Chinese character to hexadecimal string

1. String byte[]/byte[] Spin string

This is often used, C # is also relatively simple to use

Encoding.UTF8.GetBytes (string data);

Encoding.UTF8.GetString (tyte[] data, 0, length);

Note that the different coding, the length of the data and even the content of the impact (generally send and receive both use the same code)

2.16 binary string byte[]/byte[] turn hexadecimal string

hexadecimal string byte[] uses the Convert.tobyte () method; byte[] The hexadecimal string to use bitconverter.tostring () or bytes[i]. ToString ("X2").

View Plaincopy to Clipboardprint?
<summary>
16 binary string byte array
</summary>
<param name= "HexString" ></param>
<returns></returns>
private static byte[] Strtotohexbyte (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). Trim (), 16);
return returnbytes;
}

<summary>
Byte array 16 Feed string
</summary>
<param name= "bytes" ></param>
<returns></returns>
public static string bytetohexstr (byte[] bytes)
{
String returnstr = "";
if (bytes!= null)
{
for (int i = 0; i < bytes. Length; i++)
{
Returnstr + = Bytes[i]. ToString ("X2");
}
}
return returnstr;
}
<summary>
16 binary string byte array
</summary>
<param name= "HexString" ></param>
<returns></returns>
private static byte[] Strtotohexbyte (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). Trim (), 16);
return returnbytes;
}

<summary>
Byte array 16 Feed string
</summary>
<param name= "bytes" ></param>
<returns></returns>
public static string bytetohexstr (byte[] bytes)
{
String returnstr = "";
if (bytes!= null)
{
for (int i = 0; i < bytes. Length; i++)
{
Returnstr + = Bytes[i]. ToString ("X2");
}
}
return returnstr;
}

In addition, the simpler

Byte[] A hexadecimal string can also take advantage of the Bitconverter.tostring method, which is easier to use.

String str0x = Bitconverter.tostring (byte[] tyt,0,length);

The returned string is a hexadecimal string with a "-" separator that can be removed:

String str0x = Bitconverter.tostring (byte[] tyt,0,recv). Replace ("-", "");

The Bitconverter class provides methods for converting underlying data types to byte arrays.

3.16 binary string and numeric type conversion

The main classes used for converting between hexadecimal strings and numeric types are Bitconverter, Int32, String, convert

Int32.Parse (), uint. Parse (),

Bitconverter.getbytes (), Bitconverter.tosingle (),

String.Format ()

Convert.ToInt32 (), Convert.tobyte () and other methods

Specific reference to the blog

4. Chinese character to hexadecimal string

View Plaincopy to Clipboardprint?
<summary>
Converting from Chinese to 16
</summary>
<param name= "S" ></param>
<param name= "CharSet" > Code, such as "Utf-8", "gb2312" </param>
<param name= "Fenge" > whether each character used comma delimited </param>
<returns></returns>
public static string Tohex (string s, String charset, bool Fenge)
{
if ((s.length% 2)!= 0)
{
s + = "";//Space
throw new ArgumentException ("s is not valid Chinese string!");
}
System.Text.Encoding CHS = System.Text.Encoding.GetEncoding (charset);
byte[] bytes = chs. GetBytes (s);
String str = "";
for (int i = 0; i < bytes. Length; i++)
{
str = string. Format ("{0:x}", Bytes[i]);
if (Fenge && (i!= bytes). LENGTH-1))
{
str = string. Format ("{0}", ",");
}
}
Return str. ToLower ();
}

<summary>
Convert from 16 to Chinese characters
</summary>
<param name= "Hex" ></param>
<param name= "CharSet" > Code, such as "Utf-8", "gb2312" </param>
<returns></returns>
public static string Unhex (string hex, String charset)
{
if (hex = null)
throw new ArgumentNullException ("hex");
Hex = hex. Replace (",", "");
Hex = hex. Replace ("n", "");
Hex = hex. Replace ("//", "");
Hex = hex. Replace ("", "");
if (hex. Length% 2!= 0)
{
Hex + = "20";//Space
}
You need to convert hex to a byte array.
byte[] bytes = new Byte[hex. LENGTH/2];

for (int i = 0; i < bytes. Length; i++)
{
Try
{
Every two characters is a byte.
Bytes[i] = byte. Parse (Hex. Substring (i * 2, 2),
System.Globalization.NumberStyles.HexNumber);
}
Catch
{
Rethrow an exception with custom message.
throw new ArgumentException ("hex is not a valid hex number!", "hex");
}
}
System.Text.Encoding CHS = System.Text.Encoding.GetEncoding (charset);
Return CHS. GetString (bytes);
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.