pack FF in a unsigned char ?

A

archilleswaterland

hello,

is there any way that I can represent FF (11111111) as an unsigned char
?

unsigned char ch;
so if the user enters FF
and i have printf("%c",ch);
it should spit out FF

thanks
 
J

Joona I Palaste

(e-mail address removed) scribbled the following:
is there any way that I can represent FF (11111111) as an unsigned char
?
unsigned char ch;
so if the user enters FF
and i have printf("%c",ch);
it should spit out FF

Numbers are numbers are numbers. They don't carry information about how
they are represented when written down. So, what you ask can't be done
as easily like that. You have to use the %x specifier with scanf() when
entering the number in and %x with printf() when printing it out.
 
E

Emmanuel Delahaye

hello,

is there any way that I can represent FF (11111111) as an unsigned char
?

unsigned char c = 0xFF;
unsigned char ch;
so if the user enters FF

fgets() + strtoul() (base 16)
and i have printf("%c",ch);
it should spit out FF

You want the "%X" formatter.

printf ("%X", (unsigned) ch);

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

"Clearly your code does not meet the original spec."
"You are sentenced to 30 lashes with a wet noodle."
-- Jerry Coffin in a.l.c.c++
 
D

dot

hello,

is there any way that I can represent FF (11111111) as an unsigned char
?

unsigned char ch;
so if the user enters FF
and i have printf("%c",ch);
it should spit out FF

No.
 
D

dot

Numbers are numbers are numbers. They don't carry information about how
they are represented when written down. So, what you ask can't be done
as easily like that. You have to use the %x specifier with scanf() when
entering the number in and %x with printf() when printing it out.

And to prevent overflows he has to store it as something other than an
unsigned char (0 - FF).
 
S

Steven K. Mariner

To answer the question you actually asked, 0xff already fits in an
unsigned char data type.

However, to display it, you'll need to using putc(), fputc(), or
putchar(), assuming your printf() implementation doesn't support "%uc"
the way you want (or if you're looking for portability, I suppose).

unsigned char ch;
/* user enters a 0xff somehow into ch */
putchar(ch); /* because printf("%uc",ch); didn't do what I wanted
*/
 
O

Old Wolf

Steven said:
To answer the question you actually asked, 0xff already fits in an
unsigned char data type.

However, to display it, you'll need to using putc(), fputc(), or
putchar(), assuming your printf() implementation doesn't support
"%uc" the way you want (or if you're looking for portability,
I suppose).

There is no such thing as "%uc". What are you expecting it to do?
unsigned char ch;
/* user enters a 0xff somehow into ch */
putchar(ch);

I doubt that your system will output "FF" when you execute that code.
It will actually display character number 255 in your execution
character set (if that character is displayable).
 
B

Barry Schwarz

hello,

is there any way that I can represent FF (11111111) as an unsigned char
?

unsigned char ch = 0xff;

If your compiler issues an obnoxious warning about storing an int in a
char, then change it to (char)0xff
unsigned char ch;
so if the user enters FF

If the user enters FF, then you will need to use one of the scanf
functions.
and i have printf("%c",ch);
it should spit out FF

%c will print only one character.

You can use the %X format specifier.


<<Remove the del for email>>
 
R

Richard Bos

And to prevent overflows he has to store it as something other than an
unsigned char (0 - FF).

No, he doesn't. He wants the user to be able to enter FF, and read (and
redisplay) this as the hex value FF, which is representable in unsigned
char in all implementations.

Richard
 
S

Steven K. Mariner

From: "Old WOlf said:
> [...]
> There is no such thing as "%uc".

Not in the standard, which is why I warned him -- twice -- that even
it if works on his compiler it's not the "right" answer, and then
proceeded to give him the "right" answer.
> What are you expecting it to do?

Where I've seen it implemented, it displayed the character, even if it
was in the extended-ASCII set, as a single character.

Where it was not implemented, I think it has always generated a
diagnostic. It's been a long time since I've tried to use it.
>
> I doubt that your system will output "FF" when you execute
> that code.

The OP did not ask for "FF" to be displayed.
The OP asked for FF to be displayed.

The absence of quotes suggests to me that he wants character number
255 from the character set to be displayed, not the literal string
"FF".
> It will actually display character number 255 in your
> execution character set (if that character is displayable).

You sure went a long way around the barn to agree with me.

_________________
Steven K. Mariner
(e-mail address removed)
http://home.earthlink.net/~marinersk/
http://www.whirlyjigmusic.com/
 
D

dot

No, he doesn't. He wants the user to be able to enter FF, and read (and
redisplay) this as the hex value FF, which is representable in unsigned
char in all implementations.

You're right... My mistake. Sorry.
 
D

dot

Why do you have your postings set for non-archival? The information
you provide could be useful for years to come.

Just curious.

Long story... not really on topic here.

Suffice it to say, with a 15 year stalker on one's case, one takes
precautions about what one puts in public places.
 

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,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top