.Net(C#)常用转换byte转uint32、byte转float等

1、byte转String

Encoding.ASCII.GetString(byte[]);

2、base64string转byte

byte[]=Base64Decoder.Decoder.GetDecoded(string);

3、byte转UInt16

方法一

(UInt16)(bytes[0] * 256 + bytes[1])

方法二

(UInt16)((bytes[0] << 8) | bytes[1]);

方法三

字节序要对应上,下位机一般高字节在前,C#这个函数是低字节在前

BitConverter.ToInt16(bytes);

4、byte转UInt32

(UInt32)((bytes[0] << 24) | (bytes[1] << 16) | (bytes[2] << 8) | bytes[3]);

5、byte转Int32

(Int32)((bytes[0] << 24) | (bytes[1] << 16) | (bytes[2] << 8) | bytes[3]);

6、byte转float

BitConverter.ToSingle(bytes, 0);

7、byte转char

BitConverter.ToSingle(bytes, 0);

相关推荐
Wenweno0o11 小时前
0基础Go语言Eino框架智能体实战-chatModel
开发语言·后端·golang
chenjingming66612 小时前
jmeter线程组设置以及串行和并行设置
java·开发语言·jmeter
cch891812 小时前
Python主流框架全解析
开发语言·python
不爱吃炸鸡柳12 小时前
C++ STL list 超详细解析:从接口使用到模拟实现
开发语言·c++·list
十五年专注C++开发12 小时前
RTTR: 一款MIT 协议开源的 C++ 运行时反射库
开发语言·c++·反射
Momentary_SixthSense12 小时前
设计模式之工厂模式
java·开发语言·设计模式
‎ദ്ദിᵔ.˛.ᵔ₎12 小时前
STL 栈 队列
开发语言·c++
勿忘,瞬间12 小时前
数据结构—顺序表
java·开发语言
张張40812 小时前
(域格)环境搭建和编译
c语言·开发语言·python·ai
weixin_4235339912 小时前
【Windows11离线安装anaconda、python、vscode】
开发语言·vscode·python