当前位置:网站首页>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);

边栏推荐
- Machine learning final exercises
- Dbeaver installation and configuration of offline driver
- Genius makers: lone Rangers, technology giants and AI | ten years of the rise of in-depth learning
- Multipass Chinese document - use packer to package multipass image
- LeetCode 94. Middle order traversal of binary tree
- 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)
- torchvision_transform(图像增强)
- dijkstra
- 6.1 - 6.2 Introduction à la cryptographie à clé publique
- A company crawling out of its grave
猜你喜欢

2.22.2.14

Illustration of ONEFLOW's learning rate adjustment strategy

Status of processes and communication between processes

钟珊珊:被爆锤后的工程师会起飞|OneFlow U

Rdkit chemical formula molecular formula search

ModuleNotFoundError: No module named ‘numpy‘

MySql如何删除所有多余的重复数据

Yolov5 super parameter setting and data enhancement analysis

Créateur de génie: cavalier solitaire, magnat de la technologie et ai | dix ans d'apprentissage profond
![[latex] error type summary (hold the change)](/img/3c/bbb7f496c5ea48c6941cd4aceb5065.png)
[latex] error type summary (hold the change)
随机推荐
Status of processes and communication between processes
File upload and security dog
Créateur de génie: cavalier solitaire, magnat de la technologie et ai | dix ans d'apprentissage profond
date_ Range creation date range freq parameter value table and creation example
[IDE(ImageBed)]Picgo+Typora+aliyunOSS部署博客图床(2022.6)
Using requests library and re library to crawl web pages
Numpy data input / output
Simple application of KMP
Sort query
How can the intelligent transformation path of manufacturing enterprises be broken due to talent shortage and high cost?
What is UWB in ultra-high precision positioning system
Large numbers (C language)
Computer Vision Tools Chain
86.(cesium篇)cesium叠加面接收阴影效果(gltf模型)
Multipass Chinese document - use packer to package multipass image
Zuul 实现动态路由
Nabicat连接:本地Mysql&&云服务Mysql以及报错
2022.2.11
PHP之一句话木马
ThreadPoolExecutor implements file uploading and batch inserting data