Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/129.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 将字符或字符串转换为c++;_C++_Des - Fatal编程技术网

C++ 将字符或字符串转换为c++;

C++ 将字符或字符串转换为c++;,c++,des,C++,Des,我正在做一个DES加密的作业,我似乎无法转换字符串,更不用说将字符转换为位集了。有人能告诉我如何在C++中将单个字符转换成位集吗?< /P> < P>: char c = 'A'; std::bitset<8> b(c); // implicit cast to unsigned long long 这可能有点低效,但我不知道是否有更好的解决方法。接受字符串进行构造。你考虑过使用它吗?评论应该被接受。 const size_t N = 50; // bound on strin

我正在做一个DES加密的作业,我似乎无法转换字符串,更不用说将字符转换为位集了。有人能告诉我如何在C++中将单个字符转换成位集吗?< /P> < P>:
char c = 'A';
std::bitset<8> b(c);  // implicit cast to unsigned long long

这可能有点低效,但我不知道是否有更好的解决方法。

接受字符串进行构造。你考虑过使用它吗?评论应该被接受。
const size_t N = 50;  // bound on string length
bitset<N * 8> b;
for (int i = 0; i < str.length(); ++i) {
  char c = s[i];
  for (int j = 7; j >= 0 && c; --j) {
    if (c & 0x1) {
      b.set(8 * i + j);
    }
    c >>= 1;
  }
}