在c#转换为字节的二进制字符串 [英] Converting bytes to a binary string in c#

查看:1958
本文介绍了在c#转换为字节的二进制字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C#中,我转换一个字节二进制,实际的答案是 00111111 ,但报错结果是 111111 。现在,我真的需要在前面,甚至显示2 0。谁能告诉我如何做到这一点?

In c# I am converting a byte to binary, the actual answer is 00111111 but the result being given is 111111. Now I really need to display even the 2 0s in front. Can anyone tell me how to do this?

我使用的:

Convert.ToString(byteArray[20],2)

和字节值63

推荐答案

只要改变你的code为:

Just change your code to:

string yourByteString = Convert.ToString(byteArray[20], 2).PadLeft(8, '0');
// produces "00111111"

这篇关于在c#转换为字节的二进制字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