How do you concatenate a char to a string

This is a discussion on How do you concatenate a char to a string within the C++ Programming forums, part of the General Programming Boards category; i am trying to concatenate an unsigned char to a string but its not working Code: int main(){ string s= ...

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    118

    How do you concatenate a char to a string

    i am trying to concatenate an unsigned char to a string but its not working
    Code:
    int main(){
    
    string s= "ABCDE";
    unsigned char a='vv;
    s=s+a;
    
    
    }
    Last edited by sigur47; 02-19-2012 at 08:53 AM.

  2. #2
    Registered User
    Join Date
    Nov 2011
    Posts
    48
    Code:
    #include <string>
    #include <iostream>
    
    
    int main(int argc, char* argv[])
    {
    	std::string stringDemo = "Hello";
    	char c = 'v';
    	stringDemo += c;
    	std::cout << stringDemo; // prints Hellov
    	stringDemo.push_back(c);
    	std::cout << stringDemo; // prints Hellovv
    }

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    118
    Quote Originally Posted by Serapth View Post
    Code:
    #include <string>
    #include <iostream>
    
    
    int main(int argc, char* argv[])
    {
        std::string stringDemo = "Hello";
        char c = 'v';
        stringDemo += c;
        std::cout << stringDemo; // prints Hellov
        stringDemo.push_back(c);
        std::cout << stringDemo; // prints Hellovv
    }
    it works fine if its a char but once i use an unsigned char ,it does not work

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,941
    Why do you want to use an unsigned char?

    Anyway, if you know that the value of the unsigned char will be in the range of char, the solution is to cast:
    Code:
    stringDemo.push_back(static_cast<char>(c));
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Nov 2011
    Posts
    118
    Quote Originally Posted by laserlight View Post
    Why do you want to use an unsigned char?

    Anyway, if you know that the value of the unsigned char will be in the range of char, the solution is to cast:
    Code:
    stringDemo.push_back(static_cast<char>(c));
    i am using it for characters that are in the range 0-255 thats why i am using unsigned char.I wont know the range because the characters are being fetched from a file

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,941
    Ah, then instead of using a std::string, use a std::vector<unsigned char>.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Nov 2011
    Posts
    118
    Quote Originally Posted by laserlight View Post
    Ah, then instead of using a std::string, use a std::vector<unsigned char>.
    Thanks

  8. #8
    Registered User
    Join Date
    Nov 2011
    Posts
    48
    Or use wstring.

    Code:
    #include <string>
    #include <iostream>
    
    
    int main(int argc, char* argv[])
    {
        std::wstring stringDemo = L"Hello";
        unsigned char c = 'v';
        stringDemo += c;
        std::wcout << stringDemo; // prints Hellov
    }
    Although frankly, wstring is always such a pain in the .... to work with.

    Using wstring will allow you to do this though:
    Code:
        std::wstring stringDemo = L"Hello";
        unsigned char c = static_cast<unsigned char>(199);
        stringDemo += c;
        std::wcout << stringDemo; // prints Hello╟
    Last edited by Serapth; 02-19-2012 at 09:33 AM. Reason: Removed C style cast... bad habit.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,941
    Quote Originally Posted by Serapth
    Or use wstring.
    I may be wrong, but in theory, the range of a wchar_t may well be the same as that of a signed char, since they have the same constraints on implementation defined limits. So, I reason if that if you want to store unsigned chars, then store unsigned chars.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    Can't basic_string<unsigned char> be used ?
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  11. #11
    Registered User
    Join Date
    Oct 2006
    Posts
    2,503
    I'm not sure that basic_string is portable or standard-compliant. to my knowledge, string and wstring are specified by the standard, but no mention of how they must be implemented. I may be wrong about this.

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,941
    Quote Originally Posted by Elkvis
    I'm not sure that basic_string is portable or standard-compliant. to my knowledge, string and wstring are specified by the standard, but no mention of how they must be implemented. I may be wrong about this.
    basic_string is part of the standard library, but then the stuff like string literals won't exist for basic_string<unsigned char> (except maybe now there's some user defined literals thing, but I'm not familiar with that at all).
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #13
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,790
    basic_string is standard, but char_traits is not standard for unsigned char. Thus, you probably won't be able to use basic_string with unsigned char.
    You can supply your own traits struct/class if you want, though.
    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.

  14. #14
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    This works, but I couldn't get it working with any sort of streams.
    How would I use custom traits ?
    Code:
    basic_string<unsigned char> s;
    s+=static_cast<unsigned char>(200);

    Aren't user defined literals about adding custom suffixes to data ?
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  15. #15
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,790
    Quote Originally Posted by manasij7479 View Post
    Aren't user defined literals about adding custom suffixes to data ?
    C++11 - Wikipedia, the free encyclopedia
    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.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Concatenate string and int
    By millsy5 in forum C Programming
    Replies: 1
    Last Post: 01-28-2010, 03:43 AM
  2. concatenate 2 const char * problem
    By tempster09 in forum C Programming
    Replies: 15
    Last Post: 12-28-2009, 08:23 PM
  3. Concatenate a String?
    By sharkbate24 in forum C++ Programming
    Replies: 7
    Last Post: 01-02-2009, 04:16 PM
  4. concatenate int onto char
    By MK27 in forum C Programming
    Replies: 4
    Last Post: 08-06-2008, 09:54 AM
  5. convert int to char and concatenate
    By robasc in forum C++ Programming
    Replies: 2
    Last Post: 06-10-2007, 06:08 AM

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