2013-02-15 77 views
1

這是C++中的一個函數,它接受一個HEX字符串並將其轉換爲其等效的ASCII字符。在C++中優化十六進制爲Ascii函數

string HEX2STR (string str) 
{ 
    string tmp; 
    const char *c = str.c_str(); 
    unsigned int x; 
    while(*c != 0) { 
     sscanf(c, "%2X", &x); 
     tmp += x; 
     c += 2; 
    } 
    return tmp; 

如果您輸入以下字符串:

537461636b6f766572666c6f77206973207468652062657374212121 

輸出將是:

Stackoverflow is the best!!! 

說我是輸入1,000,000個獨立HEX串到這個功能,它一段時間,花費計算。

是否有更有效的方法來完成此?

回答

4

當然。看兩個角色在同一時間:

unsigned char val(char c) 
{ 
    if ('0' <= c && c <= '9') { return c  - '0'; } 
    if ('a' <= c && c <= 'f') { return c + 10 - 'a'; } 
    if ('A' <= c && c <= 'F') { return c + 10 - 'A'; } 
    throw "Eeek"; 
} 

std::string decode(std::string const & s) 
{ 
    if (s.size() % 2) != 0) { throw "Eeek"; } 

    std::string result; 
    result.reserve(s.size()/2); 

    for (std::size_t i = 0; i < s.size()/2; ++i) 
    { 
     unsigned char n = val(s[2 * i]) * 16 + val(s[2 * i + 1]); 
     result += n; 
    } 

    return result; 
} 
+0

我得到一個運行時錯誤與功能: http://ideone.com/HpKvXf – user2076892 2013-02-15 20:48:03

+0

@ user2076892嘗試http://ideone.com/iqK6xD – 2013-02-15 20:51:19

+0

謝謝,這工作。 非常感謝Kerrek SB和Mark Ransom! – user2076892 2013-02-15 20:58:07

0

請勿使用sscanf。這是一個非常普遍的靈活功能,這意味着它允許所有這些使用緩慢。相反,走路的字符串和自己轉換每個字符,更快。

2

只是因爲我寫它無論如何,這應該是相當有效:)

const char lookup[32] = 
    {0,10,11,12,13,14,15,0,0,0,0,0,0,0,0,0,0,1,2,3,4,5,6,7,8,9,0,0,0,0,0,0}; 

std::string HEX2STR(std::string str) 
{ 
    std::string out; 
    out.reserve(str.size()/2); 

    const char* tmp = str.c_str(); 

    unsigned char ch, last = 1; 
    while(*tmp) 
    { 
    ch <<= 4; 
    ch |= lookup[*tmp&0x1f]; 
    if(last ^= 1) 
     out += ch; 
    tmp++; 
    } 
    return out; 
} 
0

這個程序需要一個字符串(我稱之爲)hexwords,常用於嵌入式ECU中,例如「31 01 7F 33 38 33 37 30 35 31 30 30 20 20 49」,並在可能的情況下將其轉換爲可讀的ASCII碼。 通過處理ASCII表中的不整合性進行轉換(0-9:48-57,A-F:65-70);

int i,j, len=strlen(stringWithHexWords); 
    char ascii_buffer[250]; 
    char c1, c2, r; 

    i=0; 
    j=0; 

    while (i<len) { 

     c1 = stringWithHexWords[i]; 
     c2 = stringWithHexWords[i+1]; 

     if ((int)c1!=32) { // if space found, skip next section and bump index only once          
     // skip scary ASCII codes          
      if (32<(int)c1 && 127>(int)c1 && 32<(int)c2 && 127>(int)c2) { 
// 

      // transform by taking first hexdigit * 16 and add second hexdigit 
      // both with correct offset 


      r = (char) ((16*(int)c1+((int)c2<64?((int)c2-48):((int)c2-55)))); 


      if (31<(int)r && 127>(int)r) 
         ascii_buffer[j++] = r; // check result for readability  
      } 
      i++; // bump index 
    } 

    i++; // bump index once more for next hexdigit 
} 

ascii_bufferCurrentLength = j; 
return true; 

}

0

的hexToString()函數將其轉換十六進制字符串爲ASCII可讀的字符串

string hexToString(string str){ 
    std::stringstream HexString; 
    for(int i=0;i<str.length();i++){ 
     char a = str.at(i++); 
     char b = str.at(i); 
     int x = hexCharToInt(a); 
     int y = hexCharToInt(b); 
     HexString << (char)((16*x)+y); 
    } 
    return HexString.str(); 
} 

int hexCharToInt(char a){ 
    if(a>='0' && a<='9') 
     return(a-48); 
    else if(a>='A' && a<='Z') 
     return(a-55); 
    else 
     return(a-87); 
}