.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)(bytes0 * 256 + bytes1)

方法二

(UInt16)((bytes0 << 8) | bytes1);

方法三

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

BitConverter.ToInt16(bytes);

4、byte转UInt32

(UInt32)((bytes0 << 24) | (bytes1 << 16) | (bytes2 << 8) | bytes3);

5、byte转Int32

(Int32)((bytes0 << 24) | (bytes1 << 16) | (bytes2 << 8) | bytes3);

6、byte转float

BitConverter.ToSingle(bytes, 0);

7、byte转char

BitConverter.ToSingle(bytes, 0);

相关推荐
李妍.7 小时前
02Numpy基础(上)
开发语言·python
TheBestRucy7 小时前
Python 九阳神功之贰:面向对象(下)
开发语言·python
AI_小站8 小时前
刚面完百度的 Agent 开发岗,我才发现:世界就是个巨大的草台班子
java·开发语言·人工智能·spring·百度·langchain
felixking9 小时前
C++20 协程
开发语言·c++·协程
Zane19941210 小时前
CAS 与原子类:Java 如何实现无锁编程
java·开发语言
码农颜11 小时前
6.3 主函数流程剖析
开发语言·php
TheBestRucy11 小时前
Python 九阳神功:从零筑基到线程飞升
服务器·开发语言·网络·人工智能·python
研☆香11 小时前
聊一聊前端的常见字 关键字
开发语言·前端·javascript
草莓橙子碗12 小时前
Claude 使用指南
开发语言·python
longxiaozhang613 小时前
C# Array类:数组其实没那么难
开发语言·c#