How do I convert a char to an unsigned int?

Discussion in 'C++' started by DeMarcus, Oct 25, 2010.

  1. DeMarcus

    DeMarcus Guest

    Hi!

    How do I convert a char or wchar_t to an int in a proper and portable way?

    If I do this

    char wickedChar = 'Æ';
    unsigned int myInt = c;

    then myInt is a very big number, it's the sign bit of wickedChar that
    does that.

    Maybe I could do something like

    unsigned int myInt = (unsigned char)c;

    but the problem is that this is a template so I don't know if it's going
    to be a char or wchar_t (or even something else).

    How can I convert the char (or wchar_t) to an unsigned int in a portable
    way?


    Thanks,
    Daniel
    DeMarcus, Oct 25, 2010
    #1
    1. Advertising

  2. DeMarcus

    DeMarcus Guest

    On 10/25/2010 01:05 PM, Sam wrote:
    > DeMarcus writes:
    >
    >> Hi!
    >>
    >> How do I convert a char or wchar_t to an int in a proper and portable
    >> way?
    >>
    >> If I do this
    >>
    >> char wickedChar = 'Æ';
    >> unsigned int myInt = c;
    >>
    >> then myInt is a very big number, it's the sign bit of wickedChar that
    >> does that.
    >>
    >> Maybe I could do something like
    >>
    >> unsigned int myInt = (unsigned char)c;
    >>
    >> but the problem is that this is a template so I don't know if it's
    >> going to be a char or wchar_t (or even something else).

    >
    > If that's your concern, provide the appropriate specializations for your
    > template.
    >


    Basically it's something like this.

    #ifdef SOME_FLAG
    typedef boost::match_results<string::const_iterator> MyMatch;
    #else
    typedef boost::match_results<wstring::const_iterator> MyMatch;
    #endif

    // More about boost::match_results can be found here.
    // http://www.cs.brown.edu/~jwicks/boost/libs/regex/doc/match_results.html


    void fnc( const MyMatch& match )
    {
    unsigned int charToInt = match[0].str()[0];
    // ...
    }

    I did like this, which compiles and seems to work...

    void fnc( const MyMatch& match )
    {
    unsigned int charToInt =
    (unsigned MyMatch::char_type)match[0].str()[0];
    // ...
    }

    .... but is it proper and portable C++ to cast like that?
    DeMarcus, Oct 25, 2010
    #2
    1. Advertising

  3. * DeMarcus, on 25.10.2010 13:36:
    > On 10/25/2010 01:05 PM, Sam wrote:
    >> DeMarcus writes:
    >>
    >>> Hi!
    >>>
    >>> How do I convert a char or wchar_t to an int in a proper and portable
    >>> way?
    >>>
    >>> If I do this
    >>>
    >>> char wickedChar = 'Æ';
    >>> unsigned int myInt = c;
    >>>
    >>> then myInt is a very big number, it's the sign bit of wickedChar that
    >>> does that.
    >>>
    >>> Maybe I could do something like
    >>>
    >>> unsigned int myInt = (unsigned char)c;
    >>>
    >>> but the problem is that this is a template so I don't know if it's
    >>> going to be a char or wchar_t (or even something else).

    >>
    >> If that's your concern, provide the appropriate specializations for your
    >> template.
    >>

    >
    > Basically it's something like this.
    >
    > #ifdef SOME_FLAG
    > typedef boost::match_results<string::const_iterator> MyMatch;
    > #else
    > typedef boost::match_results<wstring::const_iterator> MyMatch;
    > #endif
    >
    > // More about boost::match_results can be found here.
    > // http://www.cs.brown.edu/~jwicks/boost/libs/regex/doc/match_results.html
    >
    >
    > void fnc( const MyMatch& match )
    > {
    > unsigned int charToInt = match[0].str()[0];
    > // ...
    > }
    >
    > I did like this, which compiles and seems to work...
    >
    > void fnc( const MyMatch& match )
    > {
    > unsigned int charToInt =
    > (unsigned MyMatch::char_type)match[0].str()[0];
    > // ...
    > }
    >
    > ... but is it proper and portable C++ to cast like that?


    At first sight this seems like meaningless technobabble.

    "#ifdef" can't be used to decide on template specialization, "unsigned" can't be
    used to alter a named type, and I haven't heard of boost:match_results.

    But perhaps you could explain what you're doing and present some /valid/ code?


    Cheers,

    - Alf

    --
    blog at <url: http://alfps.wordpress.com>
    Alf P. Steinbach /Usenet, Oct 25, 2010
    #3
  4. DeMarcus

    Bo Persson Guest

    DeMarcus wrote:

    >
    > void fnc( const MyMatch& match )
    > {
    > unsigned int charToInt = match[0].str()[0];
    > // ...
    > }
    >
    > I did like this, which compiles and seems to work...
    >
    > void fnc( const MyMatch& match )
    > {
    > unsigned int charToInt =
    > (unsigned MyMatch::char_type)match[0].str()[0];
    > // ...
    > }
    >
    > ... but is it proper and portable C++ to cast like that?


    No, it's not!

    Why don't you define a separate function ToInt, and overload that for
    the character types you need?


    Bo Persson
    Bo Persson, Oct 25, 2010
    #4
  5. DeMarcus

    DeMarcus Guest

    On 10/25/2010 07:31 PM, Bo Persson wrote:
    > DeMarcus wrote:
    >
    >>
    >> void fnc( const MyMatch& match )
    >> {
    >> unsigned int charToInt = match[0].str()[0];
    >> // ...
    >> }
    >>
    >> I did like this, which compiles and seems to work...
    >>
    >> void fnc( const MyMatch& match )
    >> {
    >> unsigned int charToInt =
    >> (unsigned MyMatch::char_type)match[0].str()[0];
    >> // ...
    >> }
    >>
    >> ... but is it proper and portable C++ to cast like that?

    >
    > No, it's not!
    >
    > Why don't you define a separate function ToInt, and overload that for
    > the character types you need?
    >
    >
    > Bo Persson
    >
    >


    You mean something like this?

    unsigned int ToInt( char c )
    {
    unsigned int i = (unsigned char)c;
    return i;
    }

    unsigned int ToInt( wchar_t c )
    {
    unsigned int i = (unsigned wchar_t)c;
    return i;
    }
    DeMarcus, Oct 26, 2010
    #5
  6. DeMarcus

    DeMarcus Guest

    On 10/25/2010 06:04 PM, Alf P. Steinbach /Usenet wrote:
    > * DeMarcus, on 25.10.2010 13:36:
    >> On 10/25/2010 01:05 PM, Sam wrote:
    >>> DeMarcus writes:
    >>>
    >>>> Hi!
    >>>>
    >>>> How do I convert a char or wchar_t to an int in a proper and portable
    >>>> way?
    >>>>
    >>>> If I do this
    >>>>
    >>>> char wickedChar = 'Æ';
    >>>> unsigned int myInt = c;
    >>>>
    >>>> then myInt is a very big number, it's the sign bit of wickedChar that
    >>>> does that.
    >>>>
    >>>> Maybe I could do something like
    >>>>
    >>>> unsigned int myInt = (unsigned char)c;
    >>>>
    >>>> but the problem is that this is a template so I don't know if it's
    >>>> going to be a char or wchar_t (or even something else).
    >>>
    >>> If that's your concern, provide the appropriate specializations for your
    >>> template.
    >>>

    >>
    >> Basically it's something like this.
    >>
    >> #ifdef SOME_FLAG
    >> typedef boost::match_results<string::const_iterator> MyMatch;
    >> #else
    >> typedef boost::match_results<wstring::const_iterator> MyMatch;
    >> #endif
    >>
    >> // More about boost::match_results can be found here.
    >> //
    >> http://www.cs.brown.edu/~jwicks/boost/libs/regex/doc/match_results.html
    >>
    >>
    >> void fnc( const MyMatch& match )
    >> {
    >> unsigned int charToInt = match[0].str()[0];
    >> // ...
    >> }
    >>
    >> I did like this, which compiles and seems to work...
    >>
    >> void fnc( const MyMatch& match )
    >> {
    >> unsigned int charToInt =
    >> (unsigned MyMatch::char_type)match[0].str()[0];
    >> // ...
    >> }
    >>
    >> ... but is it proper and portable C++ to cast like that?

    >
    > At first sight this seems like meaningless technobabble.
    >
    > "#ifdef" can't be used to decide on template specialization, "unsigned"
    > can't be used to alter a named type, and I haven't heard of
    > boost:match_results.
    >
    > But perhaps you could explain what you're doing and present some /valid/
    > code?
    >
    >
    > Cheers,
    >
    > - Alf
    >



    #include <iostream>

    typedef char MySignedChar;

    int main()
    {
    MySignedChar c = 200;

    // When doing this I get a very big number.
    unsigned int i = c;

    std::cout << "Char to int: " << i << std::endl;

    // When doing this I get the number I want.
    unsigned int i2 = (unsigned MySignedChar)c;

    std::cout << "Char to int again: " << i2 << std::endl;

    return 0;
    }


    The boost::match_results can be found here.
    http://www.cs.brown.edu/~jwicks/boost/libs/regex/doc/match_results.html

    I'm using it like this.

    wstring myFormat( const boost::wsmatch& what )
    {
    uint32 charToInt = (unsigned wsmatch::char_type)what[0].str()[0];
    std::cout << charToInt << std::endl;

    // ... create replacement ...
    return replacement;
    }

    wstring encode( const wstring& text )
    {
    const boost::wregex expression( "[^a-zA-Z0-9]" );

    return boost::regex_replace( text, expression, myFormat );
    }

    If there's a better way to do search-and-replace, preferably with
    something C++0x compliant, I'm interested in that.
    DeMarcus, Oct 26, 2010
    #6
  7. * DeMarcus, on 26.10.2010 09:27:
    > On 10/25/2010 06:04 PM, Alf P. Steinbach /Usenet wrote:
    >> * DeMarcus, on 25.10.2010 13:36:
    >>> On 10/25/2010 01:05 PM, Sam wrote:
    >>>> DeMarcus writes:
    >>>>
    >>>>> Hi!
    >>>>>
    >>>>> How do I convert a char or wchar_t to an int in a proper and portable
    >>>>> way?
    >>>>>
    >>>>> If I do this
    >>>>>
    >>>>> char wickedChar = 'Æ';
    >>>>> unsigned int myInt = c;
    >>>>>
    >>>>> then myInt is a very big number, it's the sign bit of wickedChar that
    >>>>> does that.
    >>>>>
    >>>>> Maybe I could do something like
    >>>>>
    >>>>> unsigned int myInt = (unsigned char)c;
    >>>>>
    >>>>> but the problem is that this is a template so I don't know if it's
    >>>>> going to be a char or wchar_t (or even something else).
    >>>>
    >>>> If that's your concern, provide the appropriate specializations for your
    >>>> template.
    >>>>
    >>>
    >>> Basically it's something like this.
    >>>
    >>> #ifdef SOME_FLAG
    >>> typedef boost::match_results<string::const_iterator> MyMatch;
    >>> #else
    >>> typedef boost::match_results<wstring::const_iterator> MyMatch;
    >>> #endif
    >>>
    >>> // More about boost::match_results can be found here.
    >>> //
    >>> http://www.cs.brown.edu/~jwicks/boost/libs/regex/doc/match_results.html
    >>>
    >>>
    >>> void fnc( const MyMatch& match )
    >>> {
    >>> unsigned int charToInt = match[0].str()[0];
    >>> // ...
    >>> }
    >>>
    >>> I did like this, which compiles and seems to work...
    >>>
    >>> void fnc( const MyMatch& match )
    >>> {
    >>> unsigned int charToInt =
    >>> (unsigned MyMatch::char_type)match[0].str()[0];
    >>> // ...
    >>> }
    >>>
    >>> ... but is it proper and portable C++ to cast like that?

    >>
    >> At first sight this seems like meaningless technobabble.
    >>
    >> "#ifdef" can't be used to decide on template specialization, "unsigned"
    >> can't be used to alter a named type, and I haven't heard of
    >> boost:match_results.
    >>
    >> But perhaps you could explain what you're doing and present some /valid/
    >> code?
    >>
    >>
    >> Cheers,
    >>
    >> - Alf
    >>

    >
    >
    > #include <iostream>
    >
    > typedef char MySignedChar;
    >
    > int main()
    > {
    > MySignedChar c = 200;
    >
    > // When doing this I get a very big number.
    > unsigned int i = c;
    >
    > std::cout << "Char to int: " << i << std::endl;
    >
    > // When doing this I get the number I want.
    > unsigned int i2 = (unsigned MySignedChar)c;
    >
    > std::cout << "Char to int again: " << i2 << std::endl;
    >
    > return 0;
    > }


    Well, as I wrote, you cannot use "unsigned" to alter a named type.

    g++ just warns about (perhaps one can instruct it to treat that warning as an
    error), while msvc and Comeau diagnose it as error, as is proper:


    <g++>
    x.cpp: In function 'int main()':
    x.cpp:7: warning: overflow in implicit constant conversion
    x.cpp:15: warning: long, short, signed or unsigned used invalidly for 'type name'
    <g++>

    <msvc>
    x.cpp
    x.cpp(7) : warning C4309: 'initializing' : truncation of constant value
    x.cpp(15) : error C2146: syntax error : missing ')' before identifier 'MySignedChar'
    x.cpp(15) : error C2059: syntax error : ')'
    x.cpp(15) : error C2275: 'MySignedChar' : illegal use of this type as an expression
    x.cpp(3) : see declaration of 'MySignedChar'
    x.cpp(15) : error C2146: syntax error : missing ';' before identifier 'c'
    <msvc

    <comeau>
    Comeau C/C++ 4.3.10.1 (Oct 6 2008 11:28:09) for ONLINE_EVALUATION_BETA2
    Copyright 1988-2008 Comeau Computing. All rights reserved.
    MODE:strict errors C++ C++0x_extensions

    "ComeauTest.c", line 7: warning: integer conversion resulted in a change of sign
    MySignedChar c = 200;
    ^

    "ComeauTest.c", line 15: error: expected a ")"
    unsigned int i2 = (unsigned MySignedChar)c;
    ^

    1 error detected in the compilation of "ComeauTest.c".
    <comeau>


    > The boost::match_results can be found here.
    > http://www.cs.brown.edu/~jwicks/boost/libs/regex/doc/match_results.html


    OK, thanks.


    > I'm using it like this.
    >
    > wstring myFormat( const boost::wsmatch& what )
    > {
    > uint32 charToInt = (unsigned wsmatch::char_type)what[0].str()[0];
    > std::cout << charToInt << std::endl;
    >
    > // ... create replacement ...
    > return replacement;
    > }
    >
    > wstring encode( const wstring& text )
    > {
    > const boost::wregex expression( "[^a-zA-Z0-9]" );
    >
    > return boost::regex_replace( text, expression, myFormat );
    > }
    >
    > If there's a better way to do search-and-replace, preferably with something
    > C++0x compliant, I'm interested in that.


    This latter question depends on your criterion for "better", and exactly what
    you're doing.

    However, for the conversion to "unsigned" consider


    typedef unsigned char UChar;

    inline unsigned asUnsigned( char c ) { return UChar( c ); }
    inline unsigned asUnsigned( wchar_t c ) { return unsigned( c ); }

    STATIC_ASSERT( sizeof( unsigned ) >= sizeof( wchar_t ) );

    where STATIC_ASSERT might be e.g. BOOST_STATIC_ASSERT.


    Cheers & hth.,

    - Alf




    --
    blog at <url: http://alfps.wordpress.com>
    Alf P. Steinbach /Usenet, Oct 26, 2010
    #7
  8. DeMarcus

    DeMarcus Guest

    On 10/26/2010 10:26 AM, Alf P. Steinbach /Usenet wrote:
    > * DeMarcus, on 26.10.2010 09:27:
    >> On 10/25/2010 06:04 PM, Alf P. Steinbach /Usenet wrote:
    >>> * DeMarcus, on 25.10.2010 13:36:
    >>>> On 10/25/2010 01:05 PM, Sam wrote:
    >>>>> DeMarcus writes:
    >>>>>
    >>>>>> Hi!
    >>>>>>
    >>>>>> How do I convert a char or wchar_t to an int in a proper and portable
    >>>>>> way?
    >>>>>>
    >>>>>> If I do this
    >>>>>>
    >>>>>> char wickedChar = 'Æ';
    >>>>>> unsigned int myInt = c;
    >>>>>>
    >>>>>> then myInt is a very big number, it's the sign bit of wickedChar that
    >>>>>> does that.
    >>>>>>
    >>>>>> Maybe I could do something like
    >>>>>>
    >>>>>> unsigned int myInt = (unsigned char)c;
    >>>>>>
    >>>>>> but the problem is that this is a template so I don't know if it's
    >>>>>> going to be a char or wchar_t (or even something else).
    >>>>>
    >>>>> If that's your concern, provide the appropriate specializations for
    >>>>> your
    >>>>> template.
    >>>>>
    >>>>
    >>>> Basically it's something like this.
    >>>>
    >>>> #ifdef SOME_FLAG
    >>>> typedef boost::match_results<string::const_iterator> MyMatch;
    >>>> #else
    >>>> typedef boost::match_results<wstring::const_iterator> MyMatch;
    >>>> #endif
    >>>>
    >>>> // More about boost::match_results can be found here.
    >>>> //
    >>>> http://www.cs.brown.edu/~jwicks/boost/libs/regex/doc/match_results.html
    >>>>
    >>>>
    >>>> void fnc( const MyMatch& match )
    >>>> {
    >>>> unsigned int charToInt = match[0].str()[0];
    >>>> // ...
    >>>> }
    >>>>
    >>>> I did like this, which compiles and seems to work...
    >>>>
    >>>> void fnc( const MyMatch& match )
    >>>> {
    >>>> unsigned int charToInt =
    >>>> (unsigned MyMatch::char_type)match[0].str()[0];
    >>>> // ...
    >>>> }
    >>>>
    >>>> ... but is it proper and portable C++ to cast like that?
    >>>
    >>> At first sight this seems like meaningless technobabble.
    >>>
    >>> "#ifdef" can't be used to decide on template specialization, "unsigned"
    >>> can't be used to alter a named type, and I haven't heard of
    >>> boost:match_results.
    >>>
    >>> But perhaps you could explain what you're doing and present some /valid/
    >>> code?
    >>>
    >>>
    >>> Cheers,
    >>>
    >>> - Alf
    >>>

    >>
    >>
    >> #include <iostream>
    >>
    >> typedef char MySignedChar;
    >>
    >> int main()
    >> {
    >> MySignedChar c = 200;
    >>
    >> // When doing this I get a very big number.
    >> unsigned int i = c;
    >>
    >> std::cout << "Char to int: " << i << std::endl;
    >>
    >> // When doing this I get the number I want.
    >> unsigned int i2 = (unsigned MySignedChar)c;
    >>
    >> std::cout << "Char to int again: " << i2 << std::endl;
    >>
    >> return 0;
    >> }

    >
    > Well, as I wrote, you cannot use "unsigned" to alter a named type.
    >
    > g++ just warns about (perhaps one can instruct it to treat that warning
    > as an error), while msvc and Comeau diagnose it as error, as is proper:
    >
    >
    > <g++>
    > x.cpp: In function 'int main()':
    > x.cpp:7: warning: overflow in implicit constant conversion
    > x.cpp:15: warning: long, short, signed or unsigned used invalidly for
    > 'type name'
    > <g++>
    >
    > <msvc>
    > x.cpp
    > x.cpp(7) : warning C4309: 'initializing' : truncation of constant value
    > x.cpp(15) : error C2146: syntax error : missing ')' before identifier
    > 'MySignedChar'
    > x.cpp(15) : error C2059: syntax error : ')'
    > x.cpp(15) : error C2275: 'MySignedChar' : illegal use of this type as an
    > expression
    > x.cpp(3) : see declaration of 'MySignedChar'
    > x.cpp(15) : error C2146: syntax error : missing ';' before identifier 'c'
    > <msvc
    >
    > <comeau>
    > Comeau C/C++ 4.3.10.1 (Oct 6 2008 11:28:09) for ONLINE_EVALUATION_BETA2
    > Copyright 1988-2008 Comeau Computing. All rights reserved.
    > MODE:strict errors C++ C++0x_extensions
    >
    > "ComeauTest.c", line 7: warning: integer conversion resulted in a change
    > of sign
    > MySignedChar c = 200;
    > ^
    >
    > "ComeauTest.c", line 15: error: expected a ")"
    > unsigned int i2 = (unsigned MySignedChar)c;
    > ^
    >
    > 1 error detected in the compilation of "ComeauTest.c".
    > <comeau>
    >
    >
    >> The boost::match_results can be found here.
    >> http://www.cs.brown.edu/~jwicks/boost/libs/regex/doc/match_results.html

    >
    > OK, thanks.
    >
    >
    >> I'm using it like this.
    >>
    >> wstring myFormat( const boost::wsmatch& what )
    >> {
    >> uint32 charToInt = (unsigned wsmatch::char_type)what[0].str()[0];
    >> std::cout << charToInt << std::endl;
    >>
    >> // ... create replacement ...
    >> return replacement;
    >> }
    >>
    >> wstring encode( const wstring& text )
    >> {
    >> const boost::wregex expression( "[^a-zA-Z0-9]" );
    >>
    >> return boost::regex_replace( text, expression, myFormat );
    >> }
    >>
    >> If there's a better way to do search-and-replace, preferably with
    >> something
    >> C++0x compliant, I'm interested in that.

    >
    > This latter question depends on your criterion for "better", and exactly
    > what you're doing.
    >


    Basically I want to do search-and-replace, preferably using
    std::tr1::regex. With "better" I mean something compliant to
    std::tr1::regex since, as I understand (correct me if I'm wrong), there
    are some things with boost::regex that won't go into std::tr1::regex.

    I think it's some legacy thing called boost xpressive.

    http://www.boost.org/doc/libs/1_40_...t_xpressive.user_s_guide.string_substitutions


    > However, for the conversion to "unsigned" consider
    >
    >
    > typedef unsigned char UChar;
    >
    > inline unsigned asUnsigned( char c ) { return UChar( c ); }
    > inline unsigned asUnsigned( wchar_t c ) { return unsigned( c ); }
    >
    > STATIC_ASSERT( sizeof( unsigned ) >= sizeof( wchar_t ) );
    >
    > where STATIC_ASSERT might be e.g. BOOST_STATIC_ASSERT.
    >
    >
    > Cheers & hth.,
    >
    > - Alf
    >


    Thanks!
    DeMarcus, Oct 26, 2010
    #8
  9. DeMarcus

    Bo Persson Guest

    DeMarcus wrote:
    > On 10/25/2010 07:31 PM, Bo Persson wrote:
    >> DeMarcus wrote:
    >>
    >>>
    >>> void fnc( const MyMatch& match )
    >>> {
    >>> unsigned int charToInt = match[0].str()[0];
    >>> // ...
    >>> }
    >>>
    >>> I did like this, which compiles and seems to work...
    >>>
    >>> void fnc( const MyMatch& match )
    >>> {
    >>> unsigned int charToInt =
    >>> (unsigned MyMatch::char_type)match[0].str()[0];
    >>> // ...
    >>> }
    >>>
    >>> ... but is it proper and portable C++ to cast like that?

    >>
    >> No, it's not!
    >>
    >> Why don't you define a separate function ToInt, and overload that
    >> for the character types you need?
    >>
    >>
    >> Bo Persson
    >>
    >>

    >
    > You mean something like this?
    >
    > unsigned int ToInt( char c )
    > {
    > unsigned int i = (unsigned char)c;
    > return i;
    > }
    >
    > unsigned int ToInt( wchar_t c )
    > {
    > unsigned int i = (unsigned wchar_t)c;
    > return i;
    > }


    Yes, except that wchar_t already is unsigned (and that you cannot add
    or remove signedness - it really should have been unsigned_char to be
    regular and avoid keywords that look like two words).


    Bo Persson
    Bo Persson, Oct 26, 2010
    #9
  10. DeMarcus

    Bo Persson Guest

    Pete Becker wrote:
    > On 2010-10-26 13:22:22 -0400, Bo Persson said:
    >
    >>
    >> Yes, except that wchar_t already is unsigned (and that you cannot
    >> add or remove signedness - it really should have been
    >> unsigned_char to be regular and avoid keywords that look like two
    >> words).

    >
    > wchar_t does not have to be unsigned. It has to have "[t]he same
    > size, signedness, and alignment requirements as one of the other
    > integral types...".


    Right. I just haven't seen a case where it was signed.

    And you still cannot ask for an unsigned version, even if it is
    signed.


    Bo Persson
    Bo Persson, Oct 26, 2010
    #10
  11. DeMarcus

    Öö Tiib Guest

    On 26 okt, 21:07, "Bo Persson" <> wrote:
    > Pete Becker wrote:
    > > On 2010-10-26 13:22:22 -0400, Bo Persson said:

    >
    > >> Yes, except that wchar_t already is unsigned (and that you cannot
    > >> add or remove signedness - it really should have been
    > >> unsigned_char to be regular and avoid keywords that look like two
    > >> words).

    >
    > > wchar_t does not have to be unsigned. It has to have "[t]he same
    > > size, signedness, and alignment requirements as one of the other
    > > integral types...".

    >
    > Right. I just haven't seen a case where it was signed.
    >
    > And you still cannot ask for an unsigned version, even if it is
    > signed.


    #include <limits>
    #include <boost/static_assert.hpp>
    BOOST_STATIC_ASSERT( !std::numeric_limits<wchar_t>::is_signed );
    Öö Tiib, Oct 26, 2010
    #11
  12. DeMarcus

    James Kanze Guest

    On Oct 26, 7:07 pm, "Bo Persson" <> wrote:
    > Pete Becker wrote:
    > > On 2010-10-26 13:22:22 -0400, Bo Persson said:


    > >> Yes, except that wchar_t already is unsigned (and that you cannot
    > >> add or remove signedness - it really should have been
    > >> unsigned_char to be regular and avoid keywords that look like two
    > >> words).


    > > wchar_t does not have to be unsigned. It has to have "[t]he same
    > > size, signedness, and alignment requirements as one of the other
    > > integral types...".


    > Right. I just haven't seen a case where it was signed.


    I think it's signed more often than it is unsigned. Logically,
    one would expect it to have the same signedness as char, so
    promotions would conserve the value. (Of course, logically, one
    would expect plain char to always be unsigned, so you could use
    it directly as an argument to the functions in <ctype.h>. But
    for histerical^H^H^H^H^H^Horical reasons, it's more often signed
    than not.)

    On 32 bit systems where wchar_t is 16 bit, it might generally be
    unsigned. On 32 bit systems where wchar_t is 32 bits (the
    majority, I think), it will almost certainly be signed, so that
    wint_t can also be 32 bits.

    --
    James Kanze
    James Kanze, Oct 27, 2010
    #12
    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. Timo Freiberger
    Replies:
    3
    Views:
    933
    Bob Hairgrove
    Oct 30, 2004
  2. Hal Styli
    Replies:
    14
    Views:
    1,617
    Old Wolf
    Jan 20, 2004
  3. er
    Replies:
    6
    Views:
    479
    Andre Kostur
    Sep 14, 2007
  4. ciccio

    int*unsigned int = unsigned?

    ciccio, Jun 4, 2010, in forum: C++
    Replies:
    2
    Views:
    396
    Öö Tiib
    Jun 4, 2010
  5. pozz
    Replies:
    12
    Views:
    726
    Tim Rentsch
    Mar 20, 2011
Loading...

Share This Page