当前位置:网站首页>C语言实现字节流与十六进制字符串的相互转换
C语言实现字节流与十六进制字符串的相互转换
2022-07-27 14:37:00 【xhoufei2010】
原文出自:http://blog.csdn.net/qq387732471/article/details/7360988
void ByteToHexStr(const unsigned char* source, char* dest, int sourceLen)
{
short i;
unsigned char highByte, lowByte;
for (i = 0; i < sourceLen; i++)
{
highByte = source[i] >> 4;
lowByte = source[i] & 0x0f ;
highByte += 0x30;
if (highByte > 0x39)
dest[i * 2] = highByte + 0x07;
else
dest[i * 2] = highByte;
lowByte += 0x30;
if (lowByte > 0x39)
dest[i * 2 + 1] = lowByte + 0x07;
else
dest[i * 2 + 1] = lowByte;
}
return ;
}
//字节流转换为十六进制字符串的另一种实现方式
void Hex2Str( const char *sSrc, char *sDest, int nSrcLen )
{
int i;
char szTmp[3];
for( i = 0; i < nSrcLen; i++ )
{
sprintf( szTmp, "%02X", (unsigned char) sSrc[i] );
memcpy( &sDest[i * 2], szTmp, 2 );
}
return ;
}
//十六进制字符串转换为字节流
void HexStrToByte(const char* source, unsigned char* dest, int sourceLen)
{
short i;
unsigned char highByte, lowByte;
for (i = 0; i < sourceLen; i += 2)
{
highByte = toupper(source[i]);
lowByte = toupper(source[i + 1]);
if (highByte > 0x39)
highByte -= 0x37;
else
highByte -= 0x30;
if (lowByte > 0x39)
lowByte -= 0x37;
else
lowByte -= 0x30;
dest[i / 2] = (highByte << 4) | lowByte;
}
return ;
} 边栏推荐
- 聊聊ThreadLocal
- Extended log4j supports the automatic deletion of log files according to time division and expired files
- Summer Challenge harmonyos realizes a hand-painted board
- 【剑指offer】面试题42:连续子数组的最大和——附0x80000000与INT_MIN
- NPM install error unable to access
- 网络设备硬核技术内幕 路由器篇 22
- 突发!海康/大华/商汤/旷视/依图/科大讯飞等28家中国实体被美列入黑名单
- C language: function stack frame
- Voice live broadcast system -- a necessary means to improve the security of cloud storage
- $router.back(-1)
猜你喜欢

C language: minesweeping games

Using Lombok results in the absence of parent class attributes in the printed toString

Spark 3.0 adaptive execution code implementation and data skew optimization

线程中死锁的成因及解决方案

Push down of spark filter operator on parquet file

C language: dynamic memory function

网络原理(2)——网络开发

C语言:函数栈帧
![[sword finger offer] interview question 45: arrange the array into the smallest number](/img/d1/e2e21c95ecf5b8e3854f134cc47228.png)
[sword finger offer] interview question 45: arrange the array into the smallest number

C语言:字符串函数与内存函数
随机推荐
Network principle (1) - overview of basic principles
C language: dynamic memory function
Division of entity classes (VO, do, dto)
【剑指offer】面试题39:数组中出现次数超过一半的数字
[regular expression] match the beginning and end
go语言慢速入门——包
What format is this data returned from the background
C语言:动态内存函数
网络设备硬核技术内幕 路由器篇 22
C language: string function and memory function
MySQL表数据的增删查改
Fluent -- layout principle and constraints
传音控股披露被华为起诉一事:已立案,涉案金额2000万元
C language: function stack frame
网络原理(1)——基础原理概述
股票开户佣金优惠,炒股开户哪家证券公司好网上开户安全吗
go语言慢速入门——go运算符
聊聊面试必问的索引
Half find
Talk about ThreadLocal