当前位置:网站首页>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);
边栏推荐
- Zuul 實現動態路由
- ModuleNotFoundError: No module named ‘numpy‘
- 5. < tag stack and general problems > supplement: lt.946 Verify the stack sequence (the same as the push in and pop-up sequence of offer 31. stack)
- NVM installation and use and NPM package installation failure record
- Is education important or ability important in software testing
- Wechat applet exits the applet (navigator and api--wx.exitminiprogram)
- 【Unity3D】人机交互Input
- Rsync common error messages (common errors on the window)
- 1.14 learning summary
- 钟珊珊:被爆锤后的工程师会起飞|OneFlow U
猜你喜欢
Multipass Chinese document - remote use of multipass
1.11 learning summary
DBeaver 安装及配置离线驱动
Illustration of ONEFLOW's learning rate adjustment strategy
Zuul 实现动态路由
天才制造者:独行侠、科技巨头和AI|深度学习崛起十年
【Unity3D】碰撞体组件Collider
NVM installation and use and NPM package installation failure record
Pycharm package import error without warning
Genius makers: lone Rangers, technology giants and AI | ten years of the rise of in-depth learning
随机推荐
Multipass Chinese document - use multipass service to authorize the client
Multipass中文文档-设置驱动
YOLOv5-6.0的一些参数设置和特征图可视化
为什么许多shopify独立站卖家都在用聊天机器人?一分钟读懂行业秘密!
Statsmodels Library -- linear regression model
Some parameter settings and feature graph visualization of yolov5-6.0
1.18 learning summary
Computer Vision Tools Chain
Resample
Numpy general function
UWB超高精度定位系统架构图
Sort query
Method of saving pictures in wechat applet
NVM installation and use and NPM package installation failure record
Rdkit chemical formula molecular formula search
2022.2.15
Hash problem
Floyd
Transport layer TCP protocol and UDP protocol
1.14 learning summary