I
Ivan Vecerina
: Hello,
:
: When I want to read hexnumbers from stdin and use them for a
: calculation, is there a better way instead of reading every single char
: (eg. a2d3f3a1), converting it to decimal, make the calculation, and
: reconverting it into hex, whereby I could check the validation of every
: "digit" (so 0-9,A-F)?
: I thought about something with the %x specifier and fgets, but with this
: method I still have to check if the input is valid (if there are only
: "digits" from 0-9 or a-f).
With scanf, you can add a %c after the %x to see which character stopped
the parsing of the hex number.
You may also first read a string, then use strtol -- which will return
a pointer to the position at which the parsing of the number stopped.
: Moreover I don't know how to manage that yet.
: I can only read strings from stdin, but what can I do with this string?
: Convert it to double? I must do anything wrong, because it writes _only_
: the first digit of my input to stdout, when I use something like that:
:
: int main(int argc, char **argv) {
: char hex1[8];
: .
: .
: strcpy(hex1, argv[1]);
: .
: .
: (double *)hex1;
This pointer cast does not make any kind of conversion.
: printf("%x\n", *hex1);
: What's the most efficient way to add or subtract hexnumbers?
The easiest is to convert the hex string(s) to integer(s),
to subtract those, and to print the result.
unsigned long a,b;
scanf("%lx",&a);
scanf("%lx",&b);
if( a<b )
{ /* handle as you see fit */ }
unsigned long d = a-b;
printf("%lx\n",d);
hth -Ivan
:
: When I want to read hexnumbers from stdin and use them for a
: calculation, is there a better way instead of reading every single char
: (eg. a2d3f3a1), converting it to decimal, make the calculation, and
: reconverting it into hex, whereby I could check the validation of every
: "digit" (so 0-9,A-F)?
: I thought about something with the %x specifier and fgets, but with this
: method I still have to check if the input is valid (if there are only
: "digits" from 0-9 or a-f).
With scanf, you can add a %c after the %x to see which character stopped
the parsing of the hex number.
You may also first read a string, then use strtol -- which will return
a pointer to the position at which the parsing of the number stopped.
: Moreover I don't know how to manage that yet.
: I can only read strings from stdin, but what can I do with this string?
: Convert it to double? I must do anything wrong, because it writes _only_
: the first digit of my input to stdout, when I use something like that:
:
: int main(int argc, char **argv) {
: char hex1[8];
: .
: .
: strcpy(hex1, argv[1]);
: .
: .
: (double *)hex1;
This pointer cast does not make any kind of conversion.
: printf("%x\n", *hex1);
: What's the most efficient way to add or subtract hexnumbers?
The easiest is to convert the hex string(s) to integer(s),
to subtract those, and to print the result.
unsigned long a,b;
scanf("%lx",&a);
scanf("%lx",&b);
if( a<b )
{ /* handle as you see fit */ }
unsigned long d = a-b;
printf("%lx\n",d);
hth -Ivan