Adding together ASCII(h) representation of bytes in an array<Byte>
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: Adding together ASCII(h) representation of bytes in an array<Byte>

  1. #1
    Join Date
    Aug 2017
    Posts
    1

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

    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/...l-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....orum=vcgeneral
    https://stackoverflow.com/questions/...in-a-structure
    http://forums.codeguru.com/showthrea...ddition-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] = 'A1C0';                         // A1C0 in ASCII(h) represents 0X41, 0x31, 0x43, 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[2] = 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[3] = CHKSUM;
    	message[4] = 0x03;                          // ETX
    
    	port->Write(message, 0, message->Length);
    }

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    18,956

    Re: Adding together ASCII(h) representation of bytes in an array<Byte>

    Quote Originally Posted by Talha1369 View Post
    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/...l-port-write-c
    ...
    I will really appreciate some help here! My current code is below:
    Code:
    static void message(SerialPort^ port){
    	array<Byte>^ message = gcnew array<Byte>
    	...;
    }
    As it was already mentioned in https://stackoverflow.com/questions/...l-port-write-c this code looks like managed C++/CLI.

    So I move it to corresponding forum.
    Victor Nijegorodov

  3. #3
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    18,956

    Re: Adding together ASCII(h) representation of bytes in an array<Byte>

    [moved from Visual C++ Programming forum]
    Victor Nijegorodov

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This a Codeguru.com survey!


On-Demand Webinars (sponsored)