简体   繁体   English

从字符串中读取十六进制值并将它们存储到 c/c++ 中的整数数组中

[英]Reading hex values from a String and storing them into an integer array in c/c++

I want to ask that how can I store hex values from a string into an integer array.我想问一下如何将字符串中的十六进制值存储到整数数组中。 eg coverting例如覆盖

String sbox_str= "0x65, 0xea, 0xaf, 0x37, 0xff, 0x3b, 0xc2, 0xd0";

into进入

uint8_t sbox[8]={0x65, 0xea, 0xaf, 0x37, 0xff, 0x3b, 0xc2, 0xd0};

I will really appreciate if you guide me about how can i do the same thing in QT Creator.如果您指导我如何在 QT Creator 中做同样的事情,我将非常感激。

Easy way using QString:使用 QString 的简单方法:

std::string sbox_str= "0x65, 0xea, 0xaf, 0x37, 0xff, 0x3b, 0xc2, 0xd0";
uint8_t sbox[8];

int i = 0;
for( const auto &item: QString(sbox_str.data()).split(", ")) {
    if(i == sizeof (sbox)) break; // do something
    sbox[i] = item.toInt(nullptr, 16);
    ++i;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM