basic_string<char> and basic_string<unsigned char>

This is a discussion on basic_string<char> and basic_string<unsigned char> within the C++ Programming forums, part of the General Programming Boards category; I'm using vc++ 2003, and on my compiler, if a character in a string sequence is larger than 0x80, it ...

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    271

    basic_string<char> and basic_string<unsigned char>

    I'm using vc++ 2003, and on my compiler, if a character in a string sequence is larger than 0x80, it shows some pretty weird behavior when used with cout.

    For example
    Code:
    char ct[] = {0xa1, 0xc1, 0xbf, 0xff, 0x00};
    string s(ct);
    string::iterator its;
    cout.setf(ios_base::hex, ios_base::basefield);
    cout.setf(ios_base::uppercase);
    for(its = s.begin(); its != s.end(); its++)
    	cout << setw(3) << (int) *its;
    This will print out
    Code:
    FFFFFFA1FFFFFFC1FFFFFFBFFFFFFFFF
    However, if I define the string instead as basic_string<unsigned char>:

    Code:
    unsigned char ct[] = {0xa1, 0xc1, 0xbf, 0xff, 0x00};
    basic_string<unsigned char> s(ct);
    basic_string<unsigned char>::iterator its;
    cout.setf(ios_base::hex, ios_base::basefield);
    cout.setf(ios_base::uppercase);
    for(its = s.begin(); its != s.end(); its++)
    	cout << setw(3) << (int) *its;
    This prints out:
    Code:
     A1 C1 BF FF
    Why is cout giving me that extra garbage in the upper bits (i.e. +8) if I designate it a basic_string as a plain char sequence? If you expand signed characters, do they become signed integers?

  2. #2
    ggs
    ggs is offline
    C > C++ duders ggs's Avatar
    Join Date
    Aug 2001
    Posts
    435
    yes to that last question, which explains yur output
    .sect signature

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 04-04-2008, 12:41 PM
  2. Can I make istream_iterator<unsigned char> not skip spaces?
    By Iterator++ in forum C++ Programming
    Replies: 3
    Last Post: 11-06-2002, 09:18 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21