convert unsigned to char

Discussion in 'C Programming' started by AGRAJA, Dec 14, 2007.

  1. AGRAJA

    AGRAJA Guest

    AGRAJA, Dec 14, 2007
    #1
    1. Advertising

  2. AGRAJA

    Mark Bluemel Guest

    Mark Bluemel, Dec 14, 2007
    #2
    1. Advertising

  3. AGRAJA

    pete Guest

    AGRAJA wrote:
    >
    > how to convert unsigned to char?


    int main (void)
    {
    unsigned u = 0;
    char c = (char)u;

    return 0;
    }

    --
    pete
    pete, Dec 14, 2007
    #3
  4. AGRAJA

    James Kuyper Guest

    AGRAJA wrote:
    > how to convert unsigned to char?


    Use the cast operator: (char)

    > Ref: http://www.thescripts.com/forum/thread477545.html
    >
    > how do I print without the leading ffffff (yet the result should be
    > char*)?


    Printing a pointer type requires the "%p" format specifier. On the
    machines I use most often, getting lots of leading 'f's is pretty much
    unavoidable when using %p with valid pointer values. However, are you
    sure you want the result to be char*? In context, I'd have expected that
    you wanted to print a char, not a char*.

    If that's the case, then you should use either "%d" or "%u" format
    specifier, depending upon whether or not char is signed (if CHAR_MIN <
    0, then char is a signed type). Either way, you're absolutely guaranteed
    to not get any 'f's in the output. :)

    However, I suspect that what you really want is to get no 'f's despite
    using the "%x" specifier. On most implementations where 'char' is
    unsigned, you're extremely unlikely to get leading f's when printing a
    char value.

    Therefore, I assume that you're using an implementation where 'char' is
    signed. You still won't get many leading 'f's on most implementations,
    unless the value you're converting to 'char' is greater than CHAR_MAX.
    Don't do that! Performing such a conversion will either generate an
    implementation-defined result, or cause an implementation-defined signal
    to be raised. Either way, it's generally not a good thing to do. It's
    likely to result in a negative number; if you convert that number to
    unsigned, so that it can be printed using the "%x" format, then the
    conversion will generally result in lots of leading 'f's.

    I'd recommend using 'unsigned char' rather than 'char' for such purposes.
    James Kuyper, Dec 14, 2007
    #4
  5. AGRAJA

    CBFalconer Guest

    pete wrote:
    > AGRAJA wrote:
    >
    >> how to convert unsigned to char?

    >
    > int main (void) {
    > unsigned u = 0;
    > char c = (char)u;
    >
    > return 0;
    > }


    int main (void) {
    unsigned u = 0;
    char c;

    c = u; /* provided that u value <= CHAR_MAX */
    return 0;
    }

    No cast needed. The error also happens with a cast.

    --
    Chuck F (cbfalconer at maineline dot net)
    <http://cbfalconer.home.att.net>
    Try the download section.



    --
    Posted via a free Usenet account from http://www.teranews.com
    CBFalconer, Dec 14, 2007
    #5
  6. James Kuyper <> writes:
    > AGRAJA wrote:
    >> how to convert unsigned to char?

    >
    > Use the cast operator: (char)


    A cast is neither necessary nor helpful. Just assign it; the
    conversion is implicit.

    >> Ref: http://www.thescripts.com/forum/thread477545.html
    >>
    >> how do I print without the leading ffffff (yet the result should be
    >> char*)?

    >
    > Printing a pointer type requires the "%p" format specifier. On the
    > machines I use most often, getting lots of leading 'f's is pretty much
    > unavoidable when using %p with valid pointer values. However, are you
    > sure you want the result to be char*? In context, I'd have expected
    > that you wanted to print a char, not a char*.


    Neither the original post nor the cited web page mentions char* or
    "%p".

    [...]

    --
    Keith Thompson (The_Other_Keith) <>
    Looking for software development work in the San Diego area.
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Dec 15, 2007
    #6
  7. Keith Thompson wrote:
    > Neither the original post nor the cited web page mentions char* or
    > "%p".


    The OP did indeed mention char* (and you quoted him):

    >>> how do I print without the leading ffffff (yet the result should be
    >>> char*)?


    What he meant by this, I have no idea.

    Phil
    Philip Potter, Dec 15, 2007
    #7
  8. AGRAJA

    James Kuyper Guest

    Keith Thompson wrote:
    > James Kuyper <> writes:
    >> AGRAJA wrote:
    >>> how to convert unsigned to char?

    >> Use the cast operator: (char)

    >
    > A cast is neither necessary nor helpful. Just assign it; the
    > conversion is implicit.


    While that is true in an assignment context, the question as posed was
    about conversion in general.

    >>> Ref: http://www.thescripts.com/forum/thread477545.html
    >>>
    >>> how do I print without the leading ffffff (yet the result should be
    >>> char*)?

    >> Printing a pointer type requires the "%p" format specifier. On the
    >> machines I use most often, getting lots of leading 'f's is pretty much
    >> unavoidable when using %p with valid pointer values. However, are you
    >> sure you want the result to be char*? In context, I'd have expected
    >> that you wanted to print a char, not a char*.

    >
    > Neither the original post nor the cited web page mentions char* or
    > "%p".


    ???
    "... (yet the result should be char*)?"

    I don't know what the OP intended that to mean. The result of a
    conversion to char is char, by definition. The result of a C expression
    that prints something by calling printf() is a count of the characters
    printed. The result, in a more informal sense, of printing something is
    the appearance of a set of characters on the display device. I can't
    figure out any meaning of "the result" which fits this context, for
    which char* is the appropriate type.

    However, since he DID mention char*, I decided to say something about
    char*, even if what I said probably had nothing to do with what he
    actually wanted. That was my (probably overly subtle) way of pointing
    out to him that his comment about char* was unclear.
    James Kuyper, Dec 15, 2007
    #8
  9. James Kuyper <> writes:
    > Keith Thompson wrote:
    >> James Kuyper <> writes:
    >>> AGRAJA wrote:
    >>>> how to convert unsigned to char?
    >>> Use the cast operator: (char)

    >>
    >> A cast is neither necessary nor helpful. Just assign it; the
    >> conversion is implicit.

    >
    > While that is true in an assignment context, the question as posed was
    > about conversion in general.


    Yes, but in most cases conversion from one arithmetic type to another
    doesn't require a cast.

    >>>> Ref: http://www.thescripts.com/forum/thread477545.html
    >>>>
    >>>> how do I print without the leading ffffff (yet the result should be
    >>>> char*)?
    >>> Printing a pointer type requires the "%p" format specifier. On the
    >>> machines I use most often, getting lots of leading 'f's is pretty much
    >>> unavoidable when using %p with valid pointer values. However, are you
    >>> sure you want the result to be char*? In context, I'd have expected
    >>> that you wanted to print a char, not a char*.

    >>
    >> Neither the original post nor the cited web page mentions char* or
    >> "%p".

    >
    > ???
    > "... (yet the result should be char*)?"


    Whoops, I missed that. D'oh!

    > I don't know what the OP intended that to mean. The result of a
    > conversion to char is char, by definition. The result of a C
    > expression that prints something by calling printf() is a count of the
    > characters printed. The result, in a more informal sense, of printing
    > something is the appearance of a set of characters on the display
    > device. I can't figure out any meaning of "the result" which fits this
    > context, for which char* is the appropriate type.
    >
    > However, since he DID mention char*, I decided to say something about
    > char*, even if what I said probably had nothing to do with what he
    > actually wanted. That was my (probably overly subtle) way of pointing
    > out to him that his comment about char* was unclear.


    I suspect the OP wanted a string.

    --
    Keith Thompson (The_Other_Keith) <>
    Looking for software development work in the San Diego area.
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Dec 15, 2007
    #9
    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:
    571
    Jack Klein
    May 9, 2005
  2. Ioannis Vranos
    Replies:
    11
    Views:
    750
    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:
    604
    Ben Bacarisse
    Mar 29, 2008
  4. Alex Vinokur
    Replies:
    9
    Views:
    774
    James Kanze
    Oct 13, 2008
  5. pozz
    Replies:
    12
    Views:
    726
    Tim Rentsch
    Mar 20, 2011
Loading...

Share This Page