-2

I want to ask that how can I store hex values from a string into an integer array. e.g 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.

dbush
  • 205,898
  • 23
  • 218
  • 273
A.Saeed
  • 11
  • What have you tried alraedy? – Sourav Ghosh Nov 12 '18 at 13:28
  • Welcome to Stack Overflow. Please read [the help pages](http://stackoverflow.com/help), take [the SO tour](http://stackoverflow.com/tour), read about [how to ask good questions](http://stackoverflow.com/help/how-to-ask), as well as [this question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). Lastly learn how to create a [Minimal, Complete, and Verifiable Example](http://stackoverflow.com/help/mcve). Oh and please don't spam unrelated languages tags. If you program in C++ then use only that tag. – Some programmer dude Nov 12 '18 at 13:28
  • Use `strtok` to break up the string, then `strtol` to convert the values. – dbush Nov 12 '18 at 13:32
  • [Post what you're **fully** trying to do, not just a subproblem](http://xyproblem.info/). Qt provides some special functionalities that normal C++ doesn't naturally provide. Letting us know this can help us help you... – TrebledJ Nov 12 '18 at 13:43
  • normal C++ way: https://stackoverflow.com/q/17261798/103167 – Ben Voigt Apr 29 '19 at 22:17

1 Answers1

0

Easy way using 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;
}
deMax
  • 86
  • 7