Split hex encoded string



  • How do I split hex encoded string into substrings of 2 characters?
    Example: "806982" -> "80", "69", "82".

    I tried:

    QString s = "806982";
    QRegExp re("[\\da-fA-F]{2}"); // exactly 2 hex digits
    QStringList l = s.split(re);
    

    but it did not work as intended.



  • You can use a regular expression in a loop:

    QString s = "806982";
    QRegExp re("([\\da-fA-F]{2})"); // exactly 2 hex digits
    QStringList l;
    
    int pos = 0;
    
    while ((pos = re.indexIn(s, pos)) != -1) {
        l << re.cap(1);
        pos += re.matchedLength();
    }
    


Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.