c# - int[] 到 byte[] 的转换,1 个 int 对应 1 个字节
问题描述
我正在使用Stream.Write(byte[],int int)
函数写入流,但我无法转换我的int[] to byte[]
,在我的整数数组中我有 56 个整数,它们都低于 8 位值。我想将我的 int [] byte[] 转换为我的 byte[] 也有 56 个字节。
像这样
int[]= {0x0004,0x0001,0x0003,0x0003}
字节[] ={ 0x04,0x01,0x03,0x03}
提前致谢
解决方案
您必须从每个 int 中提取低字节:myByte = (byte)(myInt & 0xFF)
using System.Linq;
//...
int[] integers = {0x0004,0x0001,0x0003,0x0003};
byte[] bytes = integers.Select(n => (byte)(n & 0xFF)).ToArray();
推荐阅读
- firebase - 未创建任何 Firebase 应用“[DEFAULT]”- 调用 Firebase App.initializeApp()
- c# - 极速查询更新mysql
- python - 在 pygame 中显示文本时遇到问题
- php - 一种从 Laravel 中的控制器调用命令提示符功能的方法?
- delphi-xe7 - 我应该如何将 char dbcc_name[1] 翻译成 Delphi?
- python - Pytest Django 函数模拟 APITestCase
- bioinformatics - 将杂原子添加到 pdb 文件
- python - 如何创建根据组大小排序的多索引数据框?
- jquery - jquery对象的每次迭代
- f# - 将嵌套匹配表达式转换为函数