E
Edward Arthur
Hi,
I'm trying to convert a string to an unsigned long long int.
% /tmp/long
str 0x20000
base 16
decimal 131072
hex 20000
str 0x1abcd8765
base 10
decimal ffffffff
hex ffffffff
But when the value is > 2^32 "cout" does not cooperate.
% gcc -v
Reading specs from /usr/lib/gcc-lib/i386-redhat-linux/2.96/specs
gcc version 2.96 20000731 (Red Hat Linux 7.1 2.96-98)
% cat long.cc
#include <iostream>
#include <string.h>
#include <stdlib.h>
void print_str(char *str)
{
int base;
long long int l;
base = (strncasecmp(str, "0x", 2) == 0) ? 16 : 10;
l = strtoul(str, NULL, base);
cout << "str " << str << endl;
cout << "base " << base << endl;
cout << "decimal " << l << endl;
cout << "hex " << hex << l << endl;
}
int main(void)
{
char str[80];
strcpy(str, "0x20000");
print_str(str);
strcpy(str, "0x1abcd8765");
print_str(str);
return 1;
}
Its NOT homework!
Thanks,
/Ed
I'm trying to convert a string to an unsigned long long int.
% /tmp/long
str 0x20000
base 16
decimal 131072
hex 20000
str 0x1abcd8765
base 10
decimal ffffffff
hex ffffffff
But when the value is > 2^32 "cout" does not cooperate.
% gcc -v
Reading specs from /usr/lib/gcc-lib/i386-redhat-linux/2.96/specs
gcc version 2.96 20000731 (Red Hat Linux 7.1 2.96-98)
% cat long.cc
#include <iostream>
#include <string.h>
#include <stdlib.h>
void print_str(char *str)
{
int base;
long long int l;
base = (strncasecmp(str, "0x", 2) == 0) ? 16 : 10;
l = strtoul(str, NULL, base);
cout << "str " << str << endl;
cout << "base " << base << endl;
cout << "decimal " << l << endl;
cout << "hex " << hex << l << endl;
}
int main(void)
{
char str[80];
strcpy(str, "0x20000");
print_str(str);
strcpy(str, "0x1abcd8765");
print_str(str);
return 1;
}
Its NOT homework!
Thanks,
/Ed