Snippet. CPP. How to Check, If a QString is Base64 EncodedThe following CPP snippet checks, if a QString is Base64 encoded. It checks both for illegal characters and the length of the QString. Due to the limitations of the encoding, the is_base64 function is not 100% accurate for strings consisting of one word. For instance, any word with four Latin characters (i.e. milk, halo, etc.) is a valid Base64 encoding. However, for strings consisting of 2 and more words the accuracy is 100%. bool utils::is_base64(QString string) { QRegExp rx("[^a-zA-Z0-9+/=]"); if(rx.indexIn(string)==-1 && (string.length()%4) == 0 && string.length()>=4){ return true; } return false; } FYI, the valid characters in the Base64 encoding are ABCDEFGHIJKLMNOPQRSTUVWXYZ, abcdefghijklmnopqrstuvwxyz, 0123456789,+ and /. And the regular expression to check for these are [^a-zA-Z0-9+/=]. Updated on: 23 Nov 2024 |
|