将8位字节转换为16位字节 [英] Converting 8 bit bytes to 16 bit byte

查看:1838
本文介绍了将8位字节转换为16位字节的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

i have a byte array.. Each index is having values like 0x01, 0x1D etc.. It can be seen that these values are 8 bit numbers. I want to convert them to 16 bit numbers.  for example, I want this type of result 0x0001 0x001D.  Is there some way to do this in C#?





我的尝试:



将使用BitConverter类。我想得到无符号数字。我不确定BitConverter Class将使用哪种方法。



What I have tried:

BitConverter class will be used. i want to get unsigned numbers. i am not sure which method will be used of BitConverter Class.

推荐答案

解决方案1中没有现有方法。



但声明一个 ushort 数组非常简单,其数量与字节数组相同,并在循环中复制数据:

There is no existing method as already noted in solution 1.

But it is quite simple to declare an array of ushort with the same number of items as in the byte array and copy the data within a loop:
ushort[] arr16 = new ushort[arr8.Length];
for (int i = 0; i < arr8.Length; i++)
{
    arr16[i] = arr8[i];
}


BitConverter类(系统) [ ^ ]似乎没有办法为您执行此操作。你需要自己编写。
The BitConverter Class (System)[^] does not appear to have a method to do this for you. You will need to write your own.


这篇关于将8位字节转换为16位字节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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