简体   繁体   English

使用C#逐字节读取字节数组

[英]Read byte array byte by byte using C#

I have a byte array and I want to read this array byte by byte and displayed each byte as integer. 我有一个字节数组,我想逐字节读取此数组,并将每个字节显示为整数。

how to do this using C#? 如何使用C#做到这一点?

Given that the array is called bytes : 假设该数组称为bytes

foreach(var b in bytes)
{
    Console.WriteLine((int)b);
}

Though, in all fairness, the cast to int is probably unnecessary for display purposes. 虽然,公平地说,出于显示目的,强制转换为int可能是不必要的。

Some of the C# code I have been writing communicates via TCP/IP with legacy C++ applications. 我正在编写的某些C#代码通过TCP / IP与旧版C ++应用程序进行通信。 Some codes use a raw packet format where C/C++ structures are passed back and forward. 某些代码使用原始数据包格式,其中C / C ++结构被前后传递。

Example of what the legacy code could look like: Best Practice Mapping a byte array to a structure in C# 遗留代码的示例如下: 最佳实践在C#中将字节数组映射到结构

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM