I'm doing debugging. How do you display an integer value on the screen?
I'm looking for a specific command, or a way to change an int to a string.
Also, what do I initialize strings as?
theres sprintf (and use with allegros textout_ex) from libc, and textprintf_ex from allegro. Also theres atoi and atol from libc as well.
Also theres atoi and atol from libc as well.
What's atoi? What's atol?
I know how to print strings, I just need to convert an int to a string.
What library do I include to use strings, and how do I convert them?
What is atoi? What is atol?
| 1 | atof, atoi, atol |
| 2 | Convert strings to double (atof), integer (atoi), or long (atol). |
| 3 | |
| 4 | double atof( const char *string ); |
| 5 | int atoi( const char *string ); |
| 6 | long atol( const char *string ); |
| 7 | |
| 8 | Routine Required Header Compatibility: |
| 9 | atof <math.h> and <stdlib.h> ANSI |
| 10 | atoi <stdlib.h> ANSI |
| 11 | atol <stdlib.h> ANSI |
| 12 | |
| 13 | Libraries: |
| 14 | LIBC.LIB Single thread static library, retail version |
| 15 | LIBCMT.LIB Multithread static library, retail version |
| 16 | |
| 17 | Return Value: |
| 18 | Each function returns the double, int, or long value produced by interpreting the |
| 19 | input characters as a number. The return value is 0 (for atoi ), 0L (for atol), or |
| 20 | 0.0 (for atof) if the input cannot be converted to a value of that type. The return |
| 21 | value is undefined in case of overflow. |
| 22 | |
| 23 | Parameter: |
| 24 | string String to be converted |
Some sample code:
| 1 | /* ATOF.C: This program shows how numbers stored |
| 2 | * as strings can be converted to numeric values |
| 3 | * using the atof, atoi, and atol functions. |
| 4 | */ |
| 5 | |
| 6 | #include <stdlib.h> |
| 7 | #include <stdio.h> |
| 8 | |
| 9 | void main( void ) |
| 10 | { |
| 11 | char *s; double x; int i; long l; |
| 12 | |
| 13 | s = " -2309.12E-15"; /* Test of atof */ |
| 14 | x = atof( s ); |
| 15 | printf( "atof test: ASCII string: %s\tfloat: %e\n", s, x ); |
| 16 | |
| 17 | s = "7.8912654773d210"; /* Test of atof */ |
| 18 | x = atof( s ); |
| 19 | printf( "atof test: ASCII string: %s\tfloat: %e\n", s, x ); |
| 20 | |
| 21 | s = " -9885 pigs"; /* Test of atoi */ |
| 22 | i = atoi( s ); |
| 23 | printf( "atoi test: ASCII string: %s\t\tinteger: %d\n", s, i ); |
| 24 | |
| 25 | s = "98854 dollars"; /* Test of atol */ |
| 26 | l = atol( s ); |
| 27 | printf( "atol test: ASCII string: %s\t\tlong: %ld\n", s, l ); |
| 28 | } |
| 29 | |
| 30 | Output: |
| 31 | atof test: ASCII string: -2309.12E-15 float: -2.309120e-012 |
| 32 | atof test: ASCII string: 7.8912654773d210 float: 7.891265e+210 |
| 33 | atoi test: ASCII string: -9885 pigs integer: -9885 |
| 34 | atol test: ASCII string: 98854 dollars long: 98854 |
Hope that helps...
You should know how to use that to work with allegro...
What's atoi? What's atol?
atoi() is a char to an integer converter. atol() is a char to a long conversion. To convert an int to a string do this:
... int a = 450; sprintf("a is %i", a);
OUTPUT: a is 450
Reference: http://www.cplusplus.com/ref/cstdio/sprintf.html
Like Thomas said, you can use the built in allegro functions like textout_ex and stuff, they are around the same format as the printf() family.
You use %C where C being the character represented by the type of variable used. Use that website above to look at the different characters you can use.
The printf() family are meant for formatting strings that are dependent on outside sources such as a variable. Learn them, use them, love them.
to: Matthew Dalrymple
Your reference does not match your code:
int sprintf ( char * buffer, const char * format [ , argument , ...] );
is what I got from that...I don't see the buffer you are storing your output in...;D
Edit:
I myself failed him! I didn't notice he wanted to convert TO string not FROM string...
Sorry...use:
| 1 | _itoa() |
| 2 | Convert an integer to a string. |
| 3 | char *_itoa( int value, char *string, int radix ); |
| 4 | |
| 5 | // Sample code: |
| 6 | /* ITOA.C: This program converts integers of various |
| 7 | * sizes to strings in various radixes. |
| 8 | */ |
| 9 | |
| 10 | #include <stdlib.h> |
| 11 | #include <stdio.h> |
| 12 | |
| 13 | void main( void ) |
| 14 | { |
| 15 | char buffer[20]; |
| 16 | int i = 3445; |
| 17 | long l = -344115L; |
| 18 | unsigned long ul = 1234567890UL; |
| 19 | |
| 20 | _itoa( i, buffer, 10 ); |
| 21 | printf( "String of integer %d (radix 10): %s\n", i, buffer ); |
| 22 | _itoa( i, buffer, 16 ); |
| 23 | printf( "String of integer %d (radix 16): 0x%s\n", i, buffer ); |
| 24 | _itoa( i, buffer, 2 ); |
| 25 | printf( "String of integer %d (radix 2): %s\n", i, buffer ); |
| 26 | |
| 27 | _ltoa( l, buffer, 16 ); |
| 28 | printf( "String of long int %ld (radix 16): 0x%s\n", l, |
| 29 | buffer ); |
| 30 | |
| 31 | _ultoa( ul, buffer, 16 ); |
| 32 | printf( "String of unsigned long %lu (radix 16): 0x%s\n", ul, |
| 33 | buffer ); |
| 34 | } |
| 35 | |
| 36 | |
| 37 | Output: |
| 38 | String of integer 3445 (radix 10): 3445 |
| 39 | String of integer 3445 (radix 16): 0xd75 |
| 40 | String of integer 3445 (radix 2): 110101110101 |
| 41 | String of long int -344115 (radix 16): 0xfffabfcd |
| 42 | String of unsigned long 1234567890 (radix 16): 0x499602d2 |
:-P Alright, you got me... OWNED
EDITED CODE:
... int a = 450; char b[480]; sprintf(b, "a is %i", a);
Sorry...just nit picking...I know.
The above fixes are ok... I think what I edited in my post above is what he needs...::)
Edit:
The radixes value above is what number system to use:
such as base 2(binary, base 10(decimal), base 16(hex),ect...
Sorry, but I have no clue about strings. I'm fine with arrays and integers and stuff, but I learned from a tutorial that didn't tell me any of this.
None of this has really helped me, just confused me.
In another language it was as easy as (string)integer_value.
Also, nobody has told me how to initialize strings.
Let me re-state my questions: Converting integers to strings. What to include to be able to make strings.
You should know how to use that to work with allegro...
http://freewebs.com/sandmans_dream<- mine.
EDIT: Dang, three posts went by when I was writing this. Let me look at them...
EDIT2: Hmm... I couldn't really comprehend that code. I don't like using what I can't understand (I can't write it in a mini-function in my code), so could I have a little explanation on what printf is and all those %s things are? Thanks for all the help though
I think you need to spend some more time learning C 
a C string is just an array of char. basic C concept. So is libc.
I didn't mean it like that...I mean that with what I showed you, you should be able to use it with allegro to display it on the screen...
-> Don Freeman - Oh, okay. I though you were calling me a newb. 
Jeez... I thought allegro was C++... >.<
I know about that list of char C idea thing, but I have no clue what this code it doing to it.
Me like C++. Me no like C.
In fact, I'm giving up on this. I'm just not gonna debug my thing this way, and just try random things.
Thanks for the help, but I don't think its gonna stick in my brain.
could I have a little explanation on what printf is and all those %s things are?
Im don't usually like it when people tell others to RTFM, but really, RTFM. And go search for some C tutorials that actually explain strings, and libc functions.
Oh, if you want C++, std::string is your friend. as is std::stringstream (at least I think thats what its called).
Seriously....very easy...
| 1 | #include <stdlib.h> |
| 2 | #include <stdio.h> |
| 3 | #include <memory.h> |
| 4 | void main( void ) |
| 5 | { |
| 6 | char buffer[20]; // make sure to set the array big enough to hold the integer... |
| 7 | // remember that signed integers can range from (-2,147,483,648) through |
| 8 | // (2,147,483,647)...just make sure not to run over the buffer... |
| 9 | // to zero out the buffer use: |
| 10 | memset(&buffer,'\0',sizeof(buffer)); |
| 11 | int i = 3445; |
| 12 | _itoa( i, buffer, 10 ); // you want to get an string that contains the integer i |
| 13 | // in base 10(decimal) format...stored in buffer |
| 14 | printf( "String of integer %d (radix 10): %s\n", i, buffer ); |
| 15 | } |
I don't know how to make it any easier for you...sorry.:'(
Even easier:
stringstream s; s << num; cout << s.str();
and no Windows-style buffer overruns even if you're running a futuristic 256-bit computer.
I'm doing debugging. How do you display an integer value on the screen?
I'm looking for a specific command, or a way to change an int to a string.
Also, what do I initialize strings as?
Actually, if ALL you want to do is display the value of an integer on the screen, then
just do this:
You don't NEED to convert it to a string!
there's a thingee in the code snippets here that is what you want...i modified it to a class to accept any text input(int, char, etc...)and display it on the screen...its pretty cool...
Actually, Don, using allegro it would be textprintf_ex 
Also, shouldn't it be %d, not %i?
%d and %i are identical afaik. for floats and doubles you use %f.
Actually, Don, using allegro it would be textprintf_ex
If we're going to be picky, then it's only for later versions of Allegro that it's textprintf_ex(). 
%d and %i are identical afaik.
For textprintf/textprintf_ex there's no difference. This isn't always the case though.
My textbook (C Programming: A Modern Approach by K.N. King) has this to say on the matter:
Q: I've seen the %i conversion used to read and write integers. What's the difference between %i and %d?
A: When used in a printf format string, there's no difference. In a scanf string however, %d can only match an integer written in decimal (base 10) form, while %i can match an integer expressed in octal (base 8), decimal, or hexidecimal (base 16). If an input number has a 0 prefix (as in 056), %i treats it as an octal number; if it has a 0x or 0X prefix (as in 0x56), %i treats it as a hex number. Using %i instead of %d to read a number can have surprising results if the user should accidentally puts a 0 at the beginning of a number. Because of this trap, I recommend sticking with %d.