| 
 | 
 
 
发表于 2021-3-27 16:45:13
|
显示全部楼层
 
 
 
 本帖最后由 yuxijian2020 于 2021-3-28 00:53 编辑  
 
是这样吗? 
- #include <stdio.h>
 
  
- char* result;
 
  
- void printASCCI(char* str, int len) 
 
 - {
 
 -     int c;
 
 -     int single;
 
  
-     result = (char*)malloc(2 * len);
 
 -     memset(result, 0, 2 * len);
 
  
-     for (int i = 0; i < len; i++) 
 
 -     {
 
 -         c = (int)*str++;
 
 -         c = c & 0x000000ff;
 
 -         //这里就是一个字节一个字节的转成字符串
 
 -         single = c >> 4;
 
 -         if(single <= 0x9)
 
 -             result[2 * i] = single + 0x30;
 
 -         else
 
 -             result[2 * i] = single + 0x37;                //这里改成0x57就变成小写字母了
 
  
-         single = c & 0x0f;
 
 -         if (single <= 0x9)
 
 -             result[2 * i + 1] = single + 0x30;
 
 -         else
 
 -             result[2 * i + 1] = single + 0x37;                //这里改成0x57就变成小写字母了
 
 -         //我没有最后判断字符串结束后加'\0'
 
 -         //而是每次加完2个字符之后立刻在后面补'\0'
 
 -         result[2 * i + 2] = '\0';
 
 -         printf("%02x", c);
 
 -     }
 
 -     printf("\n");
 
 - }
 
  
- int main()
 
 - {
 
 -     char a[] = "abcdefghijklmn";
 
 -     printASCCI(a, strlen(a));
 
 -     printf_s("result = %s\n", result);
 
  
-     return 0;
 
 - }
 
  复制代码 |   
 
 
 
 |