computing hexnumbers

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
 
M

Markus Pitha

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). 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;
printf("%x\n", *hex1);

What's the most efficient way to add or subtract hexnumbers?
 
P

pemo

Markus Pitha said:
Hello,

Ivan said:
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.

Good, I tried it, but the output is wrong:

int main(int argc, char **argv) {
char hex1[8];
long int ans = 0;
.
.
strcpy(hex1, argv[1]);
ans = strtol(hex1, NULL, 16);
printf("%ld\n", ans);

When I read "A" from stdin, or even "0xA", I get 10 as output, and
that's wrong. "A" is 41 in decimal.

dec hex
0 0
1 1
2 2
3 3
4 4
5 5
6 6
7 7
8 8
9 9
10 A
11 B
12 C
13 D
....
41 29
unsigned long a,b;
scanf("%lx",&a);
scanf("%lx",&b);

The problem is that I don't know how to use this, when I get the
argument, a string, from argv[1]?

sscanf(argv[1],"%lx",&a);
 
M

Mark B

Markus Pitha said:
Hello,

Ivan said:
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.

Good, I tried it, but the output is wrong:

int main(int argc, char **argv) {
char hex1[8];
long int ans = 0;
.
.
strcpy(hex1, argv[1]);
ans = strtol(hex1, NULL, 16);
printf("%ld\n", ans);

When I read "A" from stdin, or even "0xA", I get 10 as output, and
that's wrong. "A" is 41 in decimal.

No, it's 0x41 in hexadecimal (base 16) which is 65 in decimal (base 10).

You wanna take another guess as to what 0xA is in 'hexadecimal' ?
Clue: 0123456789ABCDEF
Need another hint? Just look at your output!
 
M

Markus Pitha

Hello,

Ivan said:
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.

Good, I tried it, but the output is wrong:

int main(int argc, char **argv) {
char hex1[8];
long int ans = 0;
..
..
strcpy(hex1, argv[1]);
ans = strtol(hex1, NULL, 16);
printf("%ld\n", ans);

When I read "A" from stdin, or even "0xA", I get 10 as output, and
that's wrong. "A" is 41 in decimal.

unsigned long a,b;
scanf("%lx",&a);
scanf("%lx",&b);

The problem is that I don't know how to use this, when I get the
argument, a string, from argv[1]?




Markus.
 
M

Michael Mair

Markus said:
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). 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;
printf("%x\n", *hex1);

What's the most efficient way to add or subtract hexnumbers?

A remark concerning other replies:
- use scanf() family functions always like
if (expected_number_of_conversions != sscanf(....))
{ /*error handling*/ }
- Consider using strtoul() instead of sscanf() as it
allows for better identifying of errors and error types.

Cheers
Michael
 
M

Markus Pitha

Hello,

I recognized that I mixed it up with the ASCII code table, thanks.



Markus
 
W

Walter Roberson

No, it's 0x41 in hexadecimal (base 16) which is 65 in decimal (base 10).

Only if you are using ASCII or similar character sets. It's completely
different in EBCDIC for example.
 
R

Rod Pemberton

Markus Pitha said:
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).

You could use strtok() to separate the input into valid numeric and
non-numeric tokens. Pass the alphabet, without a-fA-F, in both upper and
lower case to strtok(). Then, you can use strchr() to determine if the
number is hex or decimal. You can search for 'a-f', 'A-F' or 'x' and 'X'
for hex. Once, you know whether the number is hex or decimal, you can use
scanf() to convert the number from string to integer.

One routine below shows how to use strtok() and scanf(). The other routine
below shows how to use strchr() and scanf(). This should be enough to get
you onto the correct track.

unsigned __int64 strtoull(char *s, char **endp, int base)
{
unsigned __int64 value;
if(strchr(s,'x')==NULL&&strchr(s,'X')==NULL)
sscanf(s,"%Lu",&value);
else
sscanf(s,"%Lx",&value);

return(value);
}

unsigned long iptoul(char *ip)
{
char i,*tmp;
unsigned long val=0, cvt;

tmp=strtok(ip,".");
for (i=0;i<4;i++)
{
sscanf(tmp,"%lu",&cvt);
val<<=8;
val|=(unsigned char)cvt;
tmp=strtok(NULL,".");
}
return(val);
}



Rod Pemberton
 
R

russell kym horsell

Markus Pitha said:
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)?
....


The routine long strtol(const char*,char**,int) can convert strings in
various bases to ints.
It returns a pointer to the first non-digit char (for the base).
If this pointer ends up on whitespace or NUL then maybe fine -- you
haven't hit an asterisk or sommin. But make sure the "end pointer"
has actually moved from the starting position.
 
M

Markus Pitha

Hello,

thank you all for your tips, but in the end I think that "strtol" is the
best and safest choice.


Markus.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,149
Messages
2,570,842
Members
47,388
Latest member
EarthaGilm

Latest Threads

Top