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

相关推荐
HelloRevit7 分钟前
Windows Server SMB 共享文件 回收站
windows·c#
写代码的【黑咖啡】12 分钟前
Python 中的 Requests 库:轻松进行 HTTP 请求
开发语言·python·http
BD_Marathon13 分钟前
MyBatis各种查询功能
java·开发语言·mybatis
研☆香14 分钟前
JavaScript 特点介绍
开发语言·javascript·ecmascript
Howrun77717 分钟前
虚幻引擎_AController_APlayerController_AAIController
开发语言·c++·游戏引擎·虚幻
曹牧24 分钟前
C#:ToDouble
开发语言·c#
袁袁袁袁满26 分钟前
Python读取doc文件打印内容
开发语言·python·python读取doc文件
zcfeng53039 分钟前
PHP升级
开发语言·php
m0_7482523841 分钟前
Ruby 模块(Module)的基本概念
开发语言·python·ruby
羊小猪~~44 分钟前
【QT】-- QT基础类
开发语言·c++·后端·stm32·单片机·qt