如何将一个字符串转换成为16进制数表示?

解决方案 »

  1.   

    自己写函数,将字符串按照一个Byte 两个字符串的方法进行分割字符串然后两字符转换成一个Byte即可
      

  2.   

    我寫的一個函數, 將字符串轉化為一個Byte.BYTE StrToByte(CString szHex)
    {
             szHex.MakeUpper();
    char b[2], br;
    int  n[2]; if (szHex.GetLength() != 2)
    {
    return -1;
    }

    for (int i = 0; i < 2; i++)
    {
    b[i] = szHex.GetAt(i); switch (b[i])
    {
    case '0':
    n[i] = 0; break;
    case '1':
    n[i] = 1; break;
    case '2':
    n[i] = 2; break;
    case '3':
    n[i] = 3; break;
    case '4':
    n[i] = 4; break;
    case '5':
    n[i] = 5; break;
    case '6':
    n[i] = 6; break;
    case '7':
    n[i] = 7; break;
    case '8':
    n[i] = 8; break;
    case '9':
    n[i] = 9; break;
    case 'A':
    n[i] = 10; break;
    case 'B':
    n[i] = 11; break;
    case 'C':
    n[i] = 12; break;
    case 'D':
    n[i] = 13; break;
    case 'E':
    n[i] = 14; break;
    case 'F':
    n[i] = 15; break;
    default: break;
    }
    }

    br = n[0] * 16 + n[1];
    return br;
    }
      

  3.   

    不是吧?没这么麻烦吧
    你就直接把它转化一下就行了
    比如你要转化char c[4]个字节的东西
    unsigned int CReadOutDlg::CharToInt(char a[4])
    {
      unsigned int *p = (unsigned int*)a;
      return *p;
    }
    然后就
    unsigned int m = charToInt(c);
    cstring s;
    s.format=("%x",m);
    就可以得到了
      

  4.   

    有现在的StrToIntEx,注意pszString 字串要以0X开头,如没有就加一下
    BOOL StrToIntEx(
        LPCTSTR pszString,
        DWORD dwFlags,
        int FAR * piRet
        );Converts a decimal or hexadecimal string to an integer. Returns TRUE if the string is converted, or FALSE otherwise. 
    pszString 
    Address of a null-terminated string to be converted. 
    dwFlags 
    Specifies if pszString contains a decimal or hexadecimal value. This can be one of the following values: STIF_DEFAULT  pszString contains a decimal value.  
    STIF_SUPPORT_HEX  pszString contains a hexadecimal value.  piRet 
    Address of an integer variable that receives the converted string.
      

  5.   

    有现成的StrToIntEx,注意pszString 字串要以0X开头,如没有就加一下
    BOOL StrToIntEx(
        LPCTSTR pszString,
        DWORD dwFlags,
        int FAR * piRet
        );Converts a decimal or hexadecimal string to an integer. Returns TRUE if the string is converted, or FALSE otherwise. 
    pszString 
    Address of a null-terminated string to be converted. 
    dwFlags 
    Specifies if pszString contains a decimal or hexadecimal value. This can be one of the following values: STIF_DEFAULT  pszString contains a decimal value.  
    STIF_SUPPORT_HEX  pszString contains a hexadecimal value.  piRet 
    Address of an integer variable that receives the converted string.
      

  6.   

    不明白LZ意思
    1,是不是想对字符串进行编码,那样直接取它们的ASCII值就行了,
    2,如果是想把字符串对应的十进制数转换成16进制的话,就可以先用atoi函数将
    字符串转化成相应的进制整数就行了
    希望能对你有帮助