我使用以下代码将字符串转换为整数:
int main(){
const char* mystring="abcdefghijklmnop";
unsigned int* key = (unsigned int*)mystring;
for(int i=0;i<2;i++) {
std::cout << i << ": " << key[i] << std::endl;
}
std::cout << std::endl << "Result for:" << mystring << std::endl;
}
结果:
0: 1684234849
1: 1751606885
2: 1818978921
3: 1886350957
Result for:abcdefghijklmnop
如您所见,它工作得很好,但直到编码不同的那一刻,例如。对于像这样的字符串:®_ďÚ.J.®—Mf3Lý!®
(ASCII)(参见下面的Result for:
)
It returns:
0: 3294604994
1: 781894543
2: 2931961418
3: 1301577954
Result for:®_ÄŹĂš.J.®—Mf3LĂ˝!® // <-- notice this, its totally different as the input (`®_ďÚ.J.®—Mf3Lý!®`)
我试图在我的 IDE (Netbeans) 中设置编码但没有任何积极的结果,我也试图编译到 ideone.com 上的源代码,设置浏览器编码 - 不幸的是结果相同。它是否有可能根据输入字符串编码生成 real
结果而不会弄乱它?也许有任何其他的可能性来实现我想要的?
最佳答案
您的 IDE 正在输入编码为 UTF-8 的字符。我通过从您的数字输出向后计算验证了这一点,它生成了 ®_ďÚ.J.®—M
。顺便说一句,称其为 ASCII 根本不准确。
您的输出窗口使用了不同的编码。看样子是code page 1250 Central/Eastern European .
关于c++ - 将字符串转换为无符号整数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23025101/