473,395 Members | 1,456 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

computing hexnumbers

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?
Feb 1 '06 #1
10 2197

"Markus Pitha" <ma****@pithax.net> wrote in message
news:e8***************************@news.chello.at. ..
: 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
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Feb 1 '06 #2
Hello,

Ivan Vecerina wrote:
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.
Feb 1 '06 #3

"Markus Pitha" <ma****@pithax.net> wrote in message
news:bc***************************@news.chello.at. ..
Hello,

Ivan Vecerina wrote:
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);

Feb 1 '06 #4
"Markus Pitha" <ma****@pithax.net> wrote in message
news:bc***************************@news.chello.at. ..
Hello,

Ivan Vecerina wrote:
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!

Feb 1 '06 #5
Hello,

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

Markus
Feb 1 '06 #6
Markus Pitha wrote:
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
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Feb 1 '06 #7
In article <43**********************@news.inteliport.com>,
Mark B <so***@localbar.com> wrote:
"Markus Pitha" <ma****@pithax.net> wrote in message
news:bc***************************@news.chello.at ...
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).


Only if you are using ASCII or similar character sets. It's completely
different in EBCDIC for example.
--
I was very young in those days, but I was also rather dim.
-- Christopher Priest
Feb 1 '06 #8

"Markus Pitha" <ma****@pithax.net> wrote in message
news:e8***************************@news.chello.at. ..
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
Feb 2 '06 #9
Markus Pitha <ma****@pithax.net> wrote:
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)?

....
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.
Feb 2 '06 #10
Hello,

thank you all for your tips, but in the end I think that "strtol" is the
best and safest choice.
Markus.
Feb 2 '06 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Andy | last post by:
First of all, can I reassure you that this posting is NOT spam and not market research. Please feel free to email me at the address below to check/validate this message prior to doing the survey if...
39
by: Steven T. Hatton | last post by:
I came across this while looking for information on C++ and CORBA: http://www.zeroc.com/ice.html. It got me to wondering why I need two different languages in order to write distributed computing...
0
by: Andy | last post by:
First of all, can I reassure you that this posting is NOT spam and not market research. Please feel free to email me at the address below to check/validate this message prior to doing the survey if...
0
by: natty2006 | last post by:
Submission Deadline extended: 13 November 2006 ************************************************************* IADIS INTERNATIONAL CONFERENCE APPLIED COMPUTING 2007 February 17-20, 2007 -...
13
by: Xah Lee | last post by:
Today, a motherfucker Christophe Rhodes (aka Xof in irc://chat.freenode.net/lisp ) kicked banned me. Here's the few relevant excerpt. (full, unedited excerpt will be published if there is a public...
14
by: Aaron Watters | last post by:
So, in between skiing runs I noticed a Business Week cover story on "cloud computing". The article had lots of interesting information in it like about how somebody's mom used to be an airline...
0
by: knorth | last post by:
The DataServices World conference offers a program for those interested in technology for data integration and data access, with emphasis on service-oriented architecture (SOA), web-oriented...
3
by: Ty Oft | last post by:
Hello Bytes! Maybe my question seems a bit silly (as one could simply answer it "Well, what are you more passionate about?" or stuff like that - please don't answer like this), but I am in a bit...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.