字节二进制字符串C# - 显示所有8位字符串、字节

2023-09-02 10:40:50 作者:丢了薇笑″嗱什麼来伪装

我要显示在文本框中一个字节。 现在,我使用的:

  Convert.ToString(MyVeryOwnByte,2);
 

但是,当字节具有0的在开始时的0被cuted。 例如:

  MyVeryOwnByte = 00001110 // Texbox显示 - > 1110
MyVeryOwnByte = 01010101 // Texbox显示 - > 1010101
MyVeryOwnByte = 00000000 // Texbox显示 - > <空>
MyVeryOwnByte = 00000001 // Texbox显示 - > 1
 

我要显示所有8位。

解决方案

  Convert.ToString(MyVeryOwnByte,2).PadLeft(8'0');
 
1MB等于多少字节 为什么

此将填充空白区域的左边用0,共8个字符的字符串中

I want to display one byte in textbox. Now I'm using:

Convert.ToString(MyVeryOwnByte, 2);

But when byte is has 0's at begining those 0's are being cuted. Example:

MyVeryOwnByte = 00001110 // Texbox shows -> 1110
MyVeryOwnByte = 01010101 // Texbox shows -> 1010101
MyVeryOwnByte = 00000000 // Texbox shows -> <Empty>
MyVeryOwnByte = 00000001 // Texbox shows -> 1

I want to display all 8 digits.

解决方案

Convert.ToString(MyVeryOwnByte, 2).PadLeft(8, '0');

This will fill the empty space to the left with '0' for a total of 8 characters in the string

 
精彩推荐
图片推荐