当前位置:网站首页>C# [字节数组]与[16进制字符串]互相转换 - CodePlus系列
C# [字节数组]与[16进制字符串]互相转换 - CodePlus系列
2022-07-01 09:51:00 【猿长大人】
C# [字节数组]与[16进制字符串]互相转换
前言
十六进制(简写为hex或下标16)是一种基数为16的计数系统,是一种逢16进1的进位制。通常用数字0、1、2、3、4、5、6、7、8、9和字母A、B、C、D、E、F(a、b、c、d、e、f)表示,其中:AF表示1015,这些称作十六进制数字。
我们在做开发的过程中,经常需要将收发数据打印出来检查。如何简单高效的一行代码转换字节数组到字符串呢?我们来一起看看吧!
示例代码
[字节数组] 转 [16进制字符串]
byte[] bytes = new byte[] {
0xAA, 0xBB, 0xCC, 0x01, 0x02, 0x03 };
Console.WriteLine($"十六进制字符串(默认):\n{
bytes.ToHexString()}");
Console.WriteLine($"十六进制字符串(大写加空格):\n{
bytes.ToHexString("{0:X2} ")}");
Console.WriteLine($"十六进制字符串(小写加空格):\n{
bytes.ToHexString("{0:x2} ")}");
Console.WriteLine($"十六进制代码字符串:\n{
bytes.ToHexCodeString()}");
执行结果如下图:
[16进制字符串] 转 [字节数组]
string hex = "AABBCC010203";
var bytes = hex.HexStringToBytes();
引入CodePlus
转换如此简单主要是因为引用的CodePlus提供的扩展方法。
对着项目右键,选择管理NuGet程序包
在搜索栏中输入CodePlus,选择图中的程序包后,选择安装即可。

边栏推荐
- 日本教授起诉英特尔FPGA与SoC产品侵犯一项设计专利
- The latest masterpiece of Alibaba, which took 182 days to produce 1015 pages of distributed full stack manual, is so delicious
- Swift control encapsulation - paging controller
- Dotnet console uses microsoft Maui. Getting started with graphics and skia
- How to understand JS promise
- Get the list of a column in phpexcel get the letters of a column
- Meituan P4 carefully collated microservice system architecture design manual to see the world of microservice architecture
- Introduction to expressions and operators in C language
- 请问有没有人知道clickhouse 中 limit语句执行的逻辑,图片中,上面的SQL可以执行成功
- Comparison between Oracle JDK and openjdk
猜你喜欢

I like two men...

持续进阶,软通动力稳步推动云智能战略

项目必用的全局异常处理器,你学会了吗

PHP code audit and File Inclusion Vulnerability

架构实战营 模块九:设计电商秒杀系统

IPv6 learning notes

A quietly rising domestic software, low-key and powerful!

Apple amplification! It's done so well

日本教授起诉英特尔FPGA与SoC产品侵犯一项设计专利

Finally, someone made it clear what DRAM and NAND flash are
随机推荐
Live broadcast management project
日本教授起诉英特尔FPGA与SoC产品侵犯一项设计专利
BSN long story 10: how to ensure the safety of NFT
CSDN's one-stop cloud service is open for internal testing, and new and old users are sincerely invited to grab the fresh
Voice service notes
Cortex M4 systick details
那个程序员,被打了。
Network partition notes
TC8:UDP_USER_INTERFACE_01-08
Ssh server rejects password, try again; Permitrootlogin yes invalid problem
Problems caused by delete and delete[]
年薪100万,在北上广深买的起房子吗?
SQL statement modify field type "suggestions collection"
Get the list of a column in phpexcel get the letters of a column
Configure load balancing
SQL学习笔记(02)——数据库表操作
睡了二哥。。。
mysql截取_mysql截取字符串的方法[通俗易懂]
Eat a rich woman's melon...
Ubuntu system installation and MySQL configuration