我有下面這段代碼從摩斯電ASCII轉換爲ASCII字符串:位集合<4>轉換爲錯誤值Win7上的嵌入式
std::string SixBitToASCII(char *buffer)
{
std::stringstream ss;
std::stringstream out;
for (int index = 0; index < 16; index++)
{
std::bitset<4> bset(buffer[index]);
std::cout << buffer[index] << " - " << bset << std::endl;
ss << bset;
}
std::cout << ss.str() << std::endl;
for (int index = 0; index < 60; index += 6)
{
std::string s = ss.str().substr(index, index + 6);
std::bitset<6> bits;
std::istringstream is(s);
is >> bits;
int asciiCode = bits.to_ulong() + 32;
out << (char) asciiCode;
}
return out.str();
}
它編譯罰款。我正在編譯VS2012 Win7專業32位。
當我運行在嵌入式Win7的I'm得到以下的輸出:
8 - 1000
f - 0110 <<< ??? PROBLEM HERE
2 - 0010
9 - 1001
2 - 0010
3 - 0011
4 - 0100
1 - 0001
0 - 0000
4 - 0100
1 - 0001
3 - 0011
100001100010100100100011010000010000010000010011
這是怎麼回事其中maked是一個問題?轉換F
至0100
???不應該是1111
?
課程的最後轉換是錯誤的,由於這個錯誤convering F
。我試過std::bitset<4> bset((unsigned char) buffer[index])
,結果相同。
幫助讚賞。
除非你需要額外的東西,否則不要使用'std :: endl'。 ''\ n''結束一行。 –
不確定您的意思是「將F轉換爲0100」?你的意思是「將f轉換爲0110」?正如Pete指出的那樣,這對ASCII是正確的。 'f'的ASCII碼是01100110。也許你正在混合十六進制數字F. –
如果你顯示了「緩衝區」中的內容並顯示了你的預期和你正在使用的六位編碼,這將有助於你的問題。維基百科上的樣本不包括64個字符中的任何小寫字母。 –