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

相关推荐
十五年专注C++开发14 分钟前
面试题:C++11在C++98基础上增加了哪些内容?
开发语言·c++·设计模式·面试·stl·适配器模式
yivifu21 分钟前
Cython中操作C++字符串
开发语言·python·c++扩展模块
越城1 小时前
冒泡与 qsort 排序策略集
c语言·开发语言·算法·排序算法
mushangqiujin1 小时前
python爬虫 线程,进程,协程
开发语言·爬虫·python
小王努力学编程1 小时前
贪心算法学习C++
开发语言·c++·学习·算法·leetcode·贪心算法
羽沢312 小时前
js高级_执行上下文和作用域
开发语言·javascript·ecmascript
墨客Y2 小时前
ubtuntu+cmake+c++
开发语言·c++
forestsea2 小时前
Java并发编程面试题:锁(17题)
java·开发语言
寒雪谷2 小时前
用户登陆UI
开发语言·javascript·ui·harmonyos·鸿蒙
努力学习的小廉2 小时前
【C++】 —— 笔试刷题day_17
开发语言·c++