3

Is there a way to force string use unsigned char instead of char? Perhaps in constructor?

I need to do arithmetic operations (mostly incrementing, decrementing, ~ (bitwise NOT)), and I need to be able to use overflow 255++ == 0... Not 127++ == -128 (and underflow 0-- == 255...)

I am not sure if question makes sense, to put some light on it here is a good question abou topic (streams) Why do C++ streams use char instead of unsigned char?

I am not trying to convert string to unsigned char I found a lot of questions how to convert between the two.

Community
  • 1
  • 1
Kyslik
  • 8,217
  • 5
  • 54
  • 87
  • 1
    Unrelated to answer: `char` is not necessarily (-128 to 127). It may behave as `signed char` or `unsigned char` – Mohit Jain Feb 22 '16 at 11:47
  • Are you actually using the string as a string? – MikeMB Feb 22 '16 at 11:57
  • I am using it as string In a sense; I copy (strdup()) to string variables optarg from getopt, I use something like this to get rid of white spaces while(fin >> skipws >> ch) my_string.append(&ch); ( ch is type of char) and I plan using something like my_string[i]++ to increment... – Kyslik Feb 22 '16 at 12:01
  • I asked in order to decide, whether I should upvote the `std::vector` or `std::basic_string` answer. `std::basic_string` seems to be closer to your question, but `my_string[i]++` indicates, you are not really treating the contents as a string but rather a collection of bytes, in which case I'd favor `std::vector` but this would require you to change other parts of your code as well. – MikeMB Feb 22 '16 at 12:33
  • Oh I am not a c++ guru at all I would definitely have to change lots of code. Firstly I was using pure char/unsigned char (not string) then I rewrote it to use string because of lot of .methods. Now I am deciding to use ustring only in few parts of code where I need to use overflows and underflows (and ~). Thank you for your input. – Kyslik Feb 22 '16 at 12:38

3 Answers3

5

You can use:

typedef std::basic_string<unsigned char> ustring;

And then use ustring instead of std::string

Mohit Jain
  • 30,259
  • 8
  • 73
  • 100
  • Thank you I will test this out :) – Kyslik Feb 22 '16 at 11:39
  • Is it possible ustring test; test = "ABCD"; ? – Kyslik Feb 22 '16 at 12:11
  • 1
    No you cannot do it this way because you don't have a `operator=(const char*)` for `ustring` – Patryk Feb 22 '16 at 15:22
  • @Patryk I figured and I am using one of functions presented by you to convert string to ustring. Thank you. (also you made sense what string really is) +1. – Kyslik Feb 22 '16 at 18:11
  • @Kyslik One way to do this is [`ustring test = reinterpret_cast("abcd");`](http://ideone.com/R6Xpjd) though I won't recommend it. Another is the converter function as you mentioned. – Mohit Jain Feb 23 '16 at 05:36
4

std::string in reality is just a std::basic_string<char>

What you need is a std::basic_string<unsigned char>

There are some nice conversion methods in this answer from this SO thread

#include <string>
#include <iostream>

typedef std::basic_string<unsigned char> ustring;

inline ustring convert(const std::string& sys_enc) {
  return ustring( sys_enc.begin(), sys_enc.end() );
}

template< std::size_t N >
inline ustring convert(const char (&array)[N]) {
  return ustring( array, array+N );
}

inline ustring convert(const char* pstr) {
  return ustring( reinterpret_cast<const ustring::value_type*>(pstr) );
}

std::ostream& operator<<(std::ostream& os, const ustring& u)
{
    for(auto c : u)
      os << c;
    return os;
}

int main()
{
    ustring u1 = convert(std::string("hello"));
    std::cout << u1 << '\n';

    // -----------------------
    char chars[] = { 67, 43, 43 };
    ustring u2 = convert(chars);
    std::cout << u2 << '\n';

    // -----------------------
    ustring u3 = convert("hello");
    std::cout << u3 << '\n';
}

coliru

Community
  • 1
  • 1
Patryk
  • 22,602
  • 44
  • 128
  • 244
1

Simply use a:

std::vector<unsigned char> v;
Paul Evans
  • 27,315
  • 3
  • 37
  • 54