Convert from integer to string(char*)

12 views
Skip to first unread message

Sam Hu

unread,
Oct 5, 2008, 3:59:29 AM10/5/08
to eC Programming Language
Hi Jerome,

Is there any helper functions in eC to convert from int to char*?
Thanks,
Sam

Jerome St-Louis

unread,
Oct 5, 2008, 12:40:29 PM10/5/08
to ec-programm...@googlegroups.com
Hi Sam,

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, 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.

Reply all
Reply to author
Forward
0 new messages