hi, i need to get a negative int into its 2's complement binary representation.
i was screwing around with the itoa() function and found that when i put the base input as 2, it would turn my negative numbers into 2's compliment, so i tried implementing this into my project as so:
Code:
int diff, lower,higher;
char biChra[16];
string biStr;
diff=lower-higher;
itoa (diff,biChra,2);
ostringstream biStrout (biStr);
biStrout<<setw(16)<<biChra;
return biStrout.str();
say if lower=4 and higher=13, then diff =-9, and theoretically, itoa should give me 1111 1111 1111 0111, which i then want to return as a string.
however, it throws a "Unhandled exception: ... Access violation writing location" error, and when i mouse over biChra, it shows
Code:
biChra = 0x0013f1a8 "11111111111111111111111111110111"
which is a bit more than 16 digits, but still equivalent to -9 in 2's comp binary.
so, what im getting at is, is this an issue with itoa()? if so, are there any other functions that can output in 2's comp binary, or will i have to write a function of my own? (i've seen sprintf thrown around a lot, but it doesn't look like it can convert to binary)