Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > pack FF in a unsigned char ?

Reply
Thread Tools

pack FF in a unsigned char ?

 
 
archilleswaterland@hotmail.com
Guest
Posts: n/a
 
      03-28-2005
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

 
Reply With Quote
 
 
 
 
Joona I Palaste
Guest
Posts: n/a
 
      03-28-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) scribbled the following:
> 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


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.

--
/-- Joona Palaste ((E-Mail Removed)) ------------- Finland --------\
\-------------------------------------------------------- rules! --------/
"To doo bee doo bee doo."
- Frank Sinatra
 
Reply With Quote
 
 
 
 
Emmanuel Delahaye
Guest
Posts: n/a
 
      03-28-2005
(E-Mail Removed) wrote on 28/03/05 :
> 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++

 
Reply With Quote
 
dot@dot.dot
Guest
Posts: n/a
 
      03-28-2005
On 28 Mar 2005 00:58:18 -0800, (E-Mail Removed) wrote:

>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.


 
Reply With Quote
 
dot@dot.dot
Guest
Posts: n/a
 
      03-28-2005
On 28 Mar 2005 09:41:07 GMT, Joona I Palaste <(E-Mail Removed)> wrote:

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

>
>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).

 
Reply With Quote
 
Steven K. Mariner
Guest
Posts: n/a
 
      03-28-2005
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
*/

 
Reply With Quote
 
Old Wolf
Guest
Posts: n/a
 
      03-28-2005
Steven K. Mariner wrote:
>
> 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).

 
Reply With Quote
 
Barry Schwarz
Guest
Posts: n/a
 
      03-29-2005
On 28 Mar 2005 00:58:18 -0800, (E-Mail Removed) wrote:

>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>>
 
Reply With Quote
 
Richard Bos
Guest
Posts: n/a
 
      03-29-2005
(E-Mail Removed) wrote:

> On 28 Mar 2005 09:41:07 GMT, Joona I Palaste <(E-Mail Removed)> wrote:
>
> >> 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

> >
> >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).


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
 
Reply With Quote
 
Steven K. Mariner
Guest
Posts: n/a
 
      03-29-2005
> From: "Old WOlf" <(E-Mail Removed)>
> [...]
> 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.

> > 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.


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 Removed)
http://home.earthlink.net/~marinersk/
http://www.whirlyjigmusic.com/

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Casting from const pair<const unsigned char*, size_t>* to constpair<unsigned char*, size_t>* Alex Vinokur C++ 9 10-13-2008 05:05 PM
Padding bits and char, unsigned char, signed char Ioannis Vranos C Programming 6 03-29-2008 10:55 AM
Padding bits and char, unsigned char, signed char Ioannis Vranos C++ 11 03-28-2008 10:47 PM
Linking error LNK2001 - "__declspec(dllimport) private: void __thiscall std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >::_Tidy(unsigned short)" (__imp_?_Tidy@?$basic_string@DU?$char_ sharmadeep1980@gmail.com C++ 1 07-07-2006 07:27 AM
void*, char*, unsigned char*, signed char* Steffen Fiksdal C Programming 1 05-09-2005 02:33 AM



Advertisments