从 C++ 中的 std::string 获取字节
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/505021/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get bytes from std::string in C++
提问by Vic
I'm working in a C++ unmanaged project.
我在一个 C++ 非托管项目中工作。
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.
我需要知道如何获取像“一些要加密的数据”这样的字符串并获得一个 byte[] 数组,我将使用它作为 Encrypt 的源。
In C# I do
在 C# 中我做
for (int i = 0; i < text.Length; i++)
buffer[i] = (byte)text[i];
What I need to know is how to do the same but using unmanaged C++.
我需要知道的是如何使用非托管 C++ 做同样的事情。
Thanks!
谢谢!
回答by Johannes Schaub - litb
If you just need read-only access, then c_str()
will do it:
如果您只需要只读访问权限,则c_str()
可以这样做:
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('unsigned char buffer[mystring.length()];
memcpy(buffer, mystring.data(), mystring.length());
');
char *c = &bytes[0];
回答by Christopher Smith
std::string::data
would seem to be sufficient and most efficient. If you want to have non-const memory to manipulate (strange for encryption) you can copy the data to a buffer using memcpy:
std::string::data
似乎是足够和最有效的。如果您想操作非常量内存(加密很奇怪),您可以使用memcpy将数据复制到缓冲区:
std::copy(mystring.begin(), mystring.end(), buffer);
STL fanboys would encourage you to use std::copyinstead:
STL 粉丝会鼓励你使用std::copy来代替:
encrypt(const void *ptr, size_t bufferSize);
but there really isn't much of an upside to this. If you need null termination use std::string::c_str()
and the various string duplication techniques others have provided, but I'd generally avoid that and just query for the length
. Particularly with cryptography you just know somebody is going to try to break it by shoving nulls in to it, and using std::string::data()
discourages you from lazily making assumptions about the underlying bits in the string.
但这确实没有太大的好处。如果您需要使用空终止std::string::c_str()
以及其他人提供的各种字符串复制技术,但我通常会避免这种情况,只需查询length
. 特别是对于密码学,你只知道有人会试图通过将空值推入其中来破坏它,并且使用会std::string::data()
阻止你懒惰地对字符串中的底层位做出假设。
回答by Vic
Normally, encryption functions take
通常,加密函数需要
encrypt(strng.c_str(), strng.length());
as arguments. You can pass c_str and length directly:
作为论据。您可以直接传递 c_str 和 length:
length = str.copy( buffer, str.size() );
This way, extra space is allocated or wasted.
这样,额外的空间被分配或浪费。
回答by Nick Haddad
From a std::string you can use the c_ptr() method if you want to get at the char_t buffer pointer.
如果要获取 char_t 缓冲区指针,可以从 std::string 使用 c_ptr() 方法。
It looks like you just want copy the characters of the string into a new buffer. I would simply use the std::string::copyfunction:
看起来您只想将字符串的字符复制到新缓冲区中。我会简单地使用std::string::copy函数:
encrypt(str.data(),str.size());
回答by Martin York
If you just need to read the data.
如果你只需要读取数据。
std::vector<byte> source(str.begin(),str.end());
encrypt(&source[0],source.size());
If you need a read/write copy of the data put it into a vector. (Don;t dynamically allocate space that's the job of vector).
如果您需要数据的读/写副本,请将其放入向量中。(不要动态分配空间,这是矢量的工作)。
strcpy(buffer, text.c_str());
Of course we are all assuming that byte is a char!!!
当然,我们都假设 byte 是一个字符!!!
回答by Mark
If this is just plain vanilla C, then:
如果这只是普通的香草 C,那么:
encrypt(text.c_str())
Assuming that buffer is allocated and large enough to hold the contents of 'text', which is the assumption in your original code.
假设缓冲区已分配并且足够大以容纳“文本”的内容,这是原始代码中的假设。
If encrypt() takes a 'const char *' then you can use
如果 encrypt() 使用 'const char *' 那么你可以使用
string str = "some text;
byte[] bytes = System.Text.Encoding.ASCII.GetBytes(str);
and you do not need to copy the string.
并且您不需要复制字符串。
回答by Hippiehunter
I dont think you want to use the c# code you have there. They provide System.Text.Encoding.ASCII(also UTF-*)
我认为您不想使用那里的 c# 代码。他们提供 System.Text.Encoding.ASCII(也是 UTF-*)
##代码##your problems stem from ignoring the encoding in c# not your c++ code
您的问题源于忽略 c# 中的编码而不是您的 c++ 代码