C语言定义 二进制 十六进制 普通字符串 转换函数

  直接上干货,没啥好说的:

代码1:十六进制转字符串函数

 1 #include<stdio.h>
 2 #include<string.h>
 3 #include<ctype.h>
 4 void Hex2Byte(const char* source, unsigned char* dest, int sourceLen)
 5 {
 6     short i;
 7     unsigned char highByte, lowByte;
 8     for (i = 0; i < sourceLen; i += 2)
 9     {
10         highByte = toupper(source[i]);
11         lowByte  = toupper(source[i + 1]);
12         if (highByte > 0x39)  
13             highByte -= 0x37; //'A'->10 'F'->15
14         else
15             highByte -= 0x30; //'1'->1 '9'->9
16         if (lowByte > 0x39)
17             lowByte -= 0x37;
18         else
19             lowByte -= 0x30;
20         dest[i / 2] = (highByte << 4) | lowByte;
21     }
22 }
23 int main(){
24     char src[200]="466c6f776572732061726520736f20696e636f6e73697374656e74212042757420492077617320746f6f20796f756e6720746f206b6e6f7720686f7720746f206c6f76652068652e\0";
25     unsigned char buf[100]={
   0};
26     Hex2Byte(src,buf,strlen(src));
27     printf("%d\n",strlen(buf)); //打印一下生成十六进制字符串的长度
28     puts(buf);
29     return 0;
30 }

执行效果:

代码2:字符串转十六进制字符串函数

 1 #include<stdio.h>
 2 #include<string.h>
 3 //字符串转十六进制字符串
 4 void Str2Hex( const char *sSrc,  char *sDest, int nSrcLen )
 5 {
 6     int  i;
 7     char<
  • 1
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值