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

边栏推荐
- 好高的佣金,《新程序员》合伙人计划来袭,人人皆可参与!
- Hololens2 development -6-eyetracking and speech recognition
- 关于OpenCV中图像的widthStep
- PHP code audit and File Inclusion Vulnerability
- 树莓派4B系统搭建(超详细版)
- 121. thread scheduling: join() method and yield() method
- 华为帐号多端协同,打造美好互联生活
- SQL学习笔记(01)——数据库基本知识
- HMS core audio editing service 3D audio technology helps create an immersive auditory feast
- 日本教授起诉英特尔FPGA与SoC产品侵犯一项设计专利
猜你喜欢

遇到女司机业余开滴滴,日入500!

I like two men...

Introduction to expressions and operators in C language

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

谁还在买“三只松鼠”们
![[fxcg] large scale job hopping may be one of the driving forces behind the soaring inflation in the United States](/img/f6/be4e1affb9568acdedb94117b9b037.jpg)
[fxcg] large scale job hopping may be one of the driving forces behind the soaring inflation in the United States

HMS Core音频编辑服务3D音频技术,助力打造沉浸式听觉盛宴

云原生到底是什么?它会是未来发展的趋势吗?

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

Scratch big fish eat small fish Electronic Society graphical programming scratch grade examination level 2 true questions and answers analysis June 2022
随机推荐
项目必用的全局异常处理器,你学会了吗
Some tools used in embedded development
怎么理解JS Promise
About widthstep of images in opencv
How to understand JS promise
日本教授起诉英特尔FPGA与SoC产品侵犯一项设计专利
Flinkv1.13实现金融反诈骗案例
Eat a rich woman's melon...
7-Zip 遭抵制?呼吁者定下“三宗罪”:伪开源、不安全、作者来自俄罗斯!
Huawei accounts work together at multiple ends to create a better internet life
Win11账号被锁定无法登录怎么办?Win11账号被锁定无法登录
Centos 配置discuz 提示请检查 mysql 模块是否正确加载
PO模式深入封装
Thread Basics
奇怪,为什么ArrayList初始化容量大小为10?
线程基础知识
How Kolo enables NFT music industry
Swag init error: cannot find type definition: response Response
嵌入式开发用到的一些工具
年薪100万,在北上广深买的起房子吗?