hex to int

B

Bill Cunningham

I have written this small program and I must admit I'm a little stuck.
Input should be a hex number like fff or 0xfff and convert it to the decimal
equivalent. The output I get is always 0.

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char **argv)
{
if (argc != 2) {
puts("hex usage error");
exit(EXIT_FAILURE);
}
int x = strtol(argv[1], NULL, 10);
printf("%i\n", x);
return 0;
}

Am I missing a std function here that could do this for me? I know error
checking is not present.

Bill
 
B

Bill Cunningham

Bill Cunningham said:
I have written this small program and I must admit I'm a little stuck.
Input should be a hex number like fff or 0xfff and convert it to the
decimal equivalent. The output I get is always 0.

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char **argv)
{
if (argc != 2) {
puts("hex usage error");
exit(EXIT_FAILURE);
}
int x = strtol(argv[1], NULL, 10);
printf("%i\n", x);
return 0;
}

Am I missing a std function here that could do this for me? I know error
checking is not present.

Bill

I see that strtol returns a long. Could this be part of the problem?

Bill
 
J

Joachim Schmitz

Bill said:
I have written this small program and I must admit I'm a little
stuck. Input should be a hex number like fff or 0xfff and convert it
to the decimal equivalent. The output I get is always 0.

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char **argv)
{
if (argc != 2) {
puts("hex usage error");
exit(EXIT_FAILURE);
}
int x = strtol(argv[1], NULL, 10);

that 3rd argument should be 16
printf("%i\n", x);
return 0;
}

Am I missing a std function here that could do this for me? I know
error checking is not present.

http://linux.die.net/man/3/strtol
If there were no digits at all, strtol() stores the original value of nptr
in *endptr (and returns 0).
 
J

Joachim Schmitz

Bill said:
Bill Cunningham said:
I have written this small program and I must admit I'm a little
stuck. Input should be a hex number like fff or 0xfff and convert it
to the decimal equivalent. The output I get is always 0.

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char **argv)
{
if (argc != 2) {
puts("hex usage error");
exit(EXIT_FAILURE);
}
int x = strtol(argv[1], NULL, 10);
printf("%i\n", x);
return 0;
}

Am I missing a std function here that could do this for me? I know
error checking is not present.

Bill

I see that strtol returns a long. Could this be part of the
problem?

No. See my other post
 
M

Martin Ambuhl

Bill said:
I have written this small program and I must admit I'm a little stuck.
Input should be a hex number like fff or 0xfff and convert it to the decimal
equivalent. The output I get is always 0.
int x = strtol(argv[1], NULL, 10);
^^
The third argument is the base (in decimal( of the representation. The
base of a hexadecimal representation is 16, not 10.

Another issue is the use of NULL for the second argument. This is
usually a poor idea: you want to know the value of the first character
not converted. If it is not the one that should be there (often '\0' or
'\n'), you want to know that and do something about it.

Related to that is the failure to check errno (for which you need
<errno.h>) to see if it is set ERANGE. If the converted value will not
fit into a long int, you want to know it. That raises another issue,
the declaration of x to be an int. This is not a good idea. The value
returned by strtol may be a perfectly valid long int and not a valid
value for an int. So declare x a long int. You can always check that
it is in the range INT_MIN to INT_MAX if it must be stored in an int
later (you'll need <limits.h> for this),

You will, of course need to change your printf statement if you change
the type of x. Since I never use the specifier "%i", preferring to have
the base clear in the specifier, the two examples of change below
include replacing "%i" with "%d", but you need not do so.

1) Possible change 1: use the correct specifier a long int
printf("%ld\n", x);

2) Possible change 2: keep the specifier, but handle the long int
if (x >= INT_MIN && x <= INT_MAX)
printf("%d\n", (int)x);
else
printf("x (%ld) is out of range for an int.\n", x);
 
K

Keith Thompson

Bill Cunningham said:
I have written this small program and I must admit I'm a little stuck.
Input should be a hex number like fff or 0xfff and convert it to the decimal
equivalent. The output I get is always 0.

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char **argv)
{
if (argc != 2) {
puts("hex usage error");
exit(EXIT_FAILURE);
}
int x = strtol(argv[1], NULL, 10);
printf("%i\n", x);
return 0;
}

Am I missing a std function here that could do this for me? I know error
checking is not present.

If you don't have error checking and the program isn't working, it's
generally a good idea to add error checking.

But in this case the problem is fairly obvious. You want to convert
hexadecimal input. What base are you using?
 
B

Bill Cunningham

http://linux.die.net/man/3/strtol
If there were no digits at all, strtol() stores the original value of nptr
in *endptr (and returns 0).

OK I fixed the problem. I changed %i to %d (maybe not necessary) and
changed the third parameter of strtol to 16. Now if I enter fff or 0xfff I
get the decimal equivalent.

Bill
 
B

Bill Cunningham

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char **argv)
{
if (argc != 2) {
puts("hex usage error");
exit(EXIT_FAILURE);
}
long x = strtol(argv[1], NULL, 16);
printf("%d\n", x);
return 0;
}

