pack FF in a unsigned char ?

Discussion in 'C Programming' started by archilleswaterland@hotmail.com, Mar 28, 2005.

  1. Guest

    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
     
    , Mar 28, 2005
    #1
    1. Advertising

  2. 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 () ------------- Finland --------\
    \-------------------------------------------------------- rules! --------/
    "To doo bee doo bee doo."
    - Frank Sinatra
     
    Joona I Palaste, Mar 28, 2005
    #2
    1. Advertising

  3. 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++
     
    Emmanuel Delahaye, Mar 28, 2005
    #3
  4. Guest

    On 28 Mar 2005 00:58:18 -0800, 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.
     
    , Mar 28, 2005
    #4
  5. Guest

    On 28 Mar 2005 09:41:07 GMT, Joona I Palaste <> 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).
     
    , Mar 28, 2005
    #5
  6. 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
    */
     
    Steven K. Mariner, Mar 28, 2005
    #6
  7. Old Wolf Guest

    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).
     
    Old Wolf, Mar 29, 2005
    #7
  8. On 28 Mar 2005 00:58:18 -0800, 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>>
     
    Barry Schwarz, Mar 29, 2005
    #8
  9. Richard Bos Guest

    wrote:

    > On 28 Mar 2005 09:41:07 GMT, Joona I Palaste <> 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
     
    Richard Bos, Mar 29, 2005
    #9
  10. > From: "Old WOlf" <>
    > [...]
    > 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

    http://home.earthlink.net/~marinersk/
    http://www.whirlyjigmusic.com/
     
    Steven K. Mariner, Mar 29, 2005
    #10
  11. Guest

    On Tue, 29 Mar 2005 15:28:50 GMT, (Richard Bos)
    wrote:

    >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.
     
    , Mar 29, 2005
    #11
  12. Steven K. Mariner, Mar 29, 2005
    #12
  13. Guest

    On 29 Mar 2005 10:13:42 -0800, "Steven K. Mariner" <>
    wrote:

    >
    > > wrote:

    >
    >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.
     
    , Mar 29, 2005
    #13
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Steffen Fiksdal

    void*, char*, unsigned char*, signed char*

    Steffen Fiksdal, May 8, 2005, in forum: C Programming
    Replies:
    1
    Views:
    588
    Jack Klein
    May 9, 2005
  2. Ioannis Vranos
    Replies:
    11
    Views:
    762
    Ioannis Vranos
    Mar 28, 2008
  3. Ioannis Vranos

    Padding bits and char, unsigned char, signed char

    Ioannis Vranos, Mar 28, 2008, in forum: C Programming
    Replies:
    6
    Views:
    614
    Ben Bacarisse
    Mar 29, 2008
  4. Alex Vinokur
    Replies:
    9
    Views:
    798
    James Kanze
    Oct 13, 2008
  5. pozz
    Replies:
    12
    Views:
    743
    Tim Rentsch
    Mar 20, 2011
Loading...

Share This Page