当前位置:网站首页>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,选择图中的程序包后,选择安装即可。

边栏推荐
- How did the data center change from "Britney Spears" to "Mrs. cow"?
- Concept of digital currency
- Solution of EPS image blur by latex insertion
- STM32逆变器电源设计方案,基于STM32F103控制器[通俗易懂]
- 架构实战营 模块九:设计电商秒杀系统
- Voice service notes
- 7-Zip boycotted? The callers have committed "three crimes": pseudo open source, unsafe, and the author is from Russia!
- A quietly rising domestic software, low-key and powerful!
- How Kolo enables NFT music industry
- Who has the vision to cross the cycle?
猜你喜欢

Swag init error: cannot find type definition: response Response

MapReduce programming basics

好高的佣金,《新程序员》合伙人计划来袭,人人皆可参与!

Construction of esp8266 FreeRTOS development environment

Strange, why is the ArrayList initialization capacity size 10?
![Clickhouse: Test on query speed of A-share minute data [Part 2]](/img/c8/42ba748d38546d3b0d2be9b33c5d0b.jpg)
Clickhouse: Test on query speed of A-share minute data [Part 2]

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

TC8:UDP_USER_INTERFACE_01-08

Win11账号被锁定无法登录怎么办?Win11账号被锁定无法登录

Concept of digital currency
随机推荐
Who has the vision to cross the cycle?
全球基金和资管的股票建仓率达到15年内新低
哪个券商公司炒股开户佣金低又安全又可靠
这样理解mmap,挺有意思!
怎么理解JS Promise
树莓派4B系统搭建(超详细版)
持续进阶,软通动力稳步推动云智能战略
关于OpenCV中图像的widthStep
微信表情符号写入判决书,你发的OK、炸弹都可能成为“呈堂证供”
Tearful eyes, it's not easy to change jobs. Three rounds of interviews, four hours of soul torture
SQL学习笔记(04)——数据更新、查询操作
Construction of esp8266 FreeRTOS development environment
The "China Mobile Chain" state secret engine was officially launched on BSN
The market is relatively weak recently
Unity tips for reducing the amount of code -- empty protection extension
苹果放大招!这件事干的太漂亮了……
A 419 error occurred in the laravel postman submission form. July 6th, 2020 diary.
IPv6 learning notes
Voice service notes
Meituan P4 carefully collated microservice system architecture design manual to see the world of microservice architecture