无符号字符串 [英] Strings of unsigned chars

查看:176
本文介绍了无符号字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个有趣的。我正在编写AES加密算法,并设法使其进行准确的加密。当我尝试将结果写入文件时出现问题。我得到的文件输出不正确。十六进制值将被破坏,而且通常是无意义的(即使是通过加密标准)。



在将其加密输出发送到文件之前,我对其进行了一些调试。我发现我在某个地方遇到某种类型的溢出当正确的十六进制值应该是9e时,我会得到ffffff9e。它将仅对高于7F的十六进制值执行此操作,即扩展字符集中的字符未被正确处理。这在我的项目中也发生在我以前,然后问题是使用一个char [] []容器而不是一个unsigned char [] []容器。



我的代码使用字符串在用户界面和AES加密类之间传递加密数据。我猜猜std :: strings不支持扩展字符集。所以我的问题是:有没有办法来实例化一个无符号的字符串,还是需要找到一种替代我所有使用字符串的方法?

解决方案

std :: string 真的只是一个typedef,像:

  namespace std {
typedef basic_string< char>串;
}

unsigned char创建一个变体是相当容易的

  typedef basic_string< unsigned char> ustring; 

然而,您将不得不更改代码以使用 ustring (或任何您喜欢的名称),而不是 std :: string



根据您编写代码的方式,该可能不需要编辑所有代码。特别是,如果你有这样的东西:

  namespace crypto {
using std :: string;

class AES {
string data;
// ..
};
}

您可以通过更改来更改字符串类型使用声明:

  namespace unsigned_types {
typedef std :: basic_string< unsigned char>串;
}

// ...

命名空间crypto {
using unsigned_types :: string;

class AES {
string data;
};
}

还要注意,模板的不同实例化是完全独立的类型,即使类型是相关的,所以事实上你可以在 char unsigned char 之间隐式转换这意味着您将获得 basic_string< char> basic_string< unsigned char> 之间的匹配隐式转换。 / p>

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 result to a file. I was getting files with incorrect output. Hex values would be mangled and it was just generally nonsensical (even by encrypted standards).

I did some debugging by sampling my encryption output before sending it to the file. What I found was that I was getting some type of overflow somewhere. When the correct hex value was supposed to be 9e, I would get ffffff9e. It would do this only to hex values above 7F, i.e. characters in the "extended" character set weren't being handled properly. This had happened to me earlier in my project as well, and the problem then had been using a char[][] container instead of an unsigned char[][] container.

My code uses strings to pass the encrypted data between the user interface and AES encryption class. I'm guessing that std::strings don't support the extended character set. So my question is: is there a way to instantiate an unsigned string, or will I have to find a way to replace all of my usage of strings?

解决方案

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

namespace std { 
   typedef basic_string<char> string;
}

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

typedef basic_string<unsigned char> 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<unsigned char> 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<char> and basic_string<unsigned char>.

这篇关于无符号字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