On the conversion between binary, decimal, hexadecimal and string

1. Byte to decimal

Use (int) type conversion directly.

2. Hexadecimal to byte

Use (byte) type conversion directly.

3. Byte array to hexadecimal string

For each byte, sum with 0xff first, and then use integer For the tohexstring() function, if the result has only 1 bit, it needs to be preceded by 0.

4. Hexadecimal string to byte array

This is complicated. Each hexadecimal character is 4bit and one byte is 8bit, so the two hexadecimal characters are converted into one byte. For the first character, it is converted into byte and then shifted to the left by 4 bits, and then it performs or operation with the byte of the second character, so as to convert the two characters into one byte.

5. Byte array to string

Use new string() directly.

6. String to byte array

Use GetBytes () directly.

7. Hexadecimal string to string

Convert to byte [], and then to string.

8. Convert string to hexadecimal string

First convert to byte [], and then convert to hexadecimal string.

Main function:

Operation results:

The above discussion on the conversion between binary, decimal, hexadecimal and string is all the content shared by Xiaobian. I hope it can give you a reference and support more programming skills.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>