char 数组到 uint8_t 数组 [英] char array to uint8_t array
问题描述
这是我从不擅长的 C/C++ 领域.
this is one area of C/C++ that i have never been good at.
我的问题是我有一个字符串,最终需要包含一些空字符.将所有内容都视为字符数组(或字符串)是行不通的,因为当他们找到第一个空值时,事情往往会失败.所以我想,好吧,我会切换到 uint8_t,所以一切都只是一个数字.我可以根据需要移动东西,并在我准备好后将其转换回字符.
my problem is that i have a string that will need to eventually contain some null characters. treating everything as a char array (or string) won't work, as things tend to crap out when they find the first null. so i thought, ok, i'll switch over to uint8_t, so everything is just a number. i can move things around as needed, and cast it back to a char when i'm ready.
我现在的主要问题是:如何将字符串的一部分复制到 uint8_t 缓冲区?
my main question right now is: how can i copy a portion of a string to an uint8_t buffer?
实际上,我想做如下事情:
effectively, i'd like to do something like:
std::string s = "abcdefghi";
uint8_t *val = (uint8_t*)malloc(s.length() + 1);
memset(val, 0, s.length() + 1);
// Assume offset is just some number
memcpy(val + offset, s.substr(1, 5).c_str(), 5);
显然,当我尝试此操作时出现错误.可能有某种技巧可以在 memcpy 的第一个参数中完成(我在网上看到像 (*(uint8_t*)) 这样的东西,但不知道这意味着什么).
obviously, i get an error when i try this. there is probably some sort of trickery that can be done in the first argument of the memcpy (i see stuff like (*(uint8_t*)) online, and have no clue what that means).
有什么帮助吗?
当我在这里时,我如何轻松地将其转换回字符数组?只是将 uint8_t 指针 static_cast 指向 char 指针?
and while i am here, how can i easily cast this back to a char array? just static_cast the uint8_t pointer to a char pointer?
非常感谢.
推荐答案
我想,好吧,我会切换到 uint8_t,所以一切都只是一个数字.
i thought, ok, i'll switch over to uint8_t, so everything is just a number.
这不会让寻找'\0'的算法突然停止,使用char的算法也不会有关注'\0'.用空字符表示结束是 C 字符串的约定,而不是字符数组.uint8_t 可能只是 char 的 typedef.
That's not going to make algorithms that look for a '\0' suddenly stop doing it, nor do algorithms that use char have to pay attention to '\0'. Signaling the end with a null character is a convention of C strings, not char arrays. uint8_t might just be a typedef for char anyway.
正如 Nicol Bolas 指出的那样,std::string 已经能够存储包含空字符的字符串,而无需特殊处理空字符.
As Nicol Bolas points out std::string is already capable of storing strings that contain the null character without treating the null character specially.
至于你的问题,我不确定你指的是什么错误,因为以下工作正常:
As for your question, I'm not sure what error you're referring to, as the following works just fine:
#include <iostream>
#include <string>
#include <cstdint>
#include <cstring>
int main() {
std::string s = "abcdefghi";
std::uint8_t *val = (std::uint8_t*)std::malloc(s.length() + 1);
std::memset(val, 0, s.length() + 1);
int offset = 2;
std::memcpy(val + offset, s.substr(1, 5).c_str(), 5);
std::cout << (val+offset) << '\n';
}
memcpy 行从字符串 s
中取出第二到第六个字符,并将它们复制到 val 中.带有 cout 的行然后打印bcdef".
The memcpy line takes the second through sixth characters from the string s
and copies them into val. The line with cout then prints "bcdef".
当然这是 C++,所以如果你想手动分配一些内存并将其清零,你可以这样做:
Of course this is C++, so if you want to manually allocate some memory and zero it out you can do so like:
std::unique_ptr<uint8_t[]> val(new uint8_t[s.length()+1]());
或使用向量:
std::vector<uint8_t> val(s.length()+1,0);
要从 uint8_t 数组进行转换,您可以(但通常不应该)执行以下操作:
To cast from an array of uint8_t you could (but typically shouldn't) do the following:
char *c = reinterpret_cast<uint8_t*>(val);
这篇关于char 数组到 uint8_t 数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!