开发者

C++ char array to int and int to char array

开发者 https://www.devze.com 2023-02-05 06:37 出处:网络
I\'ve been trying to do this but I开发者_C百科 wasn\'t able to find any good source for it. Basicly, I want to convert a char* variable (byte array) into an int and vis-versa.

I've been trying to do this but I开发者_C百科 wasn't able to find any good source for it.

Basicly, I want to convert a char* variable (byte array) into an int and vis-versa. Thanks.


To convert from string to integer you can use atoi function and sprintf to do it in other direction.

UPDATE (see comments):

Than you need to do following

char *word = "Hello world";
int ints[11];
for(int i=0; i<strlen(word); ++i)
        ints[i] = (int)word[i];


If you're trying to convert a byte array into an int it is sufficient to use a reinterpret_cast. Technically, this is UB, but if you know the bytes are in the right format, it usually results in exactly what you're asking for.

This is while noting the difference between a char* STRING and a char* BYTE ARRAY.

0

精彩评论

暂无评论...
验证码 换一张
取 消