| Issue 97: | Parse hex int | |
| 1 person starred this issue and may be notified of changes. | Back to list |
Hi, Note: I previously found a similar problem on the php target, http://haxe.1354130.n2.nabble.com/php-Quirk-in-the-php-implementation-of-Std-parseInt-td5961920.html, this one is for the cpp target This problem arises from using Std.parseInt on a hexadecimal string with a value that would overflow a signed long if it was first converted as a unsigned long, this I assume is because strtol assumes that hexadecimal strings are positive as apposed to haXe's method of assuming that it is unsigned. The solution as I see it is to use strtoul (the unsigned version) when dealing with hexadecimal strings and to cast it to a signed long. The resulting function would be: Dynamic __hxcpp_parse_int(const String &inString) { if (!inString.__s) return null(); long result; const HX_CHAR *str = inString.__s; bool hex = (str[0]=='0' && (str[1]=='x' || str[1]=='X')); HX_CHAR *end = 0; #ifdef HX_UTF8_STRINGS if (hex) result = *(long) strtoul*(str+2,&end,16); else result = strtol(str,&end,10); #else if (hex) result = *(long) wcstoul*(str+2,&end,16); else result = wcstol(str,&end,10); #endif if (str==end) return null(); return (int)result; }
Jul 7, 2011
Project Member
#1
gameh...@gmail.com
Status:
Fixed
|