any predefined function to convert string to int
-
hello.... i have to read a file that has hex values.....i read them as string initially,i would want to know if there is already a function to convert this string(of hex) directly to int?....like we have for converting string(of int) to integer using atoi() fnction....?
-
hello.... i have to read a file that has hex values.....i read them as string initially,i would want to know if there is already a function to convert this string(of hex) directly to int?....like we have for converting string(of int) to integer using atoi() fnction....?
sscanf()
using the%x
format specifier.Ryan
"Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"
-
hello.... i have to read a file that has hex values.....i read them as string initially,i would want to know if there is already a function to convert this string(of hex) directly to int?....like we have for converting string(of int) to integer using atoi() fnction....?
Here's one way to do it: ------------------------ #include #include using namespace std; int main(int argc, char* argv[]) { const char* Number = "baadf00d beeff00d"; istringstream ss; ss.str(Number); unsigned int num; while ( ss >> hex >> num ) { // Converted...Output the number in decimal. cout << num << endl; } return 0; } Steve
-
sscanf()
using the%x
format specifier.Ryan
"Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"
thank u ryan and stephen!..... :-)
-
hello.... i have to read a file that has hex values.....i read them as string initially,i would want to know if there is already a function to convert this string(of hex) directly to int?....like we have for converting string(of int) to integer using atoi() fnction....?
You can use
*scanf
-type functions, but functions specifically designed to do convert strings to numeric values already exist and will likely perform better than the*scanf
-type functions. You do not need to use a shotgun to kill a fly... :) Look up thestrtol
/wcstol
,strtoul
/wcstoul
, andStrToIntEx
functions. TheStrToIntEx
function handles leading whitespace, and sign indicator (ignored), and a leading0x
hex. specifier. Much more robust parsing than you get doing something simple like using"%x"
with a*scanf
-type function. Peace! -=- James
If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong!
Avoid driving a vehicle taller than you and remember that Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road!
DeleteFXPFiles & CheckFavorites (Please rate this post!)