zoukankan      html  css  js  c++  java
  • C语言版的16进制与字符串互转函数

    http://www.cnblogs.com/nio-nio/p/3309367.html

    /*
    // C prototype : void StrToHex(BYTE *pbDest, BYTE *pbSrc, int nLen)
    // parameter(s): [OUT] pbDest - 输出缓冲区
    //    [IN] pbSrc - 字符串
    //    [IN] nLen - 16进制数的字节数(字符串的长度/2)
    // return value: 
    // remarks : 将字符串转化为16进制数
    */
    void StrToHex(BYTE *pbDest, BYTE *pbSrc, int nLen)
    {
    char h1,h2;
    BYTE s1,s2;
    int i;
    
    for (i=0; i<nLen; i++)
    {
    h1 = pbSrc[2*i];
    h2 = pbSrc[2*i+1];
    
    s1 = toupper(h1) - 0x30;
    if (s1 > 9) 
    s1 -= 7;
    
    s2 = toupper(h2) - 0x30;
    if (s2 > 9) 
    s2 -= 7;
    
    pbDest[i] = s1*16 + s2;
    }
    }
    
    /*
    // C prototype : void HexToStr(BYTE *pbDest, BYTE *pbSrc, int nLen)
    // parameter(s): [OUT] pbDest - 存放目标字符串
    //    [IN] pbSrc - 输入16进制数的起始地址
    //    [IN] nLen - 16进制数的字节数
    // return value: 
    // remarks : 将16进制数转化为字符串
    */
    void HexToStr(BYTE *pbDest, BYTE *pbSrc, int nLen)
    {
    char    ddl,ddh;
    int i;
    
    for (i=0; i<nLen; i++)
    {
    ddh = 48 + pbSrc[i] / 16;
    ddl = 48 + pbSrc[i] % 16;
    if (ddh > 57) ddh = ddh + 7;
    if (ddl > 57) ddl = ddl + 7;
    pbDest[i*2] = ddh;
    pbDest[i*2+1] = ddl;
    }
    
    pbDest[nLen*2] = '';
    }
  • 相关阅读:
    触发器(2)
    触发器
    GZipStream
    MemoryStream类读写内存
    ashx一般处理程序
    Redmin 一键安装
    Unity3D和网页数据交互的基本原理
    apk MIME类型
    [leetcode]Valid Sudoku
    [leetcode]Search in Rotated Sorted Array
  • 原文地址:https://www.cnblogs.com/xunbu7/p/5546232.html
Copyright © 2011-2022 走看看