How does Unity convert values between binary, octal, decimal, and hexadecimal in the code, and output strings in a specific form

1. To output the value in a special format, such as percentage, currency, scientific notation, placeholder, etc., usually use "String.Format("....", param)". Note: The parameter "param" usually uses a decimal value

 As shown above, the placeholder is usually used in countdown format output, such as "00:00:00", you can use "string.Format("{0:00}", hour)" or "string .Format("{0:D2}", hour)" . When the number of digits of the value exceeds the number of target placeholders, no processing will be done on the value, and when the number of placeholders is insufficient, it will be filled with 0 without affecting the size of the value:

string.Format("{0:000}", 10);      //输出结果:010
string.Format("{0:D5}", 10);       //输出结果:00010
             // 以十进制的形式输出,并且一共占5位

2. Convert values ​​between binary, octal, decimal, and hexadecimal:

Method 1 : Use the string.Format format above, but this method is usually limited to the conversion between decimal and hexadecimal

As can be seen from the figure above, since the numeric parameters of string.Format are usually in decimal, they are mainly used in hexadecimal conversion. For this simple conversion you can also directly use a.ToString("X"), a.ToString("D")

Method 2 : Use Convert.ToString to convert values ​​between binary, octal, decimal, and hexadecimal

Notice:

1. In the absence of special symbols, the value is usually in decimal , such as "10000" above, and only when a specific number system prefix is ​​added before the value can it indicate the corresponding base of the value, such as "0x10000" means the Value is in hexadecimal

2. The prefix "0x" of the hexadecimal value "0x10000" is the number 0 and the letter x, and the letter x is not case-sensitive, that is, 0x and 0X are equivalent

3. You can use decimal or hexadecimal when assigning a value to a parameter. The int type defaults to a 32-bit decimal value

int c = 0x100;
Debug.Log("数值是:" + c);    

 Output result:

4. The results obtained by using Convert.ToString are all string types, not int types. Therefore, if you want to use this value for subsequent calculations, you need to convert it to decimal, using Convert.ToInt32

Debug.Log(Convert.ToInt32("10000", 2));    //二进制的“10000”,输出结果:16
Debug.Log(Convert.ToInt32("10000", 8));    //八进制的“10000”,输出结果:4096
Debug.Log(Convert.ToInt32("10000", 10));   //十进制的“10000”,输出结果:10000
Debug.Log(Convert.ToInt32("10000", 16));   //十六进制的“10000”,输出结果:65536

3. The difference between Convert.ToString and Convert.ToInt32:

Convert.ToString( int value, int toBase ): It is usually used to convert decimal or hexadecimal integers into different target bases, but the result is string, and the numeric type cannot be obtained. According to the difference of toBase, the string result of the corresponding base is obtained

Convert.ToInt32( string value, int fromBase ): The returned result must be a decimal int type . Special attention is required to use the parameter "fromBase" to clearly mark the hexadecimal type of the string parameter "value", otherwise it cannot be converted . —— When the value is an int type, the fromBase parameter is not required.

PS: int.Parse(string value) —— the return result is usually a decimal integer type, similar to Convert.ToInt32, and another parameter is also required to mark the decimal type of "value":

Debug.Log(int.Parse("10000", NumberStyles.HexNumber));  //输出结果:65536

Unfortunately, this method does not have special parameters for binary, octal, and decimal. Currently, there is only hexadecimal "HexNumber" .

Notice:

1. In general, the second parameter "HexNumber" is not used, and the first parameter defaults to a value in decimal string form

2. When the second parameter is "HexNumber" hexadecimal, the first parameter cannot use "0x10000", otherwise an error will be reported; it has no effect on Convert.ToInt32

Debug.Log(Convert.ToInt32("10000", 16));                 //输出结果:65536
Debug.Log(Convert.ToInt32("0x10000", 16));               //输出结果:65536
Debug.Log(int.Parse("10000", NumberStyles.HexNumber));    //输出结果:65536
Debug.Log(int.Parse("0x10000", NumberStyles.HexNumber));  //直接报错,无法输出

 

Guess you like

Origin blog.csdn.net/m0_47975736/article/details/123072444