当前位置:网站首页>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 ;
} 边栏推荐
- go语言慢速入门——基本内置类型
- openwrt 增加RTC(MCP7940 I2C总线)驱动详解
- 台积电6纳米制程将于明年一季度进入试产
- 文字批量替换功能
- Is low code the future of development? On low code platform
- [Yunxiang book club issue 13] common methods of viewing media information and processing audio and video files in ffmpeg
- Talk about ThreadLocal
- juc包下常用工具类
- 【云享读书会第13期】视频文件的编码格式
- C language: function stack frame
猜你喜欢
![[sword finger offer] interview question 42: the maximum sum of continuous subarrays -- with 0x80000000 and int_ MIN](/img/01/bbf81cccb47b6351d7265ee4a77c55.png)
[sword finger offer] interview question 42: the maximum sum of continuous subarrays -- with 0x80000000 and int_ MIN

Division of entity classes (VO, do, dto)

C language: function stack frame

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

The difference between synchronized and reentrantlock

Inter thread wait and wake-up mechanism, singleton mode, blocking queue, timer

Mlx90640 infrared thermal imager temperature sensor module development notes (VII)

语音直播系统——提升云存储安全性的必要手段

DRF学习笔记(一):数据序列化

无线网络分析有关的安全软件(aircrack-ng)
随机推荐
openwrt 增加RTC(MCP7940 I2C总线)驱动详解
Division of entity classes (VO, do, dto)
解决openwrt package目录下多个文件夹重名编译警告(call subdir 函数)
go语言慢速入门——包
First acquaintance with MySQL database
逗号操作符你有用过吗?
The shell script reads the redis command in the text and inserts redis in batches
C language: function stack frame
Inter thread wait and wake-up mechanism, singleton mode, blocking queue, timer
Using Lombok results in the absence of parent class attributes in the printed toString
面试重点——传输层的TCP协议
[regular expression] match the beginning and end
Openwrt adds RTC (mcp7940 I2C bus) drive details
Network principle (1) - overview of basic principles
Sword finger offer 51. reverse pairs in the array
Mlx90640 infrared thermal imager temperature sensor module development notes (VII)
Interview focus - TCP protocol of transport layer
juc包下常用工具类
DRF学习笔记(一):数据序列化
[tensorboard] oserror: [errno 22] invalid argument processing