C#问题的byte [] [英] C# problem with byte[]

查看:109
本文介绍了C#问题的byte []的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我把文件加载到一个byte []。按照我的理解字节[]应该包含字节(8位)的具体内容。当我打印的每个字节,个个都不是8位(即他们没有的8的长度)。
我的代码:

I am loading a file into a byte[]. By my understanding the byte[] should contain a specific elements of bytes (8-bit). When i print each byte, all of them are not 8-bit (i.e. they dont have the length of 8). My Code:

FileStream stream = File.OpenRead(@"C:\Image\Img.jpg");
byte[] fileByte = new byte[stream.Length];
stream.Read(fileByte, 0, fileByte.Length);

for (int i = 0; i <= fileByte.Length - 1; i++)
{
  Console.WriteLine(Convert.ToString(fileByte[i], 2));
}



输出:

Output:

10001110
11101011
10001100
1000111
10011010
10010011
1001010
11000000
1001001
100100

我想我的理解是错在这里,你能请让我知道(或者给我提供了一些教程链接),我很想念这个。

I think my understanding is wrong here, Can you please let me know (or provide me some tutorial links) where I am missing this.

推荐答案

在一个数字转换成字符串,你失去任何领先零。 (请注意,所有的条目开始用1。)您可以使用PadLeft将它们放回。

When converting a numeric to a string, you lose any leading zeros. (Note that all of your entries start with "1".) You can use PadLeft to put them back in.

   FileStream stream = File.OpenRead(@"C:\Image\Img.jpg"); 
   byte[] fileByte = new byte[stream.Length]; 
   stream.Read(fileByte, 0, fileByte.Length); 

   for (int i = 0; i <= fileByte.Length - 1; i++) 
   { 
      Console.WriteLine(Convert.ToString(fileByte[i], 2).PadLeft(8,'0')); 

   }

这篇关于C#问题的byte []的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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