当前位置:网站首页>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,选择图中的程序包后,选择安装即可。
边栏推荐
- 奇怪,为什么ArrayList初始化容量大小为10?
- STM32 inverter power supply design scheme, based on STM32F103 controller [easy to understand]
- 请问有没有人知道clickhouse 中 limit语句执行的逻辑,图片中,上面的SQL可以执行成功
- 好高的佣金,《新程序员》合伙人计划来袭,人人皆可参与!
- SSH服务器拒绝密码,再试一次;PermitRootLogin yes无效问题
- CSDN's one-stop cloud service is open for internal testing, and new and old users are sincerely invited to grab the fresh
- PHP code audit and File Inclusion Vulnerability
- button按钮清除边框
- Meituan P4 carefully collated microservice system architecture design manual to see the world of microservice architecture
- Some tools used in embedded development
猜你喜欢
随机推荐
Scratch big fish eat small fish Electronic Society graphical programming scratch grade examination level 2 true questions and answers analysis June 2022
【leetcode】287. Find duplicates
SQL learning notes (03) -- data constraint relationship
嵌入式开发用到的一些工具
If you meet a female driver and drive didi as an amateur, you can earn 500 a day!
Solution of EPS image blur by latex insertion
线程基础知识
【无标题】
Weidongshan board compilation kernel problem solving
SQL learning notes (01) - basic knowledge of database
SQL学习笔记(01)——数据库基本知识
架构实战营 模块九:设计电商秒杀系统
Tearful eyes, it's not easy to change jobs. Three rounds of interviews, four hours of soul torture
4hutool practice: dateutil format time [easy to understand]
Finally, someone made it clear what DRAM and NAND flash are
This is the best flash popular science article I have ever seen!
[unity rendering] customized screen post-processing
SQL学习笔记(04)——数据更新、查询操作
I like two men...
Computer USB, HDMI, DP various interfaces and speeds