Adding together ASCII(h) representation of bytes in a array<Byte>

Talha

Junior Member
Aug 19, 2017
1
0
1
I am trying to send a message consisting of a few byte using visual studios c++. The device requires serial communication and I am using array<Byte>: https://stackoverflow.com/questions/32770503/serial-port-write-c

The message to be send should start with 02 ASCII(h) i.e. STX. This is followed by 'A1C0' which when send using array<Byte> would correspond to 0x41, 0x31, 0x43, 0x30 in ASCII(h).

The other user input value is for a variable kV which is converted into hex after user provide it; e.g. if a user enter 7700, my code is supposed to convert it to a string of hex i.e. '1E14' which when sent using array<Byte> should correspond to 0x31, 0x45, 0x31, 0x34.

The issue I am faced with is that to calculate the checksum, I am required to add all the ASCII(h) which my array<Byte> consist of and then take the total’s one complement. I am struggling to add all these ASCII(h) primarily because I am sending ‘A1C0’ and ‘1E14’ as hex and depends on computer to interpret them in ASCII(h). In this situation I am struggling, with how to add all these ASCII(h) which in described case would require me to add ASCII(h): 2+41+31+43+30+31+45+31+34 = 1C2 and then take the total’s one’s complement i.e 3D which in ASCII(h) is represented 0x33, 0x44.

Following the checksum, I am required to send 03, i.e. ETX to tell the device that the message has ended. So the complete message to be send will contain STX, A1C0, kV, checksum and ETX which from my given explanation in this example will look like: 0x02, 0x41, 0x31, 0x43, 0x30, 0x31, 0x45, 0x31, 0x34, 0x33, 0x44, 0x03.

I know to add elements in an array as described here: http://www.cplusplus.com/forum/windows/32209/ but that is not simply what I want. Before reaching out here: I tried to also look online, but was not able to accomplish what I wanted:
https://social.msdn.microsoft.com/F...3a/sum-of-char-array-as-bytes?forum=vcgeneral
https://stackoverflow.com/questions/30223983/c-add-up-all-bytes-in-a-structure
http://forums.codeguru.com/showthread.php?383059-Perform-hex-addition-in-C-C

For the one’s complement, my code is based on: https://msdn.microsoft.com/en-us/library/dxt4z71k.aspx

I will really appreciate some help here! My current code is below:
Code:
static void message(SerialPort^ port){

array<Byte>^ message = gcnew array<Byte>

message[0] = 0x02; // STX

message[1] = 'A'; // A in ASCII(h) represents 0X41

message[2] = '1'; // 1 in ASCII(h) represents 0x31

message[3] = 'C'; // C n ASCII(h) represents 0x43

message[4] = '0'; // 0 in ASCII(h) represents 0x30



kV<< std::hex << decimal_value; // user entered int decimal_value for kV using Cin - for example user entered 7700, converted to hex --- hex value of 7700 is 1E14

std::string power ( kV.str() );

message[5] = power; // ASCII(h) of power (which is hex 1E14) represents 0x31, 0x45, 0x31, 0x34

unsigned short CHKSUM = ???; // PROBLEM: Calculating total --- adding all the Ascii(h) bytes in the array together

cout << hex << CHKSUM << endl;

CHKSUM = ~CHKSUM; // Take one's complement

cout << hex << CHKSUM << endl;

std::stringstream CHKSUM;

message[6] = CHKSUM;

message[7] = 0x03; // ETX



port->Write(message, 0, message->Length);

}
 
Last edited:
sale-70-410-exam    | Exam-200-125-pdf    | we-sale-70-410-exam    | hot-sale-70-410-exam    | Latest-exam-700-603-Dumps    | Dumps-98-363-exams-date    | Certs-200-125-date    | Dumps-300-075-exams-date    | hot-sale-book-C8010-726-book    | Hot-Sale-200-310-Exam    | Exam-Description-200-310-dumps?    | hot-sale-book-200-125-book    | Latest-Updated-300-209-Exam    | Dumps-210-260-exams-date    | Download-200-125-Exam-PDF    | Exam-Description-300-101-dumps    | Certs-300-101-date    | Hot-Sale-300-075-Exam    | Latest-exam-200-125-Dumps    | Exam-Description-200-125-dumps    | Latest-Updated-300-075-Exam    | hot-sale-book-210-260-book    | Dumps-200-901-exams-date    | Certs-200-901-date    | Latest-exam-1Z0-062-Dumps    | Hot-Sale-1Z0-062-Exam    | Certs-CSSLP-date    | 100%-Pass-70-383-Exams    | Latest-JN0-360-real-exam-questions    | 100%-Pass-4A0-100-Real-Exam-Questions    | Dumps-300-135-exams-date    | Passed-200-105-Tech-Exams    | Latest-Updated-200-310-Exam    | Download-300-070-Exam-PDF    | Hot-Sale-JN0-360-Exam    | 100%-Pass-JN0-360-Exams    | 100%-Pass-JN0-360-Real-Exam-Questions    | Dumps-JN0-360-exams-date    | Exam-Description-1Z0-876-dumps    | Latest-exam-1Z0-876-Dumps    | Dumps-HPE0-Y53-exams-date    | 2017-Latest-HPE0-Y53-Exam    | 100%-Pass-HPE0-Y53-Real-Exam-Questions    | Pass-4A0-100-Exam    | Latest-4A0-100-Questions    | Dumps-98-365-exams-date    | 2017-Latest-98-365-Exam    | 100%-Pass-VCS-254-Exams    | 2017-Latest-VCS-273-Exam    | Dumps-200-355-exams-date    | 2017-Latest-300-320-Exam    | Pass-300-101-Exam    | 100%-Pass-300-115-Exams    |
http://www.portvapes.co.uk/    | http://www.portvapes.co.uk/    |