473,326 Members | 2,175 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,326 software developers and data experts.

Inverse of printf %x format

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:
0000<
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

Nov 14 '05 #1
6 4047
"John L." wrote:

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.
Nov 14 '05 #2
John L. wrote:
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:
>0000<

No. At least not reasonably. With a field width of two, I would
expect00<
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:
>Hexed again<


Thanks in advance


Cheers
Michael

--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Nov 14 '05 #3
In article <11**********************@o13g2000cwo.googlegroups .com>,
John L. <la********@juno.com> wrote:
short i = 0;
printf(">%2x<",i);

I might resonably expect the following output:
>0000<

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.

--
Göran Larsson http://www.mitt-eget.com/
Nov 14 '05 #4
John L. wrote:
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:
>0000<

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] 0< With the correct specifier for OP's "expected" output0000<
Hexed again<



--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
Nov 14 '05 #5
Michael Mair wrote:
John L. wrote:
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:
>0000<

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


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


--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
Nov 14 '05 #6
Martin Ambuhl wrote:
Michael Mair wrote:
John L. wrote:
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:

>0000<


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

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..." ;-)
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Nov 14 '05 #7

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

Similar topics

11
by: Steven T. Hatton | last post by:
OK, I take back the nasty stuff I said about printf. At least until I see a similarly concise way of writing this using C++ formatters. I want to convert the following to something I can put onto...
6
by: vishnu mahendra | last post by:
hello to all, can any one please give me an algorithm to find inverse of a matrix of order n rows and m columns. thank you in advance, vishnu.
11
by: Grumble | last post by:
Hello, I have the following structure: struct foo { char *format; /* format string to be used with printf() */ int nparm; /* number of %d specifiers in the format string */ /* 0 <= nparm <=...
7
by: teachtiro | last post by:
Hi, 'C' says \ is the escape character to be used when characters are to be interpreted in an uncommon sense, e.g. \t usage in printf(), but for printing % through printf(), i have read that %%...
7
by: a.dheeraj.kumar | last post by:
i know that there is a function to find the logarithm of a number, sin, cos, tan etc. but are there which can find sin^-1, cos^-1, tan^-1 and antilog of a given number? PS: sin^-1 means sin...
3
by: nandh_dp | last post by:
When the below program is compiled and executed, #include <stdio.h> #define MASK 0xFFFFFFULL main() { unsigned long long a; unsigned long b, c;
4
by: Jonathan Fine | last post by:
Hello As part of the MathTran project I found myself wanting to maintain a bijection between long names and short names. http://www.open.ac.uk/mathtran In other words, I wanted to have two...
0
by: DarrenWeber | last post by:
# Copyright (C) 2007 Darren Lee Weber # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free...
3
by: google | last post by:
Consider the following code: char str; char str2; strcpy(str, "%alfa% %beta% d%100%d %gamma% %delta%"); printf("printf: "); printf("1%s2", str); printf("\nsprintf: "); sprintf(str2, "1%s2",...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.