Fundamentals of. NET coding

Some basic concepts and analysis of. NET coding

Simple type concept

• Hex (HEX)
• The byte range is 0 ~ 255, and the binary range is 00000000 ~ 11111111, which is equivalent to 1 byte.
• Byte [] byte array
• bit, only 2 states: 0, 1

1 byte equals 8 bits, i.e. 1 byte = 8 bits

In c#. Byte is byte, which is a struct structure,

b1 equals b2

byte b = 1;
byte b1 = 0xf;//Starting with 0x is hexadecimal
byte b2 = 15;
Assert.Equal(b1, b2);

Incorrect syntax

byte b3 = b1 + b2;

Correct grammar

byte b3 = (byte)(b1 + b2);
//or
int b4 = b1 + b2;

Because the mutual operation of byte s is too prone to overflow, the overload of + is the addition of int type, so the result is int type

Encoding.UTF8.GetBytes

UTF8 is a unified coding method, which is a variable length coding method It can use 1 ~ 4 bytes to represent a symbol, and the byte length varies according to different symbols

byte[] plaintext = Encoding.UTF8.GetBytes("0123456789");

What kind of data can be obtained, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57. Why?

Search the ASCII code, we know that the ASCII code of 0 is 48

What if it's a letter.

byte[] plaintext = Encoding.UTF8.GetBytes("ABCDabcd");

What kind of data can be obtained, 65,66,67,68,97,98,99100. Why? Or ASCII. UTF8 is, of course, ASCII compatible.

Because an ASCII code can represent 256 states in one byte, if English letters, Arabic numerals and punctuation are not available, which is Chinese. Here we take UTF8 coding to analyze.

byte[]   plaintext = Encoding.UTF8.GetBytes("China");

As follows, the length of 6228184173 is decimal. What if it turns to hexadecimal. e4, b8, ad, if you can't distinguish hexadecimal, it's recommended to learn the basics of computer system. Of course, to explain, a stands for 10 and e stands for 14 That is, 14 * 16 + 4 = 228 It is not case sensitive, that is, both uppercase A and lowercase a can represent 10

0 228 e4
1 184 b8
3 229 e5
4 155 9b
5 189 bd

We can find the UTF-8 code of China from the Internet: E4B8AD E59BBD Description through encoding UTF8. The result of GetBytes must be right. Chinese is stored in 3 or 4 Bytes.

1.byte [] convert hex (hexadecimal string)

1. Encoding.UTF8.GetBytes can get the decimal byte array of data.
2. BitConverter.ToString supports the conversion of decimal data to hexadecimal. The middle is separated by -, so you need to Replace the middle
public string ByteToHex(string str)
{
byte[] bytes = Encoding.UTF8.GetBytes(str);
string hex = BitConverter.ToString(bytes, 0).Replace("-", string.Empty);
return hex;
}

The data obtained by hex is E4B8ADE59BBD

string hex= ByteToHex("China");

2.byte [] convert hex:StringBuilder mode

Why can the following convert data to hexadecimal

1. Encoding.UTF8.GetBytes can get the decimal byte array of data.
2. Using c# the Composite formatting properties {0:X2} is automatically converted to hexadecimal. The same content supports console Writeline et al
public string ByteToHex2(string str)
{
byte[] bytes = Encoding.UTF8.GetBytes(str);
StringBuilder ret = new StringBuilder();
foreach (byte b in bytes)
{
//{0:x2} lowercase
ret.AppendFormat("{0:X2}", b);
}
return ret.ToString();
}

Hex (hexadecimal string) conversion byte []

public byte[] HexToByte(string hex)
{
byte[] inputByteArray = new byte[hex.Length / 2];
for (var x = 0; x < inputByteArray.Length; x++)
{
var i = Convert.ToInt32(hex.Substring(x * 2, 2), 16);
inputByteArray[x] = (byte)i;
}
return inputByteArray;
}

Call the following contents. The length in bytes [] is 6, and the data are 228184173 and 229155189. That is, the decimal byte array of China above