Convert a fixed number string into a number.
Convert a fixed number string into a number.
Since v3.3 R2851, 12.04.2012 The string may also begin with hexadecimal numbers. Hexadecimal numbers must begin with "0x".
Name | Type | Default | Description |
Return | int | Number value of the string | |
s | char* | - | Number string |
char s[100];
strcpy (s, "-1231"); showmessage ("%d", val (s)-3); // -1234
The following lines all will return the integer 255.
val ("255"); val ("0xff"); val ("0xFF"); val ("0xFFpaul");
Data type int is 64bit aware. Here's an example with integers greater than 32 bit:
int main () { int a = 6917529027641081856; char * astr = "6917529027641081856";
wlog ("", "a=%d\t(and as string '%s')\n", a, itoa (a)); wlog ("", "a=0x%x\t(and as string '%s')\n", a, itoa (a)); wlog ("", "astr='%s'\t(and as int %d)\n", astr, val (astr)); wlog ("", "astr='%s'\t(and as int 0x%x)\n", astr, val (astr));
wlog ("", "string compare : a ?= astr : %d\n", (a == val (astr))); wlog ("", "int compare : a ?= astr : %d\n", strcmp (itoa (a), astr)==0);
return 0; } // Ausgabe // //a=6917529027641081856 (and as string '6917529027641081856') //a=0x6000000000000000 (and as string '6917529027641081856') //astr='6917529027641081856' (and as int 6917529027641081856) //astr='6917529027641081856' (and as int 0x6000000000000000) //string compare : a ?= astr : 1 //int compare : a ?= astr : 1
Of course, the above example willstill work, if the intergers are given as hex values.
int main () { int a = 0x6000000000000000; char * astr = "0x6000000000000000";
wlog ("", "a=%d\t(and as string '%s')\n", a, itoa (a)); wlog ("", "a=0x%x\t(and as string '%s')\n", a, itoa (a)); wlog ("", "astr='%s'\t(and as int %d)\n", astr, val (astr)); wlog ("", "astr='%s'\t(and as int 0x%x)\n", astr, val (astr));
wlog ("", "string compare : a ?= astr : %d\n", (a == val (astr))); wlog ("", "int compare : a ?= astr : %d\n", strcmp (itoa (a), astr)==0);
return 0; } // Ausgabe // //a=6917529027641081856 (and as string '6917529027641081856') //a=0x6000000000000000 (and as string '6917529027641081856') //astr='0x6000000000000000' (and as int 6917529027641081856) //astr='0x6000000000000000' (and as int 0x6000000000000000) //string compare : a ?= astr : 1 //int compare : a ?= astr : 1
Alphabetic index HTML hierarchy of classes or Java