I am writing a program that sends ascii text out the serial port to a simple display device.
The device talks 8-N-1 but we all know ascii only needs 7 bits for numbers and letters on the
first ASCII chart; so the display has a "value added" feature that if the eight bit is set high,
then that character is blinking. I know setting the eight bit will add 128 to the decimal
value of the character so this is the code I wrote:
char c_Letter;
char c_LetterBlinking;
c_LetterBlinking = (c_Letter + 128);
My question is: Although this does the job and I send the letters out the port; my MS Vis C++ 6.0 compiler gives me a warning on the equals sign telling me:
"converting from int to char, possible loss of data"
So, there must be a better way.....too bad the compiler can't just come out and show me how it thinks I should be writing code.
FrancisPosted May 10, 2009, 4:26 PM
OK I'm still new and all the attempts I made with unicode have failed, so now
I am thinking about using the "....bitwise-inclusive-OR operator which compares each
bit of its first operand to the corresponding bit of its second operand. If either
bit is 1, the corresponding result bit is set to 1. Otherwise, the
corresponding result bit is set to 0......"
So my plan to get the blinking char goes something like this:
char c_Letter; //the letter to start with
char c_LetterBlinking; //the equivalent char but with the 8th bit set
int n_Letter; //a var to hold the result of atoi
int n_One28 = 0x80; //128 in hexadecimal the eighth bit is set high
1 call on atoi() to convert to the char to a different "encoding system" like int
n_Letter = atoi(c_Letter);
2 Make a mask which has the 8th bit high which would be 128 (8 x 16 = 80)
int n_mask = 0x80;
3 have the bitwise-inclusive -OR operator compare the two and set the eight bit high
4 n_LetterBlinking = n_Letter | n_One28;
5 then I could convert back if I wanted to
6 c_LetterBlinking = itoa(n_LetterBlinking);
Bechir BejaouiPosted May 4, 2009, 5:54 PM
Ok I expalain the problem when you make c_letter + 128 the compiler see the 128 and then it converts the c_letter +128 to int then he finds the assignament c_char <--- c_letter + 128 so it puts and int which is larger than char in c_char so some data will be lost if you are over the 128 bit size
so it's not a good solution to do that in the System.Text namespace you can find good classes that could help you to make translations from an encoding type to another you can leverage a method that make an intermediate or conversion between different encoding without loosing the information when translating from encoding to another. The Encoding class is one among them