473,386 Members | 1,810 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,386 software developers and data experts.

printf conversion problem

hi,

i just wanted to print out
some unsigned long int values in hexadecimal,
printing out one value works, but not byte by byte.
anybody has a suggestinon what my problem is?

this is my program:
#include <stdio.h>
#include <stdlib.h>
#include <arpa/inet.h>

#include <stdio.h>
int main (void) {
unsigned long testint = 6070;
unsigned long bigend = htonl(testint);
char *le = (char *) &testint;
char *be = (char *) &bigend;

printf("HO: %08X\n", testint);
printf("NO: %08X\n", bigend);

printf("HO: %02X %02X %02X %02X\n", le[0], le[1], le[2], le[3]);
printf("NO: %02X %02X %02X %02X\n", be[0], be[1], be[2], be[3]);

}

and this is my output:

$ ./test
HO: 000017B6
NO: B6170000
HO: FFFFFFB6 17 00 00
NO: 00 00 17 FFFFFFB6

anybody can tell me why i have that FFFFFFB6 Values in the output wiht
%02X ?

buzz
Nov 14 '05 #1
3 2274
On Sun, 26 Sep 2004 16:54:46 +0200, buzzdee
<re******@fh-brandenburg.de> wrote in comp.lang.c:

C has three character types, signed char, unsigned char, and 'plain'
char, the type you get when you don't specify signed or unsigned.
'Plain' char is the same as either signed or unsigned char, but it is
up to the implementation which it is.

In your case, 'plain' char is signed. The value 0xb6 in a char on
your implementation has the value -74. When you pass the value of
that char to printf(), it is converted to an int with the value -74,
which is represented in your implementation as 0xffffffb6.
hi,

i just wanted to print out
some unsigned long int values in hexadecimal,
printing out one value works, but not byte by byte.
anybody has a suggestinon what my problem is?

this is my program:
#include <stdio.h>
#include <stdlib.h>
#include <arpa/inet.h>

#include <stdio.h>
int main (void) {
unsigned long testint = 6070;
unsigned long bigend = htonl(testint);
char *le = (char *) &testint;
char *be = (char *) &bigend;
Change the types, and the casts, of 'le' and 'be' to unsigned char,
instead of just 'plain' char.

printf("HO: %08X\n", testint);
printf("NO: %08X\n", bigend);

printf("HO: %02X %02X %02X %02X\n", le[0], le[1], le[2], le[3]);
printf("NO: %02X %02X %02X %02X\n", be[0], be[1], be[2], be[3]);

}

and this is my output:

$ ./test
HO: 000017B6
NO: B6170000
HO: FFFFFFB6 17 00 00
NO: 00 00 17 FFFFFFB6

anybody can tell me why i have that FFFFFFB6 Values in the output wiht
%02X ?

buzz


--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 14 '05 #2
On Sun, 26 Sep 2004 16:54:46 +0200
buzzdee <re******@fh-brandenburg.de> wrote:
hi,

i just wanted to print out
some unsigned long int values in hexadecimal,
printing out one value works, but not byte by byte.
anybody has a suggestinon what my problem is?

this is my program:
#include <stdio.h>
#include <stdlib.h>
#include <arpa/inet.h>

#include <stdio.h>
int main (void) {
unsigned long testint = 6070;
unsigned long bigend = htonl(testint);
char *le = (char *) &testint;
char *be = (char *) &bigend; Try:
unsigned char *le = (unsigned char *) &testint;
unsigned char *be = (unsigned char *) &bigend;
printf("HO: %08X\n", testint);
printf("NO: %08X\n", bigend);
According to the man page (my copy of K&R is in the office) %X expects
an unsigned in not an unsigned long. You want
printf("HO: %08lX\n", testint);
printf("NO: %08lX\n", bigend);
You are also assuming that unsigned long is 32 bits, which is not
guaranteed by the standard. This is OK if you want at least 32 bits (I
think), but if you want exactly 32 bits (which from using the
non-standard htonl I guess you do) you might want to use a typedef which
you can set to the appropriate type on each system, including using the
exact width types that are optional in C99 if you use any C99
implementations,<OT> and the POSIX ones if you use POSIX systems.
Why couldn't C99 have adopted the POSIX fixed width types?</OT>
printf("HO: %02X %02X %02X %02X\n", le[0], le[1], le[2],
le[3]);
printf("NO: %02X %02X %02X %02X\n", be[0], be[1],
be[2], be[3]);
You are assuming that CHAR_BIT==8 and sizeof unsigned int == 4. This
might be true for the systems you care about, but neither is always
true.
}

