Thread: Best way to cast unsigned int to int?

  1. #1
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827

    Best way to cast unsigned int to int?

    I have an unsigned int variable that I want to cast to an int, as it may store a -1 value.
    What is the best way to do this?

    I tried this:

    Code:
    lastCharIndex_ui = static_cast<int>(lastCharIndex_ui);
    as well as:

    Code:
    (int)lastCharIndex_ui;
    and other things as well, but it doesn't appear to work since the compiler still complains about the comparison between a unsigned int and a signed int expression.
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    Why not post a larger snippet of what you tried, including the portion that the compiler warned about?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by laserlight View Post
    Why not post a larger snippet of what you tried, including the portion that the compiler warned about?
    This is the only relevant code:

    Code:
    lastCharIndex_ui = static_cast<int>(lastCharIndex_ui); //cast the unsigned int to an int since it may now store a -1 value
    //get the index of the last character of the current enumerator:
    lastCharIndex_ui = findIndexOfLastChar(filestreamInBuffer_s.c_str(), currentEnumeratorName_s.c_str()); 
    
    if (lastCharIndex_ui == -1) {
        return false; //since something funny occurred...
    }
    The bolded expression is what the compiler complains about.
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Well, duh. You're only converting the contents of lastCharIndex_ui to a signed int, but the type itself is still an unsigned type.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    Oh, then that cast achieved nothing. lastCharIndex_ui is presumably an unsigned int, and of course it remains an unsigned int.

    Rather, what you can do is:
    Code:
    //get the index of the last character of the current enumerator:
    lastCharIndex_ui = findIndexOfLastChar(filestreamInBuffer_s.c_str(), currentEnumeratorName_s.c_str()); 
    
    if (lastCharIndex_ui == static_cast<unsigned int>(-1)) {
        return false; //since something funny occurred...
    }
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    ~0U is an unsigned number where all the bits are set. You could replace -1 with that instead.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  7. #7
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by Elysia View Post
    Well, duh. You're only converting the contents of lastCharIndex_ui to a signed int, but the type itself is still an unsigned type.
    Well, how do I change the type then?
    Quote Originally Posted by laserlight View Post
    Oh, then that cast achieved nothing. lastCharIndex_ui is presumably an unsigned int, and of course it remains an unsigned int.

    Rather, what you can do is:
    Code:
    //get the index of the last character of the current enumerator:
    lastCharIndex_ui = findIndexOfLastChar(filestreamInBuffer_s.c_str(), currentEnumeratorName_s.c_str()); 
    
    if (lastCharIndex_ui == static_cast<unsigned int>(-1)) {
        return false; //since something funny occurred...
    }
    Somehow that doesn't seem right...
    Since the cast in your code is after where the variable might be assigned a -1 value (i.e. because of the return value of findIndexOfLastChar), rather than before, then it seems like casting a -1 to an unsigned int (which value I have no idea what it would be, btw) and comparing it with whatever value was stored in the unsigned int variable "lastCharIndex_ui" (since it couldn't have been a -1 seeing as its an unsigned int) makes no sense at all.
    So I think I have the right idea of doing the cast before the function call which might return a -1 value. I just don't know how to change the type of the variable instead of casting the value stored in the variable to a int type.
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

  8. #8
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by iMalc View Post
    ~0U is an unsigned number where all the bits are set. You could replace -1 with that instead.
    Yeah, but is that what the unsigned int variable lastCharIndex_ui would be given too if the function returned a -1? Otherwise the comparison wouldn't work.
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    Quote Originally Posted by Programmer_P
    Since the cast in your code is after where the variable might be assigned a -1 value (i.e. because of the return value of findIndexOfLastChar), rather than before, then it seems like casting a -1 to an unsigned int (which value I have no idea what it would be, btw) and comparing it with whatever value was stored in the unsigned int variable "lastCharIndex_ui" (since it couldn't have been a -1 seeing as its an unsigned int) makes no sense at all.
    Ah, but conversion from signed to unsigned is well defined. For -1, the result will be the maximum value for the unsigned integer type, which is why iMalc's suggestion will work (although it is arguably a little less readable: most readable might be something like std::string::npos, which is -1 cast to std::string::size_type, which is an unsigned integer type).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by laserlight View Post
    Ah, but conversion from signed to unsigned is well defined. For -1, the result will be the maximum value for the unsigned integer type, which is why iMalc's suggestion will work (although it is arguably a little less readable: most readable might be something like std::string::npos, which is -1 cast to std::string::size_type, which is an unsigned integer type).
    And what value is the maximum value for the unsigned integer type? I'm guessing its probably not equilivant to a -1...
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Programmer_P View Post
    Well, how do I change the type then?
    You cannot change the type of an existing variable! That would break the foundations C++ was built upon.

    Somehow that doesn't seem right...
    Since the cast in your code is after where the variable might be assigned a -1 value (i.e. because of the return value of findIndexOfLastChar), rather than before, then it seems like casting a -1 to an unsigned int (which value I have no idea what it would be, btw) and comparing it with whatever value was stored in the unsigned int variable "lastCharIndex_ui" (since it couldn't have been a -1 seeing as its an unsigned int) makes no sense at all.
    It is right. The signed value returned will be converted to an unsigned value.
    Logic then tells me that if we take that same signed number and convert it to unsigned, it should be the same as what was assigned to that unsigned number, yes?
    That is what this logic builds upon and is perfectly defined and right.

    So I think I have the right idea of doing the cast before the function call which might return a -1 value. I just don't know how to change the type of the variable instead of casting the value stored in the variable to a int type.
    You can't and shouldn't.

    Quote Originally Posted by Programmer_P View Post
    And what value is the maximum value for the unsigned integer type? I'm guessing its probably not equilivant to a -1...
    Assuming 4 bytes, 0xFFFFFFFF, or a little over 2 * 10^9.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    Quote Originally Posted by Programmer_P
    And what value is the maximum value for the unsigned integer type?
    UINT_MAX, or if you prefer, std::numeric_limits<unsigned int>::max().

    Quote Originally Posted by Programmer_P
    I'm guessing its probably not equilivant to a -1.
    Obviously
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #13
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by Elysia View Post
    It is right. The signed value returned will be converted to an unsigned value.
    Logic then tells me that if we take that same signed number and convert it to unsigned, it should be the same as what was assigned to that unsigned number, yes?
    That is what this logic builds upon and is perfectly defined and right.
    Ahh...ok. I get it. That does make sense. So essentially, we've got two -1 values that have become the same something else (neither of which are actually -1). I got it.
    Ok, that's what I'll do then.
    Last edited by Programmer_P; 07-11-2010 at 02:37 PM.
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  2. Need Help Please
    By YouShallNotPass in forum C++ Programming
    Replies: 3
    Last Post: 08-22-2006, 11:22 AM
  3. Converted from Dev-C++ 4 to Dev-C++ 5
    By Wraithan in forum C++ Programming
    Replies: 8
    Last Post: 12-03-2005, 07:45 AM
  4. Switch/case Problems (long code in post)
    By Wraithan in forum C++ Programming
    Replies: 2
    Last Post: 12-01-2005, 06:40 PM
  5. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 05:26 PM