1

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.

3
  • 3
    Loop, % and / operators are your friends there. Commented Jul 3, 2013 at 4:34
  • There's really no need at all for new here. Commented Jul 3, 2013 at 4:35
  • 2
    also note that x+'0' is equal to 'x' for all 1-digit integers Commented Jul 3, 2013 at 4:35

1 Answer 1

1

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';
Sign up to request clarification or add additional context in comments.

2 Comments

You're printing the digits into chararray backwards ;-). Easiest to start at the right and work back towards the left (i.e. chararray[10-i] = exponent % 10 + '0';)....
Yes, you are right. Edited the post! Checked the test code with 11111111, that's why couldn't catch it :).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.