C语言版 字符串转换为二进制

此程序功能:
假如: unsigned char *szBuf = "1234567890";
那么使用此程序后:
unsigned char *szBuf ="\x12\x34\x56\x78";  //即成员是 0x12, 0x34, 0x56, 0x78,0x90。 

请大家支持一下我的商城:  http://www.yangcheng520.com/goods.php?id=14604

点击(此处)折叠或打开

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <fcntl.h>

  4. #define APP_SUCC 0
  5. #define APP_FAIL -1

  6. #define MAX_NUM 8000

  7. /******************************************************************************
  8. *    函数名称:PubAscToHex
  9. *    函数介绍:将AscII码的字符串转换成压缩的HEX格式
  10. *    函数描述:非偶数长度的字符串根据对齐方式,采取左补0,右补F方式
  11. *    入口参数:pszAsciiBuf    ---- 被转换的ASCII字符串
  12. *             nLen            ---- 输入数据长度(ASCII字符串的长度)
  13. *             cType            ---- 对齐方式 0-左对齐 1-右对齐
  14. *    出口参数:pszBcdBuf        ---- 转换输出的HEX数据
  15. *    返 回 值:APP_SUCC 0 成功; APP_FAIL -1 失败
  16. *    修改日期:2006-09-11
  17. *******************************************************************************/
  18. int PubAscToHex (const unsigned char* pszAsciiBuf, int nLen, char cType, unsigned char* pszBcdBuf)
  19. {
  20.     int i = 0;
  21.     char cTmp, cTmp1;

  22.     if (pszAsciiBuf == NULL)
  23.     {
  24.         return APP_FAIL;
  25.     }

  26.     if (nLen & 0x01 && cType)/*判别是否为奇数以及往那边对齐*/
  27.     {
  28.         cTmp1 = 0 ;
  29.     }
  30.     else
  31.     {
  32.         cTmp1 = 0x55 ;
  33.     }

  34.     for (i = 0; i < nLen; pszAsciiBuf ++, i ++)
  35.     {
  36.         if ( *pszAsciiBuf >= 'a' )
  37.         {
  38.             cTmp = *pszAsciiBuf - 'a' + 10 ;
  39.         }
  40.         else if ( *pszAsciiBuf >= 'A' )
  41.         {
  42.             cTmp = *pszAsciiBuf - 'A' + 10 ;
  43.         }
  44.         else if ( *pszAsciiBuf >= '0' )
  45.         {
  46.             cTmp = *pszAsciiBuf - '0' ;
  47.         }
  48.          else
  49.          {
  50.             cTmp = *pszAsciiBuf;
  51.             cTmp&=0x0f;
  52.          }

  53.         if ( cTmp1 == 0x55 )
  54.         {
  55.             cTmp1 = cTmp;
  56.         }
  57.         else
  58.         {
  59.             *pszBcdBuf ++ = cTmp1 << 4 | cTmp;
  60.             cTmp1 = 0x55;
  61.         }
  62.     }
  63.     if (cTmp1 != 0x55)
  64.     {
  65.         *pszBcdBuf = cTmp1 << 4;
  66.     }

  67.     return APP_SUCC;
  68. }

  69. int PubDispData(const unsigned char *data, int len)
  70. {
  71.     int i = 0;
  72.     int j = 1;
  73.     for(i = 0; i < len; i++)
  74.     {
  75.         if(data[i] <= 9)
  76.         {
  77.             printf("%02x ", data[i]);
  78.         }
  79.         else
  80.         {
  81.             printf("%02x ", data[i]);
  82.         }
  83.         if(i == (8*j-1))
  84.         {
  85.             printf("\n");
  86.             j++;
  87.         }

  88.     }
  89.     printf("\n");
  90.     return 0;
  91. }

  92. int main(int argc, char *argv[])
  93. {
  94.  int i, j;
  95.  int len, rec;
  96.  int nENnum = 0;
  97.  int recTmp = 0;
  98.  char *asc_in = "ASC_IN";
  99.  char *hex_out = "HEX_OUT";
  100.  FILE *out;
  101.  unsigned char in_str[128];
  102.  unsigned char szTmpBuf[MAX_NUM];
  103.  unsigned char szHexbuf[MAX_NUM];
  104.  unsigned char szAscbuf[MAX_NUM];
  105.  
  106.     memset(in_str, 0, 128);
  107.     memset(szAscbuf, 0, sizeof(szAscbuf));
  108.     memset(szHexbuf, 0, sizeof(szHexbuf));
  109.     rec = 0;
  110.     len = MAX_NUM+1;     
  111.      out = fopen(asc_in, "rb");
  112.      if(out != NULL){
  113.          printf("成功打开了 ASC_IN文件\n");
  114.      }
  115.      else{
  116.          printf("打开文件出错!");
  117.          getch();
  118.          return -1;
  119.      }
  120.      rec = fread(szAscbuf, sizeof(unsigned char), len, out);
  121.      printf("读出的字符串长度: %d\n", rec);
  122.      //PubDispData(szAscbuf, rec);
  123.      if(rec >= len){
  124.          printf("读出的字符串长度大于8k!\n");
  125.          fclose(out);
  126.          getch();
  127.          return APP_FAIL;
  128.      }
  129.      if(rec == 0){
  130.          printf("ASC_IN文件没有数据!\n");
  131.          fclose(out);
  132.          getch();
  133.          return APP_FAIL;
  134.      }
  135.      fclose(out);
  136.      
  137.      memcpy(szTmpBuf, szAscbuf, rec);
  138.      recTmp = rec;
  139.      j = 0;
  140.      for(i = 0; i < recTmp; i++)
  141.      {
  142.          if(szTmpBuf[i] == 0x0D && szTmpBuf[i+1] == 0x0A)
  143.          {
  144.              rec -= 2;
  145.              nENnum++;
  146.              i++;
  147.              continue;
  148.          }
  149.          else{
  150.              szAscbuf[j] = szTmpBuf[i];
  151.              j++;
  152.          }
  153.      }
  154.      printf("去点回车字节个数:%d,包含%d个字节\n", nENnum, nENnum*2);
  155.      printf("去点回车字节后长度:%d", rec);
  156.      len = rec;
  157.      PubAscToHex(szAscbuf, len, 0, szHexbuf);
  158.      
  159.      out = fopen(hex_out, "wb");
  160.      fwrite(szHexbuf, sizeof(unsigned char), (len/2+len%2), out);
  161.      fclose(out);
  162.  /*
  163.  out = fopen("Test", "r+");
  164.  fread(in_str, sizeof(unsigned char), len, out);
  165.  for(i = 0; i < len; i++)
  166.   printf("%x ", in_str[i]);
  167.  fclose(out);*/
  168.  printf("\n");
  169.  getch();
  170.  
  171.  /*
  172.  {
  173.   memset(in_str, 0, 128);
  174.   int fd;
  175.   fd = open("PText", O_CREAT|O_RDWR, 0666);
  176.   write(fd, string, len);
  177.   close(fd);
  178.   
  179.   fd = open("PText", O_RDWR);
  180.   read(fd, in_str, len);
  181.   for(i = 0; i < len; i++)
  182.    printf("%x ", in_str[i]);
  183.   close(fd);
  184.   printf("\n");
  185.  }
  186.  
  187.  {
  188.   out = fopen("TTest", "w+");
  189.   for(i = 0; i < len; i++){
  190.    fprintf(out, "0x%x ", string[i]);
  191.   }
  192.   fclose(out);
  193.   
  194.   memset(in_str, 0, 128);
  195.   out = fopen("TTest", "r+");
  196.   for(i = 0; i < len; i++){
  197.    fscanf(out, "%x", &in_str[i]);
  198.   }
  199.   for(i = 0; i < len; i++)
  200.    printf("%x ", in_str[i]);
  201.   fclose(out);
  202.   printf("\n");
  203.  }
  204.  */
  205.  return 0;
  206. }
使用VC6.0即可编译:
VCMake.bat:

点击(此处)折叠或打开

  1. set VCBIN=D:\Microsoft Visual Studio\VC98\Bin
  2. set INCLUDE=D:\Microsoft Visual Studio\VC98\Include
  3. set LIB=D:\Microsoft Visual Studio\VC98\Lib
  4. set PATH=%VCBIN%
  5. cl *.c
  6. pause
编译成功后既可以运行。要自己建立两个文件:"ASC_IN" 和 "HEX_OUT"。
ASC_IN放入一串字符,0~9,a~f 即可。


阅读(494) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~
评论热议
阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页