decimal to ascii for mathematical purposes

Hi,

I have a small problem where I am required to add the ascii values of characters but display the result as hex.

I am transmitting data via RS232, the data packet consists of 12 characters including line feed, carriage return.

Before transmitting the carriage return, I am required to include a checksum which is to be the sum of the ascii values of dataCharacter2 through to dataCharacter8.

E.G. if the packet was <LF> 1 1 1 1 0 4 5 1 1 0 <CR>, then the sum would be 15

The individual data is derived from separate functions and passed to a dataSend function. The code in the dataSend function is:

int checkSum = word2 + word3 + word4 + word5 + word6 + word7 + word8;

serialPort1->Write( checkSum );

I am sending out the data as shown above and I can see this via hyperterminal on a separate pc.

It writes the checkSum data as the decimal value of each character added up, not the ascii value of each character added up.

Can someone help me to add and send the data as the ascii values added please.

[1222 byte] By [MarcJones] at [2007-12-24]
# 1
Marc Jones wrote:
I have a small problem where I am required to add the ascii values of characters but display the result as hex

.....................

It writes the checkSum data as the decimal value of each character added up, not the ascii value of each character added up.

Can someone help me to add and send the data as the ascii values added please.

You can get the ascii value this way.

int i = 1;
char* str = new char[2];
str = itoa(i , str, 10);
i = str[0]; // i holds the value 49 now which is the ascii value of '1'

To display the value as hex in the user interface you can use itoa with base 16.

Regards

Sahir

SahirShah at 2007-8-31 > top of Msdn Tech,Visual C++,Visual C++ General...
# 2

Thanks for the reply.

I can already do this - I can add both the decimal and hexadecimal values . What I want to do is add the actual ascii values.

So to clarify, if the ascii is '1' ( decimal 49, hex 30 ) I want to add 1, not 49 or 30.

E.G. if the data was 1 1 1, the answer should be 3 not 147 or 90.

....................................2 2 2 would be 6 etc.

MarcJones at 2007-8-31 > top of Msdn Tech,Visual C++,Visual C++ General...
# 3

Marc Jones wrote:
E.G. if the data was 1 1 1, the answer should be 3

Sorry for not reading the post carefully. So in

int checkSum = word2 + word3 + word4 + word5 + word6 + word7 + word8;

What type is word2 , word3 etc. ?

SahirShah at 2007-8-31 > top of Msdn Tech,Visual C++,Visual C++ General...
# 4

I presume they are chars ?
In that case

char a = '1';
char str[2];
str[0] = a;
int v = atoi(str); // v = 1

SahirShah at 2007-8-31 > top of Msdn Tech,Visual C++,Visual C++ General...
# 5
Why not you Simply use Pointer in this case like

char *str ="1";
int a = atoi(str); // value of a is 1

pintu* at 2007-8-31 > top of Msdn Tech,Visual C++,Visual C++ General...