Thread: Push ints into a char vector

  1. #1
    Registered User
    Join Date
    Jun 2011
    Posts
    17

    Push ints into a char vector

    Hi, I am trying to push short ints onto a char vector, with 4 digit padding (ie 1 becomes 0001 pushed on the vector )

    This is what I have...but I keep on having these crazy f's appear all over the place and I don't know how to make it so they arent there...

    Code:
    vector<char> data;
    // Note have some short int array pack length of pack_length
    
    for ( int i = 0; i < pack_length; i++ ){
        data.push_back ( ( pack[i] & 0xffff ) );
    }
    
    // Display results
    for (int i = 0; i < data.size(); i < data.size(); i++ ) {
        cout << hex << (int)data[i];
    }
    If I input the pack with a single value, 0xf9 then the output is
    ffffffff8
    However if the value is all digits then it works ok, like my test case of 45 gets pushed on just fine, but it is not padded with 0's.

    Any help would be greatly appreciated. Thanks,

    Prediluted

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    At this point, I suspect you're just abusing data types because you can. What possible use is there for anding with 0xffff? (Hint: none.) Why are you trying to push a short onto a vector of char? (Hint: you can't.) Do you want to break up the short into bytes? (Hint: you aren't.) Why are you trying to print as an int? (Hint: whenever you do, you will get a bunch of f's when you print a negative value.)

  3. #3
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    A short int is guaranteed to be larger then a char so you really can't push anything onto the vector without overwriting memory and overflowing your char integer. Both of these behaviors are undefined. A char can only safely handle values from -127 to 128. So you'll have to do something safer than this.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by whiteflags
    A short int is guaranteed to be larger then a char
    Strictly speaking, that is not true, but you should consider that the range of a short int is typically larger than that of a char.

    Quote Originally Posted by whiteflags
    you really can't push anything onto the vector without overwriting memory and overflowing your char integer. Both of these behaviors are undefined.
    Looking at what Prediluted wrote, it does not look like pushing into the vector will overwrite memory that shouldn't be overwritten. If char is signed, and overflow occurs, the resulting value is implementation defined.

    Quote Originally Posted by whiteflags
    A char can only safely handle values from -127 to 128.
    The minimum guaranteed range for char, if char is signed, is -127 to 127, not 128 because one's complement or sign and magnitude might be used.
    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

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    You are casting a char to an int, this results in sign extension, meaning that the highest bit from the char is copied into all the other bits that were not present in a char. This is done to guarantee that a negative number has the same value when assigned from one type to the other.
    To avoid it, you need to use unsigned types.
    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"

  6. #6
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    733
    Quote Originally Posted by laserlight View Post
    Looking at what Prediluted wrote, it does not look like pushing into the vector will overwrite memory that shouldn't be overwritten. If char is signed, and overflow occurs, the resulting value is implementation defined.
    I am wondering if there is anyone who has ever written a fully standard conforming code, including all bit and integer overflow / underflow glitches. If I am right, it should be checked from time to time whether the integer exceeds its limits? And I hardly ever see such tests (and to be honest hardly ever bother with them too).

  7. #7
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by kmdv View Post
    I am wondering if there is anyone who has ever written a fully standard conforming code, including all bit and integer overflow / underflow glitches. If I am right, it should be checked from time to time whether the integer exceeds its limits? And I hardly ever see such tests (and to be honest hardly ever bother with them too).
    How can you check for an integer "exceeding its limits?" It is impossible to exceed the limits. It's like checking whether an object is outside the universe.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  8. #8
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    733
    Quote Originally Posted by brewbuck View Post
    How can you check for an integer "exceeding its limits?" It is impossible to exceed the limits. It's like checking whether an object is outside the universe.
    You probably did not understand me... Exceeding was referred to my previous statement where I mentioned overflows.

    Code:
    int increment(int x)
    {
        if (x < std::numeric_limits<int>::max())
        {
            return x + 1;
        }
        // error
    }

  9. #9
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    838
    it seems like what you're after would be to convert the short in its 2-byte binary representation into an ASCII(or other encoding) character array containing the arabic numeral characters that comprise that number, then store those characters in the vector.

    however, i am not really sure what you're trying to achieve, as your stated goal is very imprecise; i am really basing all of this based on the fact that you say you need "4 digit padding", which has absolutely no relevance outside of a text representation of a number.

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by kmdv View Post
    You probably did not understand me... Exceeding was referred to my previous statement where I mentioned overflows.

    Code:
    int increment(int x)
    {
        if (x < std::numeric_limits<int>::max())
        {
            return x + 1;
        }
        // error
    }
    All you're going to get there is unreachable code. If you have x stored in an int variable, then it must needs be less than intmax. Once you've stored the variable the overflow has already happened and wrapped around (or whatever else happens). You'd have to do the calculations in a larger type and then check, or get some sort of hardware flag.

    EDIT: Sorry I was reading that as checking after the increment. The way it's written is fine, but is not going to be very useful for reasons above.

  11. #11
    Registered User
    Join Date
    Mar 2005
    Posts
    9
    Hi, I am trying to push short ints onto a char vector, with 4 digit padding (ie 1 becomes 0001 pushed on the vector )
    What you would like to do is convert the short int into a stream of bytes and then you are good to go.
    These conversions are easy when you use stringstreams
    good luck

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 04-04-2008, 12:41 PM
  2. Dealing with ints that are in char array
    By Wiretron in forum C Programming
    Replies: 4
    Last Post: 05-15-2006, 04:06 PM
  3. need help with char and ints in an array
    By satory in forum C Programming
    Replies: 1
    Last Post: 12-05-2004, 01:41 PM
  4. vector of ints[]
    By robquigley in forum C++ Programming
    Replies: 2
    Last Post: 11-11-2003, 01:44 PM
  5. using push back function of a vector class template
    By anti-hw in forum C++ Programming
    Replies: 1
    Last Post: 07-20-2002, 11:25 AM