从C ++中的std :: string获取字节 [英] Get bytes from std::string in C++
问题描述
我在一个C ++非托管项目中工作。
I'm working in a C++ unmanaged project.
我需要知道如何使用这样的字符串一些数据加密 []数组,我将用作加密源。
I need to know how can I take a string like this "some data to encrypt" and get a byte[] array which I'm gonna use as the source for Encrypt.
在C#中,
for (int i = 0; i < text.Length; i++)
buffer[i] = (byte)text[i];
我需要知道的是如何做同样的,但使用非托管C ++。
What I need to know is how to do the same but using unmanaged C++.
谢谢!
推荐答案
如果您只需要只读访问权限, =http://en.cppreference.com/w/cpp/string/basic_string/c_str> c_str()
会这样做:
If you just need read-only access, then c_str()
will do it:
char const *c = myString.c_str();
如果需要读/写访问权限,则可以将字符串复制到向量中。向量管理动态内存为您。你不必乱搞分配/释放,然后:
If you need read/write access, then you can copy the string into a vector. vectors manage dynamic memory for you. You don't have to mess with allocation/deallocation then:
std::vector<char> bytes(myString.begin(), myString.end());
bytes.push_back('\0');
char *c = &bytes[0];
这篇关于从C ++中的std :: string获取字节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!