There are no stupid questions, only stupid answers.
Don't be shy to ask, that's how we fill up these forums with easy to find answers!
Turning numbers into a string of digits characters is a process that is often greatly simplified by programming languages. C belongs to the category of those in which it is not.
Because C supports direct memory access, and because character strings are typically stored into pointers used to reference an address, casting a number to a
char * in fact makes up a pointer pointing at the memory location represented by that number. That will be an invalid address unless your number is indeed a valid address in memory your program allocated. So you don't want to be doing that.
The typical way to turn a number into a string in C is using the standard C library sprintf function.
There are a few additional ways to do it in eC. You can take a look at this
post on the eC programming Google Groups. Here's the excerpt:
Hi Sam,
Code: Select all
// The usual sprintf method from the standard C library works:
int number = 1234;
char string[100];
sprintf(number, "%d", 1234);
// Alternatively, eC proposes some new ways:
PrintBuf(string, sizeof(string), number); // This outputs to the string buffer, just like the printf call above
// PrintString allocates a new string ( which must be deleted )
char * string;
string = PrintString(number);
// Use the string
delete string;
// If putting to a file, the File class also has Print methods:
File f = FileOpen("test.txt", write);
f.Print(number);
delete f;
The PrintLn equivalent for Print also exists, printing a new line at the
end. A variable number of additional parameters to be printed (which can be
of various different types) can also be added.
See the
latest release notes from 0.43 for a short description of the
Print/PrintLn functions.
It seems the Surface object is lacking a
Print like function that can conveniently accept any type of parameters. (
New Mantis ticket here) DialogBoxes and other Window objects also seem to be lacking this type of functionality. For now this code should do it:
Code: Select all
char s[100];
PrintBuf(s, sizeof(s), i);
surface.WriteText(x1, y1, s, strlen(s));