将字符串转换为二进制的最快方法? [英] Fastest way to Convert String to Binary?
本文介绍了将字符串转换为二进制的最快方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想一个字符串转换,使用String类 - 二进制。什么是字符做此字符的快速方式。循环?或者是有一些功能,在那里,会为我转换? 1和0的二进制代码。
一个字符为:
的#include<串GT;
使用命名空间std;
诠释主(){
MyString的=Hello World的;
}
解决方案
使用和std :: bitset
将工作:
的#include<串GT;
#包括LT&;&位集GT;
#包括LT&;&iostream的GT;
使用命名空间std;
诠释主(){
字符串的myString =Hello World的;
对于(的std ::为size_t我= 0; I< myString.size(); ++ I)
{
COUT<<位集&所述8为氢;(myString.c_str()[I])所述;&下; ENDL;
}
}
输出:
01001000
01100101
01101100
01101100
01101111
00100000
01010111
01101111
01110010
01101100
01100100
I want to convert a string, using the string class - to Binary. What is the fast way to do this character by character. Loop? Or is there some function out there that will convert for me? 1's and 0's binary.
A string being:
#include <string>
using namespace std;
int main(){
myString = "Hello World";
}
解决方案
Using std::bitset
would work:
#include <string>
#include <bitset>
#include <iostream>
using namespace std;
int main(){
string myString = "Hello World";
for (std::size_t i = 0; i < myString.size(); ++i)
{
cout << bitset<8>(myString.c_str()[i]) << endl;
}
}
Output:
01001000
01100101
01101100
01101100
01101111
00100000
01010111
01101111
01110010
01101100
01100100
这篇关于将字符串转换为二进制的最快方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文