-5
#include <iostream>
#include <sstream>
#include <iomanip>
#include <string>
std::string toPDU(const std::string &original)
// Converts an ANSI string to PDU format
{
if (original.empty()) {
// Empty string -> nothing to do
return original;
}
std::string result;
// Reserve enough space to hold all characters
result.reserve((original.length() * 7 + 7)/8);
// Variables for holding the current amount of bits to copy from the next character
size_t bitshift = 1, mask = 0x01;
unsigned char curr = original[0]; //->> ERROR !!!!!!!!!!!!!!!!!
for (size_t i = 1; i < original.length(); ++i) {
if (bitshift != 0) {
// If bitshift is 0, then curr will be 0, so in effect we should skip a character
// So only do the following when bitshift different from 0
// Add the low bits (using the mask) to the left of the current character
curr += (static_cast<unsigned char>(original[i]) & mask) << (8 - bitshift);
result += curr;
}
// Remember the remaining bits of this character so that we can add them later
curr = (original[i] & ~mask) >> bitshift;
// Cycle bitshift through 0-7 (could also be written as bitshift = (bitshift + 1) mod 8)
bitshift = (bitshift + 1) & 0x7;
// Set the mask to have all bitshift lower bits set
// e.g. bitshift = 3, then mask = 0x07
// bitshift = 5, then mask = 0x1F
// bitshift = 7, then mask = 0x7F
mask = (1 << bitshift) - 1;
}
result += curr;
return result;
}
std::string toHEX(const std::string &original)
// Converts a string to the hexadecimal representation of its characters
{
std::ostringstream os;
os << std::hex << std::uppercase;
for (size_t i = 0; i < original.length(); ++i) {
os << static_cast<unsigned int>(static_cast<unsigned char>(original[i])) << " ";
}
return os.str();
}
int main()
{
using namespace std;
cout << toHEX(toPDU("hellohello")) << endl;
return 0;
}
(중선은 문자 옥텟) PDU에 변환 문자열이 코드를 발견 및 Visual Studio 2012이 오류가 발생했습니다 :
오류 C2014 : 전 처리기 명령으로 시작해야합니다 첫 번째 비 흰색 공간
저는 C++을 처음 접했고, 누구나이 오류가 발생하는 이유를 설명 할 수 있습니까? 감사.
좋은 캐치 ... 어쩌면 그는 웹 또는 불일치를 인코딩 문자로 어떤 장소에서 코드를 복사. –
예, 두 번이나 "인코딩 된"것으로 의심됩니다. 또는 phpBB 포럼에 "코드"로 게시 된 다음 일부 자동 소프트웨어 또는 다른 "맹 글링"으로 긁어 모았습니다. –