简体   繁体   中英

C++, Integer and Char array conversion trouble

I have the char array:

char* chararray = new char[33];

and the int :

int exponent = 11111111;

What I want to do, but am confused as to how, is: input the values of exponent into chararray . With the restrictions that exponent has to take up the 2nd to 9th values of chararray . chararray will be all 32 0s,and I want it to become 0xxxxxxxx0000....00, the x's being the 8 digits in exponent .

Furthermore, no build-in conversion functions like atof or atoi. I also want to avoid using Floats or doubles not that you'd really need to.

Note, this is for making IEEE754 32bit values to get some understanding.

Will edit for additional details or clarification if needed.

Try this after initializing the array with '0' :

for(int i=9; i>=2; i--) {
        chararray[i] = (exponent%10) + '0';
        exponent = exponent/10;
    }
chararray[32] = '\0';

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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