Strings of unsigned chars

后端 未结 3 1098
猫巷女王i
猫巷女王i 2021-01-16 04:46

Here\'s an interesting one. I\'m writing an AES encryption algorithm, and have managed to get it making accurate encryptions. The trouble comes when I attempt to write the r

3条回答
  •  粉色の甜心
    2021-01-16 05:29

    std::string is really just a typedef, something like:

    namespace std { 
       typedef basic_string string;
    }
    

    It's fairly easy to create a variant for unsigned char:

    typedef basic_string ustring;
    

    You will, however, have to change your code to use a ustring (or whatever name you prefer) instead of std::string though.

    Depending on how you've written your code, that may not require editing all the code though. In particular, if you have something like:

    namespace crypto { 
       using std::string;
    
       class AES { 
          string data;
          // ..
        };
    }
    

    You can change the string type by changing only the using declaration:

    namespace unsigned_types { 
        typedef std::basic_string string;
    }
    
    // ...
    
    namespace crypto {
        using unsigned_types::string;
    
        class AES {
            string data;
        };
    }
    

    Also note that different instantiations of a template are entirely separate types, even when the types over which they're intantiated are related, so the fact that you can convert implicitly between char and unsigned char doesn't mean you'll get a matching implicit conversion between basic_string and basic_string.

提交回复
热议问题