Inverse of printf %x format

J

John L.

I've seen several postings asking this, but no simple, clear answer. My
C language reference book is deficient in this area.

If I

short i = 0;
printf(">%2x<",i);

I might resonably expect the following output:

Now, what format code (%?? below) will restore i's value? e.g.

short i;
char s[5];
strcpy(s,"0000");
sprintf((char *)&i,"%??",s);
if (i==0) printf(">Hexed again<");

Produces:
>Hexed again<

Thanks in advance
 
I

infobahn

John L. said:
I've seen several postings asking this, but no simple, clear answer. My
C language reference book is deficient in this area.

Then get a better C book. Kernighan and Ritchie's "The C Programming
Language", 2nd edition, mentions the functions you need on pp251-252.
 
M

Michael Mair

John said:
I've seen several postings asking this, but no simple, clear answer. My
C language reference book is deficient in this area.

If I

short i = 0;
printf(">%2x<",i);

Note that %x refers to unsigned integer types.
I might resonably expect the following output:

No. At least not reasonably. With a field width of two, I would
expect
>00<

Now, what format code (%?? below) will restore i's value? e.g.

short i;
char s[5];
strcpy(s,"0000");
sprintf((char *)&i,"%??",s);

This is crap. What you want to have is conversion of a string
into a number, not conversion of a string into a string.
So, have a look into your reference book and look at strtol,
which can convert strings representing numbers in a base to be
specified by you into long int values.
Be careful to check for errors and use a long int variable to
store the value (and check it against SHORT_MAX) before assigning
the value to i.
if (i==0) printf(">Hexed again<");

Produces:


Thanks in advance

Cheers
Michael
 
G

Goran Larsson

John L. said:
short i = 0;
printf(">%2x<",i);

I might resonably expect the following output:

Why is that output resonable? I would expect the output to be > 0<.
short i;
char s[5];
strcpy(s,"0000");
sprintf((char *)&i,"%??",s);

Do you expect to get a shredded document back by feeding it to the
document shredder a second time? You need the reverse of sprintf,
i.e. sscanf.
 
M

Martin Ambuhl

John said:
I've seen several postings asking this, but no simple, clear answer. My
C language reference book is deficient in this area.

If I

short i = 0;
printf(">%2x<",i);

I might resonably expect the following output:

No, you couldn't. Why did you put a '2' into the specifier if you
expected four digits to be printed? Why did you leave out the '0' flag
if you expected leading zeros to be printed?
Now, what format code (%?? below) will restore i's value? e.g.

short i;
char s[5];
strcpy(s,"0000");
sprintf((char *)&i,"%??",s);
if (i==0) printf(">Hexed again<");

Produces:
Hexed again<

The above makes no sense in C or in English. Perhaps the following
addresses your real question;

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

int main(void)
{
short i = 0;
char s[5];
printf(">%2x<\n", i);
printf("With the correct specifier for OP's"
" \"expected\" output\n"
">%04x<\n", i);

i = 42;
strcpy(s, "0000");
i = strtol(s, 0, 16);
if (!i)
printf(">Hexed again<\n");
return 0;
}

[Output]
With the correct specifier for OP's "expected" output
 
M

Martin Ambuhl

Michael said:
Note that %x refers to unsigned integer types.



No. At least not reasonably. With a field width of two, I would
expect

You shouldn't. You *should* expect > 0<
 
M

Michael Mair

Martin said:
You shouldn't. You *should* expect > 0<

You are right. Realised it throughout the afternoon but thought
"one of the clc crowd will correct me, no need to run for the
computer..." ;-)
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top