Convert NonAscii ByteArray data to QString
I am confused how to convert non-ascii characters from an byte array.Can anyone pls help me to finish this.
Here is my code:
QByteArray data = "0a0a0a0a0aFFFFFFFFFF";
i want to convert above hex data to integer values. I need to convert 5,6,7 byte to an integer.i.e FFFFFF = 16777215.(respective decimal number)
How do i do this ? When i read i get "????" as they are non ascii chars.
@Beemaneni-Bala First you have to know the encoding of the string/characters. Do you know it and what encoding means in general?
Not exactly..But i know about utf8 encoding is what QString supports. Am i correct ?
@Beemaneni-Bala So clearly you don't yet know how encoding works. See for example http://kunststube.net/encoding/ and https://en.wikipedia.org/wiki/Character_encoding. If your byte array represents a string, array of characters, it already has encoding implicitly by nature (but the program can't know it). This means you have to have some kind of knowledge what textual character each group of bytes represent. You have to explicitly tell the program what is the encoding because it doesn't yet know it. Sometimes you can know it for sure, sometimes you have to guess.
See the documentation for QString, search for "encoding" there.
@Beemaneni-Bala The example seems to show that the data is ASCII formatted hex. If this really is the case then you can use QByteArray data; ... data.mid(5,3).toUint(16);
If those a raw bytes (they actually contains 0x0a and 0xff) then, for the example shown, you'd do something like int output_int = (((unsigned) (unsigned char) (data.at(5)) << 16) | (((unsigned) (unsigned char) (data.at(5)) << 8) | (unsigned) (unsigned char) (data.at(5));
You can do much crazier stuff with casting pointers but you'll need to watch out for odd length'd data (you seem to want 24 bit) and byte endianism.
Thank you sir and it worked well...I will get through encoding stuff in mean time
@Beemaneni-Bala Marvellous, if the solves your problem please mark the thread as "Solved".