当前位置:网站首页>C# 40. byte[]与16进制string互转
C# 40. byte[]与16进制string互转
2022-06-26 05:02:00 【lljss2020】
1. 代码
//byte[]转16进制string
//0x13 0xab 0xef -> “13ABEF”
public string BytesToHexString(byte[] bytes,int offset, int length)
{
string hexString = string.Empty;
if((bytes != null) && (length != 0))
{
StringBuilder strB = new StringBuilder();
for(int i=offset;i<length+offset;i++)
{
strB.Append(bytes[i].ToString("X2"));
}
hexString = strB.ToString();
}
return hexString;
}
//16进制string转byte[]
//“13ABEF” -> 0x13 0xab 0xef
public byte[] StringToHexbytes(string hexString)
{
if (hexString == null) return null;
hexString = hexString.Replace(" ","");
byte[] returnBytes = new byte[hexString.Length/2];
for (int i = 0; i < returnBytes.Length; i++)
{
returnBytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2), 16);
}
return returnBytes;
}
2. 测试
byte[] a = new byte[] {
0x13,0xAB,0xEF };
string str = BytesToHexString(a,0,3);
byte[] b = StringToHexbytes(str);

边栏推荐
- Comment enregistrer une image dans une applet Wechat
- 钟珊珊:被爆锤后的工程师会起飞|OneFlow U
- Computer Vision Tools Chain
- 6.1 - 6.2 公鑰密碼學簡介
- One of token passing between microservices @feign's token passing
- 广和通联合安提国际为基于英伟达 Jetson Xavier NX的AI边缘计算平台带来5G R16强大性能
- Computer Vision Tools Chain
- torchvision_ Transform (image enhancement)
- How MySQL deletes all redundant duplicate data
- Essential foundation of programming - Summary of written interview examination sites - computer network (1) overview
猜你喜欢

torchvision_transform(图像增强)
![[H5 development] 03- take you hand in hand to improve H5 development - single submission vs batch submission with a common interface](/img/37/84b7d59818e854dac71d6f06700cde.jpg)
[H5 development] 03- take you hand in hand to improve H5 development - single submission vs batch submission with a common interface

Wechat applet exits the applet (navigator and api--wx.exitminiprogram)

Machine learning final exercises

【Latex】错误类型总结(持更)

Text horizontal alignment attribute text align and element vertical alignment attribute vertical align

Differences between TCP and UDP

UWB超高精度定位系统原理图

UWB超高精度定位系统架构图

MySql如何删除所有多余的重复数据
随机推荐
Text horizontal alignment attribute text align and element vertical alignment attribute vertical align
LeetCode 94. Middle order traversal of binary tree
PSIM software learning ---08 call of C program block
6.1 - 6.2 公鑰密碼學簡介
File upload and security dog
Record a circular reference problem
Floyd
【Unity3D】刚体组件Rigidbody
2.< tag-动态规划和常规问题>lt.343. 整数拆分
2.9 learning summary
Image translation /gan:unsupervised image-to-image translation with self attention networks
Codeforces Round #802 (Div. 2)(A-D)
[H5 development] 03- take you hand in hand to improve H5 development - single submission vs batch submission with a common interface
Use fill and fill in Matplotlib_ Between fill the blank area between functions
Multipass中文文档-设置驱动
1.17 learning summary
[latex] error type summary (hold the change)
Multipass中文文档-远程使用Multipass
torchvision_transform(图像增强)
Some parameter settings and feature graph visualization of yolov5-6.0