当前位置:网站首页>C language realizes the conversion between byte stream and hexadecimal string
C language realizes the conversion between byte stream and hexadecimal string
2022-07-27 16:02:00 【xhoufei2010】
Original text :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 ;
}
// Another way to convert byte stream to hexadecimal string
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 ;
}
// Hexadecimal string converted to byte stream
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 ;
} 边栏推荐
- 低代码是开发的未来吗?浅谈低代码平台
- 为应对RISC-V挑战?Arm CPU引入自定义指令功能!
- Stock account opening commission discount, stock trading account opening which securities company is good, is online account opening safe
- Interview focus - TCP protocol of transport layer
- 判断数据的精确类型
- CAS compares the knowledge exchanged, ABA problems, and the process of lock upgrading
- Three uses of static keyword
- 网络设备硬核技术内幕 路由器篇 小结(下)
- 网络层的IP协议
- Understand │ what is cross domain? How to solve cross domain problems?
猜你喜欢

Half find

Division of entity classes (VO, do, dto)

MySQL表数据的增删查改

Interview focus - TCP protocol of transport layer

Openwrt增加对 sd card 支持

C language: custom type

单机高并发模型设计
![[sword finger offer] interview question 49: ugly number](/img/7a/2bc9306578530fbb5ac3b32254676d.png)
[sword finger offer] interview question 49: ugly number

Understand │ what is cross domain? How to solve cross domain problems?

网络原理(2)——网络开发
随机推荐
Is the array name the address of the first element?
QT (VI) value and string conversion
MySQL表数据的增删查改
Flask连接mysql数据库已有表
减小程序rom ram,gcc -ffunction-sections -fdata-sections -Wl,–gc-sections 参数详解
DRF学习笔记(四):DRF视图
leetcode25题:K 个一组翻转链表——链表困难题目详解
台积电6纳米制程将于明年一季度进入试产
busybox login: can't execute '/bin/bash': No such file or directory 解决方法
Pycharm导入已有的Project
[sword finger offer] interview question 49: ugly number
juc包下常用工具类
线程间等待与唤醒机制、单例模式、阻塞队列、定时器
leetcode234题-简单方法判断回文链表
profileapi.h header
js操作dom节点
Hj8 consolidated statement record
Implementation of spark lazy list files
Binder初始化过程
DRF学习笔记(三):模型类序列化器ModelSerializer