eine Hex zahl in Int umrechnen
-
Hallo
hab viel gegooglet, und auch die Forensuche benutzt, aber nichts dazu gefunden.
ich habe in einem CString eine Hexzahl stehen. Gibt es eine Möglichkeit aus dieser Hexzahl im cString eine Int-zahl zu machen??mfg
-
Schau dir strtol(..) an.
Es gibt sicher auch noch Möglichkeiten mit stringstreams.
Kurt
-
Das du viel gegoogelt hast ist aber gelogen oder? Erster Treffer:
#include "stdafx.h" #include <malloc.h> #include <Afx.h> int _httoi(const TCHAR *value) { struct CHexMap { TCHAR chr; int value; }; const int HexMapL = 16; CHexMap HexMap[HexMapL] = { {'0', 0}, {'1', 1}, {'2', 2}, {'3', 3}, {'4', 4}, {'5', 5}, {'6', 6}, {'7', 7}, {'8', 8}, {'9', 9}, {'A', 10}, {'B', 11}, {'C', 12}, {'D', 13}, {'E', 14}, {'F', 15} }; TCHAR *mstr = _tcsupr(_tcsdup(value)); TCHAR *s = mstr; int result = 0; if (*s == '0' && *(s + 1) == 'X') s += 2; bool firsttime = true; while (*s != '\0') { bool found = false; for (int i = 0; i < HexMapL; i++) { if (*s == HexMap[i].chr) { if (!firsttime) result <<= 4; result |= HexMap[i].value; found = true; break; } } if (!found) break; s++; firsttime = false; } free(mstr); return result; } int main(int argc, char* argv[]) { CString myString ("0xFFFF"); _tprintf(_T("Hex String: %s is int: %d\n\r"), myString, _httoi(myString)); return 0; }
-
asdrubael schrieb:
Das du viel gegoogelt hast ist aber gelogen oder?
sry aber sowas lass ich mir ned unterstellen...
cya
-
ZuK schrieb:
Schau dir strtol(..) an.
Danke für deine freundliche Hilfe. Mit strtol(...) funktioniert es.
Vielen Dank