Veröffentlicht 24. April 200322 j Hallo, ich habe einen String eingelesen der eine Hexadezimale zahl enthält z.B. "3F2D" ich möchte jetzt den Hex string in eine dezimale zahl umwandeln. z.B. "16173" in diesem zusammenhang habe ich einmal eine funktion gefunden die mir eine zahl aus einem string macht (string = "1234" int ="1234") int i_zahl = atoi(s_zahl); solch eine funktion suche ich um einen Hex string in eine dezimale zahl umzuwandeln! danke mfg
24. April 200322 j Mithilfe der Suchfunktion und dem Stichwort "Hex", habe ich folgende Beiträge gefunden;) Gruß Guybrush
24. April 200322 j K & R: long strtol(const char *s, char **endp, int base) strtol converts the prefix of s to long, ignoring leading white space; it stores a pointer to any unconverted suffix in *endp unless endp is NULL. If base is between 2 and 36, conversion is done assuming that the input is written in that base. If base is zero, the base is 8, 10, or 16; leading 0 implies octal and leading 0x or 0X hexadecimal. Letters in either case represent digits from 10 to base-1; a leading 0x or 0X is permitted in base 16. If the answer would overflow, LONG_MAX or LONG_MIN is returned, depending on the sign of the result, and errno is set to ERANGE.
24. April 200322 j Hallo, http://www.fachinformatiker-world.de/forums/showthread.php?s=&threadid=33194&highlight=hex sollte Dein Problem lösen. Nic
24. April 200322 j Auf Vorzeichenprüfung hab ich jetzt keinen Bock... und Überläufe lass ich laufen... #include <ctype.h> int Hex2Int(const char *hex) { int val = 0; // skip leading"0x" or "0X" if(hex[0] == '0' && (hex[1] == 'x' || hex[1] == 'X')) hex += 2; while(*hex) { int nibble; if(*hex >= '0' && *hex <= '9') nibble = *hex - '0'; else nibble = 0xA + tolower(*hex) - 'a'; val = (val << 4) + nibble; hex++; } return val; } [/PHP]
Erstelle ein Konto oder melde dich an, um einen Kommentar zu schreiben.