I made these changes to the code. It seems to work right now. Might
there be anything else that I could do to make sure this code returns the
decimal equivalent of a hex no matter how big? I am not familiar with
limts.h and errno.h functions and macros. I've just never used them.

Bill
 
J

Joachim Schmitz

Bill said:
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char **argv)
{
if (argc != 2) {
puts("hex usage error");
exit(EXIT_FAILURE);
}
long x = strtol(argv[1], NULL, 16);
printf("%d\n", x);
return 0;
}

I made these changes to the code. It seems to work right now. Might
there be anything else that I could do to make sure this code returns
the decimal equivalent of a hex no matter how big? I am not familiar
with limts.h and errno.h functions and macros. I've just never used
them.

Look at the URL I poptsed eralier and here again:
http://linux.die.net/man/3/strtol
It contains a fully blown example, including error checking...

Bye, Jojo
 
J

Joachim Schmitz

Joachim said:
Bill said:
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char **argv)
{
if (argc != 2) {
puts("hex usage error");
exit(EXIT_FAILURE);
}
long x = strtol(argv[1], NULL, 16);
printf("%d\n", x);
return 0;
}

I made these changes to the code. It seems to work right now.
Might there be anything else that I could do to make sure this code
returns the decimal equivalent of a hex no matter how big? I am not
familiar with limts.h and errno.h functions and macros. I've just
never used them.

Look at the URL I poptsed eralier and here again:

Outch. 'posted earlier' of course. Yet again fingers faster than brain...
 
B

Barry Schwarz

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char **argv)
{
if (argc != 2) {
puts("hex usage error");
exit(EXIT_FAILURE);
}
long x = strtol(argv[1], NULL, 16);
printf("%d\n", x);
return 0;
}

I made these changes to the code. It seems to work right now. Might
there be anything else that I could do to make sure this code returns the
decimal equivalent of a hex no matter how big? I am not familiar with
limts.h and errno.h functions and macros. I've just never used them.

Your printf is still invoking undefined behavior. What type is
required for %d. What type is x?

Do you have a C99 compiler? If not, why do you have declarations
following executable statements? (Even if you do have C99, using
irrelevant C99 features like this limits the number of people who can
try to help you.)
 
B

Bill Cunningham

Your printf is still invoking undefined behavior. What type is
required for %d. What type is x?

%d is an int I can change it to %ld
Do you have a C99 compiler? If not, why do you have declarations
following executable statements? (Even if you do have C99, using
irrelevant C99 features like this limits the number of people who can
try to help you.)

It's gcc so it is c99 compatible but then I don't think it's totally c99
compatible.

Bill
 
M

Mark L Pappin

Bill Cunningham said:
%d is an int I can change it to %ld

Bill, if you insist on answering rhetorical questions then please
answer _all_ such questions levelled at you in a given post.

If you "can change it", why did you not do so prior to posting?
It's gcc so it is c99 compatible

That depends on what command-line arguments you drive it with.

What command-line arguments do you drive it with?
but then I don't think it's totally c99
compatible.

Don't "don't think" (although we can see you're very good at it).
Know, by Reading the Fu^Hine Manual.


You continue to aggravate those people who are trying to help you, by
failing to do the following:

1. Decide what problem you want to solve, or what output you want to
see for given input, in the program you're about to write. Leave
"making it more flexible" until after you've got the first version
working.

2. Read and understand the description in K&R2's Appendix for each
standard library function you decide to use. If you don't understand
how the function should be used, don't use it until you do (and gain
that understanding by asking specific questions about the aspects of
its use that you don't grok). Don't guess. Use the mechanisms
provided to check for anomalous conditions.

3. (As Barry said) Avoid C99-but-not-C90 features that provide no
significant benefit for your program, until full C99 compilers are
more widespread.

4. Compile your code with a compiler (and command-line arguments) that
gives you something as close as possible to a C90 or C99 compiler.

5. If you don't understand an error or warning message you get from
that compiler, post a query about the message, showing the message
itself, the full command-line you used, and the full source code that
triggers that message.

6. Modify your code to eliminate all error messages, and as many
warning messages as you can.

7. If you don't understand the runtime behaviour of your program, post
a query about the behaviour, showing the full command-line you used to
compile it, the full source code of your program, all messages
produced by the compiler, the full command-line you used to run your
program, all the input you supplied to it, and all the output it
gave. Describe precisely what you expected to see, and how what you
did see differs from that.

8. Judiciously follow suggestions given to you by posters here, and
provide feedback to let others know that you have done so.


If you start to do these things, two wonderful things will happen:
- you'll get more and better help
- you'll need less help

If you don't start to do these things, bad things (for you):
- people who can usefully help you will instead ignore you
- at best, you'll continue your current rate of improvement


mlp
 

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

Similar Threads

pointer to int error 24
Command Line Arguments 0
Incrementing hex address 5
How to try a range of hex values in C# code ? 0
Hex to int 25
string to int 5
hex error 61
Linux: using "clone3" and "waitid" 0

Members online

No members online now.

Forum statistics

Threads
473,954
Messages
2,570,116
Members
46,704
Latest member
BernadineF

Latest Threads

Top