简体   繁体   English

将字符串转换为无符号整数

[英]Converting string into the unsigned integer

I use the following code to convert the string into the integers: 我使用以下代码将字符串转换为整数:

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;
}

Result: 结果:

0: 1684234849
1: 1751606885
2: 1818978921
3: 1886350957

Result for:abcdefghijklmnop

As you can see, its working really fine, but just until the moment when the encoding is different, eg. 如您所见,它的工作确实很好,但是直到编码不同的那一刻,例如。 for the string like: ®_ďÚ.J.®—Mf3Lý!® (ASCII) (see the Result for: below) 用于以下字符串: ®_ďÚ.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ý!®`)

I was trying setting the encoding in my IDE (Netbeans) but without any possitive results, also I was trying to compile to source on ideone.com, setting the browser encoding - unfortunately with the same results. 我试图在我的IDE(Netbeans)中设置编码,但是没有任何积极的结果,我也试图在ideone.com上编译源代码,设置浏览器编码-不幸的是,结果相同。 Is there any possibility for it to generate the real result based on the input string encoding without messing it up? 它有可能根据输入的字符串编码生成real结果而不会弄乱它吗? Maybe there are any other possibilities to achieve what I want? 也许还有其他可能性可以实现我想要的?

Your IDE is entering the characters encoded as UTF-8. 您的IDE正在输入编码为UTF-8的字符。 I verified this by working backwards from your numeric output, it produced ®_ďÚ.J.®—M . 我通过从您的数字输出中进行反向®_ďÚ.J.®—M验证了这一点,它产生了®_ďÚ.J.®—M By the way, calling that ASCII is not accurate at all. 顺便说一句,调用ASCII根本不准确。

Your output window is using a different encoding. 您的输出窗口正在使用其他编码。 By the looks of it it's code page 1250 Central/Eastern European . 从外观上看,它是代码页1250中欧/东欧

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM