.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);

相关推荐
froginwe112 分钟前
Rust 文件与 IO
开发语言
liulilittle4 分钟前
LIBTCPIP 技术探秘(tun2sys-socket)
开发语言·网络·c++·信息与通信·通信·tun
yyy(十一月限定版)5 分钟前
c++(3)类和对象(中)
java·开发语言·c++
落羽凉笙8 分钟前
Python基础(4)| 玩转循环结构:for、while与嵌套循环全解析(附源码)
android·开发语言·python
ytttr87310 分钟前
MATLAB的流体动力学与热传导模拟仿真实现
开发语言·matlab
码农学院12 分钟前
使用腾讯翻译文本
服务器·数据库·c#
山上三树13 分钟前
详细介绍 C 语言中的 #define 宏定义
c语言·开发语言·算法
测试游记21 分钟前
基于 FastGPT 的 LangChain.js + RAG 系统实现
开发语言·前端·javascript·langchain·ecmascript
小罗和阿泽25 分钟前
java 【多线程基础 三】
java·开发语言
ulias21225 分钟前
AVL树的实现
开发语言·数据结构·c++·windows