Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 将字符拆分为字符数组_C++_Arrays_String_Char_Substring - Fatal编程技术网

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样式的字符串