從C ++中的std :: string獲取字節

[英]Get bytes from std::string in C++


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 []數組,我將用它作為加密的源。

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!

7 个解决方案

#1


24  

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('\0');
char *c = &bytes[0];

#2


18  

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將數據復制到緩沖區:

unsigned char buffer[mystring.length()];
memcpy(buffer, mystring.data(), mystring.length());

STL fanboys would encourage you to use std::copy instead:

STL粉絲會鼓勵你使用std :: copy代替:

std::copy(mystring.begin(), mystring.end(), buffer);

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.

但這確實沒有太大的好處。如果你需要null終止使用std :: string :: c_str()和其他人提供的各種字符串復制技術,但我通常會避免這種情況,只是查詢長度。特別是對於密碼學,你只知道有人會試圖通過向它推送空值來破解它,並且使用std :: string :: data()會阻止你懶散地對字符串中的底層位進行假設。

#3


3  

Normally, encryption functions take

通常,加密功能需要

encrypt(const void *ptr, size_t bufferSize);

as arguments. You can pass c_str and length directly:

作為論點。您可以直接傳遞c_str和length:

encrypt(strng.c_str(), strng.length());

This way, extra space is allocated or wasted.

這樣,分配或浪費了額外的空間。

#4


1  

From a std::string you can use the c_ptr() method if you want to get at the char_t buffer pointer.

從std :: string中,如果要獲取char_t緩沖區指針,可以使用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::copy function:

看起來你只想將字符串的字符復制到一個新的緩沖區中。我只想使用std :: string :: copy函數:

length = str.copy( buffer, str.size() );

#5


1  

If you just need to read the data.

如果您只需要讀取數據。

encrypt(str.data(),str.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).

如果需要數據的讀/寫副本,請將其放入向量中。 (不要;動態分配作為矢量工作的空間)。

std::vector<byte>  source(str.begin(),str.end());
encrypt(&source[0],source.size());

Of course we are all assuming that byte is a char!!!

當然我們都假設這個字節是個字符!

#6


0  

If this is just plain vanilla C, then:

如果這只是簡單的香草C,那么:

strcpy(buffer, 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.

假設緩沖區已分配並且足夠大以容納“text”的內容,這是原始代碼中的假設。

If encrypt() takes a 'const char *' then you can use

如果encrypt()采用'const char *',那么你可以使用

encrypt(text.c_str())

and you do not need to copy the string.

而且您不需要復制字符串。

#7


-6  

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- *)

string str = "some text;
byte[] bytes = System.Text.Encoding.ASCII.GetBytes(str);

your problems stem from ignoring the encoding in c# not your c++ code

你的問題源於忽略c#中的編碼而不是你的c ++代碼


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2009/02/02/e81ec7ce0b3592c9f5a965c4bca4c19b.html



 
  © 2014-2022 ITdaan.com 联系我们: