2013-06-02 167 views
-5

我發現字符串轉換爲PDU此代碼(七重峯至八位字節字符)錯誤C2014;的Visual Studio 2012

#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&#091;0&#093;; //->> 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&#091;i&#093;) & mask) << (8 - bitshift); 
     result += curr; 
    } 
    // Remember the remaining bits of this character so that we can add them later 
    curr = (original&#091;i&#093; & ~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&#091;i&#093;)) << " "; 
    } 
    return os.str(); 
} 

int main() 
{ 
    using namespace std; 
    cout << toHEX(toPDU("hellohello")) << endl; 
    return 0; 
} 

而在視覺工作室2012得到這個錯誤:

錯誤C2014:預處理器命令必須作爲啓動第一個非空白空間

我是新的C++,任何人都可以解釋爲什麼會發生這種錯誤?謝謝。

回答

5

這可能是 「HTML編碼出錯」

original&#091;0&#093;; 

應該

original[0]; 

在你有&#其他地方一樣。

解碼可以在這裏找到:

http://www.ascii.cl/htmlcodes.htm

+0

良好的漁獲...所以也許他從網上或一些地方用編碼不一致的字符複製的代碼。 –

+1

是的,我懷疑它被「編碼」過兩次,或者其他的。或者它已經作爲「代碼」發佈在phpBB論壇上,然後通過一些自動化軟件或其他一些「搗亂」來進行挖掘。 –