C # characters and 16 binary conversions

Source: Internet
Author: User
Tags array to string

A byte contains 8 bits, and a hexadecimal can represent 4 bits, so a byte can be represented by 2 hexadecimal digits.

Summarized as follows:
(1) A byte corresponds to a two-bit hexadecimal bit instead of a eight-bit (32-bit bits);
(2) After the conversion to hexadecimal, less than two, the high level to fill 0.

8-bit hexadecimal number of bytes such as 0x12345678 How many bytes is this?

A hexadecimal represents a four-bit binary, 0x12345678 converted to binary is 0001-0010-0011-0100-0101-0110-0111-1000 and no eight binary takes one byte, so you have 8 hexadecimal digits of 4 bytes

16 binary, two characters is a byte, one character word, is a byte.

For example: A is a byte, AA is a byte, AAA is two bytes, and AAAA is two bytes.

However, the characters that can appear in the 16 binary (case insensitive) are: 1,2,3,4,5,6,7,8,9,0,a,b,c,d,e,f

6e69 takes Two bytes

1. Transfer between numbers and bytes
int num=12345;byte[] bytes=BitConverter.GetBytes(num);//将int32转换为字节数组num=BitConverter.ToInt32(bytes,0);//将字节数组内容再转成int32类型
    • 1
    • 2
    • 3
2. Convert string to 16 characters, allow Chinese
private string StringToHexString(string s, Encoding encode)        {            byte[] b = encode.GetBytes(s);//按照指定编码将string编程字节数组 string result = string.Empty; for (int i = 0; i < b.Length; i++)//逐字节变为16进制字符 { result += Convert.ToString(b[i], 16); } return result; }
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

Such as:

///注意,一个中文转为utf-8占三个字节,英文占一个字节System.Console.WriteLine(StringToHexString("中华人民共和国", System.Text.Encoding.UTF8));
    • 1
    • 2
    • 3
    • 4

or use

BitConverter.ToString(Encoding.UTF8.GetBytes("中华人民共和国"))
    • 1

Returns the result as Xx-xx-xx

And then remove the "-"

3. Convert 16 binary string to string
private string hexstringtostring (string HS, Encoding encode) {string strtemp=byte[] b=new BYTE[HS. Length/2]; for (int i = 0; i < HS. Length/2; i++) {strtemp = hs. Substring (i * 2, 2); B[i] = Convert.tobyte (strtemp, 16); } //the byte array to string return encode according to the specified encoding. GetString (b); } 
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

Such as:

string hexstring= StringToHexString("中华人民共和国", System.Text.Encoding.UTF8);string content=HexStringToString(hexstring, System.Text.Encoding.UTF8)
    • 1
    • 2
4. Convert byte[] to 16 binary string
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; }
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

ToString("X2")As a string format control in C #

X is hexadecimal
2 is a double digit every time.

such as 0x0A, if not 2, will only output 0xA
Assuming that there are two numbers 10 and 26, the normal hexadecimal display 0xA, 0x1A, so it looks untidy, in order to look good, you can specify "X2", so that the display is: 0x0A, 0x1A

5. Convert the 16 binary string to byte[]
  PrivateStaticbyte[] 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), 16); return returnbytes;}           

C # characters and 16 binary conversions

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.