Printing an IP address...HELP! 
Author Message
 Printing an IP address...HELP!

Quote:
>-----Original Message-----

>Sent: 11 May 2000 00:14

>Subject: Re: Printing an IP address...HELP!



><...>
>>Globals::QUERY_DIR.c_str(),

>This is not a C program (C has no :: operator)
>Try our neighbours:
>comp.lang.c++

But was not the question about sprintf and a problem printing signed char as
unsigned?

That's surely an ISO/ANSI C question?

I had a similar problem myself a while back, and no matter which permutation
of casts I used, still printed char's as signed instead of unsigned. In the
end I assigned the char to int, messed with it, and used that instead.

Anyway, I waffle. If you don't want to answer 'Befuddle's question because
of some unrelated (to the question) code, could you answer the following
please...

How do I get the following to print 255 (the unsigned value) instead of
-1(the signed value) on a system that uses signed char?
Assume that declaring c as unsigned isn't an option since (like the OP case)
it's in a library somewhere, or compiler dependant (Why didn't ISO/ANSI
specify this - anyone?).

*Can* it be done with casts or does it have to be a work-around?

$ list test.c
#include <stdio.h>
#include <stdlib.h>

int main(void){
    char c=-1;

    printf("%d\n", (unsigned)c); /* the cast won't work here */

    return EXIT_SUCCESS;

Quote:
}

$ test
-1

One final (OT) question - why do (certain) people in this group continue to
(only) pull posts apart wrt non standard stuff, instead of answering the
(on-topic (and?) within standard) questions? Granted, that in this case the
OP included some C++ code in there, but the question was (IMHO) definatly C
related.

--
PJH

Semper Ubi Sub Ubi

 Sent via Deja.com http://www.*-*-*.com/
 Before you buy.



Sat, 02 Nov 2002 03:00:00 GMT  
 Printing an IP address...HELP!

Quote:

> How do I get the following to print 255 (the unsigned value) instead of
> -1(the signed value) on a system that uses signed char?=20

No. You _can_ get it to print UCHAR_MAX, but this need not be 255 (but
on your system probably is, seeing that you blithely assume that it will
be).

Quote:
> Assume that declaring c as unsigned isn't an option since (like the OP case=
> )
> it's in a library somewhere, or compiler dependant (Why didn't ISO/ANSI
> specify this - anyone?).=20

Because ISO C was designed to work on real computers as well as on toys
that don't consider whether char can be anything but 8 bits. Consider
Unicode systems, for example.

Quote:
> *Can* it be done with casts or does it have to be a work-around?

> $ list test.c
> #include <stdio.h>
> #include <stdlib.h>

> int main(void){
>     char c=3D-1;

WTB is that 3D doing there?

Quote:
>     printf("%d\n", (unsigned)c); /* the cast won't work here */

Yes, it does: it converts -1 into (unsigned)-1, which is UINT_MAX, which
is then printed as a signed int, which makes it -1 again.

You must cast c to unsigned char to get UCHAR_MAX, and then print it
using %u, not %d. The latter part is only strictly necessary on systems
where sizeof(int)==sizeof(char), but doing so anyway a) is more
portable, b) gives the correct semantical hint to the reader and )c
suggests that you know what you are doing.

  printf("%u\n", (unsigned char)c);

Quote:
>     return EXIT_SUCCESS;
> }

> $ test
> -1

255 (because on my system, UCHAR_MAX also happens to be 255.)

Richard



Sun, 03 Nov 2002 03:00:00 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Printing an IP address...HELP!

2. IP address and Mac Address help!!!

3. getting ip address of the default route interface (public ip vs private ip)

4. Trying to print IP address of socket connection

5. How to print to a printer using just it's IP address

6. How to print to a printer using just it's IP address

7. How to get MAC address from IP Address

8. convert mac address to ip address

9. How can i get MAC Address by IP address in SCO UNIX C

10. MAC Address from IP Address

11. IP Address Linking Errors - help!!!!!

12. Need HELP on searching all IP address in Local Network

 

 
Powered by phpBB® Forum Software