Umwandlung eines Ganzzahlen-Zahlenstrings in eine Zahl.
Umwandlung eines Ganzzahlen-Zahlenstrings in eine Zahl.
[Ab v3.3 R2851, 12.04.2012] Der String darf auch Hexadezimalzahlen enthalten. Hexadezimalzahlen beginnen mit "0x".
Name | Typ | Default | Beschreibung |
Return | int | Zahlenwert des Strings | |
s | char* | - | Zahlenstring |
char s[100];
strcpy (s, "-1231"); showmessage ("%d", val (s)-3); // -1234
Die folgenden Aufrufe geben allesamt die int-Zahl 255 zurück.
val ("255"); val ("0xff"); val ("0xFF"); val ("0xFFpaul");
Der Datentyp int ist 64 Bit breit. Hier ein Beispiel mit Zahlen, deren Darstellung mehr als 32 Bit benötigt.
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
Das obige Beispiel funktioniert natürlich auch dann, wenn die Zahlen als Hex-Zahlen definiert werden.
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