and this is my output:

$ ./test
HO: 000017B6
NO: B6170000
HO: FFFFFFB6 17 00 00
NO: 00 00 17 FFFFFFB6

anybody can tell me why i have that FFFFFFB6 Values in the output wiht
%02X ?


On your system char is obviously signed and your system uses 2s
complement, so it gets promoted to an int using signed extension, i.e.
filling the extra bits with what the sign bit was.
--
Flash Gordon
Sometimes I think shooting would be far too good for some people.
Although my email address says spam, it is real and I read it.
Nov 14 '05 #3
buzzdee wrote:
hi,

i just wanted to print out
some unsigned long int values in hexadecimal,
printing out one value works, but not byte by byte.
anybody has a suggestinon what my problem is?

this is my program:
#include <stdio.h>
#include <stdlib.h>
#include <arpa/inet.h>

#include <stdio.h>
int main (void) {
unsigned long testint = 6070;
unsigned long bigend = htonl(testint);
char *le = (char *) &testint;
char *be = (char *) &bigend;

printf("HO: %08X\n", testint);
printf("NO: %08X\n", bigend);

printf("HO: %02X %02X %02X %02X\n", le[0], le[1], le[2], le[3]);
printf("NO: %02X %02X %02X %02X\n", be[0], be[1], be[2], be[3]);

}

and this is my output:

$ ./test
HO: 000017B6
NO: B6170000
HO: FFFFFFB6 17 00 00
NO: 00 00 17 FFFFFFB6

anybody can tell me why i have that FFFFFFB6 Values in the output wiht
%02X ?


Because you have signed chars:
#include <stdio.h>
#include <stdlib.h>

#include <stdio.h>
int main(void)
{
unsigned long testint = 6070;
unsigned long bigend = 0xb6170000;
unsigned char *le = (unsigned char *) &testint; /* mha: fixed types
*/
unsigned char *be = (unsigned char *) &bigend; /* mha: fixed types
*/

printf("HO: %08lX\n", testint); /* mha: fixed specifier */
printf("NO: %08lX\n", bigend); /* mha: fixed specifier */
if (sizeof(unsigned long) < 4) {
fprintf(stderr, "Damn!\n");
exit(EXIT_FAILURE);
}
printf("HO: %02X %02X %02X %02X\n", le[0], le[1], le[2], le[3]);
printf("NO: %02X %02X %02X %02X\n", be[0], be[1], be[2], be[3]);
return 0;
}

[output]

HO: 000017B6
NO: B6170000
HO: B6 17 00 00
NO: 00 00 17 B6
Nov 14 '05 #4

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

Similar topics

188
by: infobahn | last post by:
printf("%p\n", (void *)0); /* UB, or not? Please explain your answer. */
12
by: John Devereux | last post by:
Hi, I would like to know if their is a conversion specifier, or other method, that will allow me to not use particular arguments to printf. Failing that, is it permissable to simply "not use"...
16
by: Jordan Abel | last post by:
I have written this function and was wondering if anyone else could point out if there's anything wrong with it. The purpose is to substitute for printf when in a situation where low-level I/O has...
8
by: Gregc. | last post by:
Hi Sorry to bother you gusy with such a basic question, but I am working on a conversion program. I've got most of it down, but I am having trouble with the output: *Dollar Conversion...
19
by: v4vijayakumar | last post by:
why the following statement dumps the core(Segmentation fault)? printf("%s\n", __FILE__);
3
by: pranab.salian | last post by:
I need to compile some newer code in Borland TC 3.0. Here's the snippet.. /* CODE */ /* // --------------------------------------------------------------- // Shift register implementation:...
10
by: lovecreatesbeauty | last post by:
Is parameter type conversion required for the 2nd argument on printf("%p", (void *)&i); ? But one would never call memcpy like: memcpy((void *)pi, (void *)pj, sizeof *pj); /*memcpy((void *)pi,...
4
by: marsarden | last post by:
code below: #include <stdio.h> int main(void) { char *aaa={"a","b","c"}; printf("%S\n",*aaa); /*the uppercase character S*/ getchar();
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",...
15
by: Andreas Eibach | last post by:
.... but I have an unsigned long value in the printf. This warning came when I used gcc 4.x to compile. .... unsigned long offset = 0; .... Well OK, an "easy" way would be instead of printf...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.