2016-02-20 259 views
4

我在這裏發現了類似的解決方案: Missing punctuation from C++ hex2bin和使用回答:C++長十六進制字符串轉換爲二進制

std::string hex2bin(std::string const& s) { 

    std::string sOut; 
    sOut.reserve(s.length()/2); 

    std::string extract; 
    for (std::string::const_iterator pos = s.begin(); pos<s.end(); pos += 2) 
    { 
     extract.assign(pos, pos+2); 
     sOut.push_back(std::stoi(extract, nullptr, 16)); 
    } 
    return sOut; 
} 

int main() 
{ 
    printf("DECODED: %s\n", hex2bin("5468697320697320612031323320746573742e").c_str()); 

} 

這就是打印樣例如:

DECODED: This is a 123 test. 

但似乎並不是運作良好例如:較長的十六進制字符串,例如:

printf("DECODED: %s\n", hex2bin("0200000081cd02ab7e569e8bcd9317e2fe99f2de44d49ab2b8851ba4a308000000000000e320b6c2fffc8d750423db8b1eb942ae710e951ed797f7affc8892b0f1fc122bc7f5d74df2b9441a42a14695").c_str()); 

並且結果爲:

DECODED: 

因此如何用C++來完成。

我更熟悉PHP,並有功能hex2bin做所有的工作。

那個小更新: 我使用該函數:

std::string hex2bin(std::string s) { 
    std::string rc; 
    int nLen = s.length(); 
    int tmp; 
    for (int i(0); i + 1 < nLen; i += 2) { 
     if (std::istringstream(s.substr(i, 2)) >> std::hex >> tmp) { 
      rc.push_back(tmp); 
     } 
    } 
    return rc; 
} 

,什麼那功能由字符返回字符: C++

index 0: 2 
index 1: 0 
index 2: 0 
index 3: 0 
index 4: -127 
index 5: -51 
index 6: 2 
index 7: -85 
index 8: 126 
index 9: 86 
index 10: -98 
index 11: -117 
index 12: -51 
index 13: -109 
index 14: 23 
index 15: -30 
index 16: -2 
index 17: -103 
index 18: -14 
index 19: -34 
index 20: 68 
index 21: -44 
index 22: -102 
index 23: -78 
index 24: -72 
index 25: -123 
index 26: 27 
index 27: -92 
index 28: -93 
index 29: 8 
index 30: 0 
index 31: 0 
index 32: 0 
index 33: 0 
index 34: 0 
index 35: 0 
index 36: -29 
index 37: 32 
index 38: -74 
index 39: -62 
index 40: -1 
index 41: -4 
index 42: -115 
index 43: 117 
index 44: 4 
index 45: 35 
index 46: -37 
index 47: -117 
index 48: 30 
index 49: -71 
index 50: 66 
index 51: -82 
index 52: 113 
index 53: 14 
index 54: -107 
index 55: 30 
index 56: -41 
index 57: -105 
index 58: -9 
index 59: -81 
index 60: -4 
index 61: -120 
index 62: -110 
index 63: -80 
index 64: -15 
index 65: -4 
index 66: 18 
index 67: 43 
index 68: -57 
index 69: -11 
index 70: -41 
index 71: 77 
index 72: -14 
index 73: -71 
index 74: 68 
index 75: 26 
index 76: 66 
index 77: -95 
index 78: 70 
index 79: -107 

PHP

index 0: 2 
index 1: 0 
index 2: 0 
index 3: 0 
index 4: 129 
index 5: 205 
index 6: 2 
index 7: 171 
index 8: 126 
index 9: 86 
index 10: 158 
index 11: 139 
index 12: 205 
index 13: 147 
index 14: 23 
index 15: 226 
index 16: 254 
index 17: 153 
index 18: 242 
index 19: 222 
index 20: 68 
index 21: 212 
index 22: 154 
index 23: 178 
index 24: 184 
index 25: 133 
index 26: 27 
index 27: 164 
index 28: 163 
index 29: 8 
index 30: 0 
index 31: 0 
index 32: 0 
index 33: 0 
index 34: 0 
index 35: 0 
index 36: 227 
index 37: 32 
index 38: 182 
index 39: 194 
index 40: 255 
index 41: 252 
index 42: 141 
index 43: 117 
index 44: 4 
index 45: 35 
index 46: 219 
index 47: 139 
index 48: 30 
index 49: 185 
index 50: 66 
index 51: 174 
index 52: 113 
index 53: 14 
index 54: 149 
index 55: 30 
index 56: 215 
index 57: 151 
index 58: 247 
index 59: 175 
index 60: 252 
index 61: 136 
index 62: 146 
index 63: 176 
index 64: 241 
index 65: 252 
index 66: 18 
index 67: 43 
index 68: 199 
index 69: 245 
index 70: 215 
index 71: 77 
index 72: 242 
index 73: 185 
index 74: 68 
index 75: 26 
index 76: 66 
index 77: 161 
index 78: 70 
index 79: 149 

所以喲你可以注意到在C++中的值小於0則顯示在另一個php中。我想在php中有相同的功能鏈接。

回答

3

您的示例包含數字「00」,該數字將轉換爲字符值0,printf將將其解釋爲零終止符並停止打印。

+0

但是爲什麼?從PHP的hex2bin函數打印一些字符: 〜V ͓ DԚ u#ۋ B q ח+ MDBF。 IMO C++函數應該做同樣的事情。 – Robert

+1

@Robert嘗試C++'std :: cout'。 – LogicStuff

+2

這是因爲PHP是不同規則的不同語言。在C++中,零用作字符串終止符,因此只要遇到零就停止打印。 –

1
printf("DECODED: %s\n", hex2bin("0200000081cd02ab7e569e8bcd9317e2fe99f2de44d49ab2b8851ba4a308000000000000e320b6c2fffc8d750423db8b1eb942ae710e951ed797f7affc8892b0f1fc122bc7f5d74df2b9441a42a14695").c_str()); 

hex2bin函數實際上返回std::string與80個字符。這只是一個顯示問題。剛打印的字符與它們的整數值一起單獨看這個問題:

std::string const s = hex2bin("0200000081cd02ab7e569e8bcd9317e2fe99f2de44d49ab2b8851ba4a308000000000000e320b6c2fffc8d750423db8b1eb942ae710e951ed797f7affc8892b0f1fc122bc7f5d74df2b9441a42a14695"); 

for (std::string::size_type i = 0; i < s.size(); ++i) 
{ 
    std::cout << "index " << i << ": " << static_cast<int>(s[i]) << ", printed like this: " << s[i] << "\n"; 
} 

你會得到的輸出是這樣的:

index 0: 2, printed like this: ☻ 
index 1: 0, printed like this: 
index 2: 0, printed like this: 

[...]

index 77: -95, printed like this: í 
index 78: 70, printed like this: F 
index 79: -107, printed like this: ò 

在我看來,函數hex2bin應該返回std::vector<char>,因爲它傳達了in帳篷更好,而且打印不正確更難。不應將二進制數據輸入到std::coutprintf,就好像它是可讀的字符串一樣,編譯器不會有任何抱怨的機會。

+0

a已經做了一些比較,請閱讀我的下一篇文章,以便更清楚地瞭解 – Robert

相關問題