I've build an EEPROM (Kind of like a very low tech USB stick) Programmer and I'm writing a program witch reads text from a txt file and then converts it into bin/hex that the programmer can use to program the data onto the EEPROM. I've got everything except for a function that converts the string into hex. I've tried using this code which works somewhat well.
string Text = "This is a string.";
for(int i = 0; i < Text.size(); i ) {
cout << uppercase << hex << (int)Text[i] << " ";
}
This will out put this:
54 68 69 73 20 69 73 20 61 20 73 74 72 69 6E 67 2E
But when giving it:
Thïs ìs â stríng.
It wil retun this:
54 68 FFFFFFC3 FFFFFFAF 73 20 FFFFFFC3 FFFFFFAC 73 20 FFFFFFC3 FFFFFFA2 20 73 74 72 FFFFFFC3 FFFFFFAD 6E 67 2E
This doesn't look right to me. My best guess is that normal char are converted to ASCII and the special ones get converted in some form of Unicode. Is there a way to make everything Unicode?
Side note The EEPROM can only hold 2k bytes so the more space efficient the better.
So my end goal is:
- Make a function that turns a string into its hex equivalent. With the end result being space efficient and supporting diacritics.
- Make another function that could read the hex and turn it into a string, also with support for diacritics.
If that is not possible I'm willing to use a custom formatting that would store an 'ê' like "|e^" for example. With an equivalent of "|" as a way for me to intercept a special character.
Thanks for your help!
CodePudding user response:
a double cast is needed here: first cast the character to (unsigned char), then cast to (int):
(int)(unsigned char)Text[i]
this is necessary because casting as (unsigned int)
does not work as you might expect. the signed char value is first widened, then the cast is applied, but at that point, the sign extension has already been performed.
see this on https://godbolt.org/z/GhYz3T8v6