C++ 将字符拆分为字符数组
我有一个字符字符串:C++ 将字符拆分为字符数组,c++,arrays,string,char,substring,C++,Arrays,String,Char,Substring,我有一个字符字符串: uint8_t word[40] = "a9993e364706816aba3e25717850c26c9cd0d89d"; 我需要以某种方式将其拆分为char数组,因此它看起来如下所示: uint32_t hfFile[5]; hfFile[0] = 0xa9993e36; hfFile[1] = 0x4706816a; hfFile[2] = 0xba3e2571; hfFile[3] = 0x7850c26c; hfFile[4] = 0x9cd
uint8_t word[40] = "a9993e364706816aba3e25717850c26c9cd0d89d";
我需要以某种方式将其拆分为char数组,因此它看起来如下所示:
uint32_t hfFile[5];
hfFile[0] = 0xa9993e36;
hfFile[1] = 0x4706816a;
hfFile[2] = 0xba3e2571;
hfFile[3] = 0x7850c26c;
hfFile[4] = 0x9cd0d89d;
int length = strlen(word);
for(int i = 0; (i*8) < length; i++)
{
strncpy(hfFile[i], word + i*8, 8);
}
稍后我要检查,其他数组元素是否等于hfFile的元素
我的问题是,我不知道如何从char字符串中提取精确的部分,如果
another_array[0] = 0xa9993e36;
如下所示?使用,然后,您可以使用来执行此操作:
std::string word = "a9993e364706816aba3e25717850c26c9cd0d89d";
std::string hfFile[5]; // or use std::vector instead of C-array
hfFile[0] = word.substr(0, 8);
hfFile[1] = word.substr(8, 16);
hfFile[2] = word.substr(16, 24);
hfFile[3] = word.substr(24, 32);
hfFile[4] = word.substr(32, 40);
然后比较就可以这么简单了:
if(hfFile[0] == "a9993e36")
std::cout << "equal\n";
遵循这种方法不会影响性能。使用优化标志编译,您会很好。我怀疑您是这里过早优化的受害者。使用,然后,您可以使用来执行此操作:
std::string word = "a9993e364706816aba3e25717850c26c9cd0d89d";
std::string hfFile[5]; // or use std::vector instead of C-array
hfFile[0] = word.substr(0, 8);
hfFile[1] = word.substr(8, 16);
hfFile[2] = word.substr(16, 24);
hfFile[3] = word.substr(24, 32);
hfFile[4] = word.substr(32, 40);
然后比较就可以这么简单了:
if(hfFile[0] == "a9993e36")
std::cout << "equal\n";
遵循这种方法不会影响性能。使用优化标志编译,您会很好。我怀疑您是这里过早优化的受害者。您可以这样做:
uint32_t hfFile[5];
hfFile[0] = 0xa9993e36;
hfFile[1] = 0x4706816a;
hfFile[2] = 0xba3e2571;
hfFile[3] = 0x7850c26c;
hfFile[4] = 0x9cd0d89d;
int length = strlen(word);
for(int i = 0; (i*8) < length; i++)
{
strncpy(hfFile[i], word + i*8, 8);
}
+2用于跳过另一个_数组开头的0x
请注意,此代码不包含任何错误检查
还请注意,我建议使用std::string而不是C样式的字符串 您可以这样做:
uint32_t hfFile[5];
hfFile[0] = 0xa9993e36;
hfFile[1] = 0x4706816a;
hfFile[2] = 0xba3e2571;
hfFile[3] = 0x7850c26c;
hfFile[4] = 0x9cd0d89d;
int length = strlen(word);
for(int i = 0; (i*8) < length; i++)
{
strncpy(hfFile[i], word + i*8, 8);
}
+2用于跳过另一个_数组开头的0x
请注意,此代码不包含任何错误检查
还请注意,我建议使用std::string而不是C样式的字符串为什么你使用C风格的字符串而不是C++字符串?用字符来做的速度比字符串快,而且我需要我的代码尽可能快地使用这个方法。您需要数据的副本还是只需要数据的视图?Bob提出了一个好问题。如果您的性能要求如此严格,以至于您无法负担50000个std::string,那么在体面的现代硬件上,它很可能在一毫秒内完成!那么您可能希望完全避免复制,在需要时直接索引原始数据。hfFile的类型是什么?你的例子直接赋值不是合法C++为什么你使用C风格字符串而不是C++字符串?用字符来处理字符串速度更快,我需要我的代码尽可能快地使用这个代码。您需要数据的副本还是只需要数据的视图?Bob提出了一个好问题。如果您的性能要求如此严格,以至于您无法负担50000个std::string,那么在体面的现代硬件上,它很可能在一毫秒内完成!那么您可能希望完全避免复制,在需要时直接索引原始数据。hfFile的类型是什么?您的示例直接赋值不合法c++是否有任何可能的方法避免使用字符串?我希望代码尽可能保持低级别,使用chars onlyA std::string操作应该非常快。不要放弃那些使你的代码变得简单和易于证明的合理工具,除非你知道你必须这样做。50000不是很多次。@user3109672:很明显,这只是缺少的一组。原始字符串中没有0x,只是缺少一些std::stou,这可能是最慢的部分。有没有任何可能避免使用字符串的方法?我希望代码尽可能保持低级别,使用chars onlyA std::string操作应该非常快。不要放弃那些使你的代码变得简单和易于证明的合理工具,除非你知道你必须这样做。50000不是很多次。@user3109672:很明显,这只是缺少的一组。原始字符串中没有0x,只是缺少一些std::stou,这可能是最慢的部分6或8?我没有做数学题;p@gsamaras当前位置我认为应该是正确的,因为如果我是1,我从单词+I*8复制,它从字符8开始。或者你看到了什么问题?@gsamaras:嗯?一次复制八个字节。这是基本的算术。@gsamaras表示int i=0;i<长度;i++hfFile[i/8][i%8]=word[i];也会起作用,但这样做太过分了,但你是对的@BoundaryPosition=6号还是8号?我没有做数学题;p@gsamaras当前位置我认为应该是正确的,因为如果我是1,我从单词+I*8复制,它从字符8开始。或者你看到了什么问题?@gsamaras:嗯?一次复制八个字节。这是基本的算术。@gsamaras表示int i=0;i<长度;i++hfFile[i/8][i%8]=word[i];也会起作用,但这样做太过分了,但你是对的@BoundaryPosition=