Advertisement

Integer to string??

Started by March 12, 2001 10:30 PM
11 comments, last by DisKordCoder 23 years, 10 months ago
Im currently working within MFC and i was wondering how u convert an Integer to a String for use in a TreeView. Any help will do thanks!!
HEre is my algo for displaying integers in Win32. I am not sure what you were asking for exactly.




const int ARRAY_LENGTH = 64;
char array[ARRAY_LENGTH]; // Array to hold final string to represent the number
const int k = 10; // If you devide a number by 10, you can get the number in the end digit
int j = 10; // Used to drop the end digit

memset((void*)array, '' '', sizeof(array));

for(int i = (ARRAY_LENGTH - 2); i >= 0; i--)
{
switch(number % k)
{
case 0:
array = ''0'';
break;
case 1:
array = ''1'';<br> break;<br> case 2:<br> array = ''2'';<br> break;<br> case 3:<br> array = ''3'';<br> break;<br> case 4:<br> array = ''4'';<br> break;<br> case 5:<br> array = ''5'';<br> break;<br> case 6:<br> array = ''6'';<br> break;<br> case 7:<br> array = ''7'';<br> break;<br> case 8:<br> array = ''8'';<br> break;<br> case 9:<br> array = ''9'';<br> break;<br> default:<br> return ARROW_FAILURE;<br> }<br><br> number /= j;<br><br> if(number <= 0)<br> break;<br> }<br><br> array[ARRAY_LENGTH - 1] = ''\0'';<br> <br> for(int m = 0, n = 0; m < ARRAY_LENGTH; m++)<br> if(array[m] != '' '')<br> {<br> array[n] = array[m];<br> n++;<br> }<br><br><br> ::DrawText(m_hDC, array, -1, lpRect, uFormat);<br><br></code><br><br><br> </i> <br><br> Brent Robinson<br>"Stupidity is a virtue"-me
"The computer programmer is a creator of universes for which he alone is the lawgiver...No playwright, no stage director, no emperor, however powerful, has ever exercised such absolute athority to arrange a stage or a field of battle and to command such unswervingly dutiful actors or troops." - Joseph Weizenbaum-Brent Robinson
Advertisement
sprintf(text_string,"%i",integer);

Edit:
After a quick reminder that MFC uses CString (i dont use mfc, dont like it at all heh) I did a little checkup. For CString, use:

str.FormatV("%i",integer);

where str is a CString.


Edited by - Maximus on March 13, 2001 1:16:37 AM
-----------------------"When I have a problem on an Nvidia, I assume that it is my fault. With anyone else's drivers, I assume it is their fault" - John Carmack
quote:
Brent Robinson

"Stupidity is a virtue"-me



you don't say =) =) =) nah just kidding but it looks so funny to see such a long function, and then seeing Maximus' one line answer on the next post.

But it is good to be able to write functions like that. Builds the mind. Heightens the skills. Etc. Etc.

Edited by - wise_guy on March 12, 2001 12:37:20 AM
quote:
Original post by Coconut

HEre is my algo for displaying integers in Win32. I am not sure what you were asking for exactly.



You know, instead of that huge case statement, you can use:

temp = number % 10;if (temp >= 0 || temp <= 9){    array = temp + ''0'';    number /= 10;}else{    return ARROW_FAILURE} 



logically, if you add the character ''0'' to the number 0, you get the character ''0''.

''0'' + 1 = ''1'', etc.

===============================================
Hurry up madness, hurry up disease,
hurry up insanity, hurry up please.
Hooray! I say, for the end of the world.
This is my signature. There are many like it, but this one is mine. My signature is my best friend. It is my life. I must master it as I must master my life. My signature, without me, is useless. Without my signature, I am useless.
use the functions

itoa() integer to string
atoi() string to integer

i.e. the number 123456789 would become "123456789" and
vice versa

-Vulcan
Advertisement
The atof instruction is good. I use this myself.

Indy
Indy
What I''ve never got about atof() is why isn''t the inverse function called ''ftoa'', but fcvt, ecvt & gcvt...
I wrote an ftoa function of my own, though it requires an external data stack (the 16 bit ASM version doesnt though, it uses the system stack.)


anyhow, it takes a float and converts it into normalized scientific notation:


(sign) | (first digit) | . | (15 digits) | e | (exponent sign) | (3 digit exponent)


ie:

-4.1234e-010
4.1234e010
1.234e-100


etc.
normalized requires that the first digit is non-zero, and that there is only one digit in front of the decimal point.

I''ll post the general algorithm later if i have time.


===============================================
Hurry up madness, hurry up disease,
hurry up insanity, hurry up please.
Hooray! I say, for the end of the world.
This is my signature. There are many like it, but this one is mine. My signature is my best friend. It is my life. I must master it as I must master my life. My signature, without me, is useless. Without my signature, I am useless.
Oh yeah, it cuts off digits if they round to zero, instead of printing 15 zero''s.


instead of:
1.234500000000000e002

it would print:
1.2345e002


i suppose i should modify the routine to print an alterable number of digits... heh.

===============================================
Hurry up madness, hurry up disease,
hurry up insanity, hurry up please.
Hooray! I say, for the end of the world.
This is my signature. There are many like it, but this one is mine. My signature is my best friend. It is my life. I must master it as I must master my life. My signature, without me, is useless. Without my signature, I am useless.

This topic is closed to new replies.

Advertisement