Store an int in a char array?

后端 未结 10 1618
攒了一身酷
攒了一身酷 2020-11-27 17:24

I want to store a 4-byte int in a char array... such that the first 4 locations of the char array are the 4 bytes of the int.

Then, I want to pull the int back out o

10条回答
  •  眼角桃花
    2020-11-27 17:48

    char a[10];
    int i=9;
    
    a=boost::lexical_cast(i)
    

    found this is the best way to convert char into int and vice-versa.

    alternative to boost::lexical_cast is sprintf.

    char temp[5];
    temp[0]="h"
    temp[1]="e"
    temp[2]="l"
    temp[3]="l"
    temp[5]='\0'
    sprintf(temp+4,%d",9)
    cout<

    output would be :hell9

提交回复
热议问题