简体   繁体   English

C ++,Integer和Char数组转换麻烦

[英]C++, Integer and Char array conversion trouble

I have the char array: 我有char数组:

char* chararray = new char[33];

and the int : int

int exponent = 11111111;

What I want to do, but am confused as to how, is: input the values of exponent into chararray . 我想做的是,但对方法却感到困惑,是:将exponent值输入到chararray With the restrictions that exponent has to take up the 2nd to 9th values of chararray . 由于存在限制, exponent必须占用chararray的2到9值。 chararray will be all 32 0s,and I want it to become 0xxxxxxxx0000....00, the x's being the 8 digits in exponent . chararray将全部为32个0,我希望它成为0xxxxxxxx0000 .... 00,x为exponent的8位数字。

Furthermore, no build-in conversion functions like atof or atoi. 此外,没有像atof或atoi这样的内置转换函数。 I also want to avoid using Floats or doubles not that you'd really need to. 我还想避免使用不是您真正需要的Floats或doubles。

Note, this is for making IEEE754 32bit values to get some understanding. 注意,这是为了使IEEE754 32位值有所了解。

Will edit for additional details or clarification if needed. 如有需要,将进行编辑以获取其他详细信息或进行说明。

Try this after initializing the array with '0' : '0'初始化数组后,尝试以下操作:

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

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

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