Pages: [1]   Go Down
Author Topic: Sum of ASCII values of characters in a string  (Read 1395 times)
0 Members and 1 Guest are viewing this topic.
New Zealand
Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello,

My Arduino Mega periodically reads the voltage applied to one of its analog inputs. I then form a string which is to be transmitted out on one of the serial TX pins into another hardware device. Various parameters make up the whole string but I must write a checksum to the end of the string.

To calculate the checksum, I need to add the ASCII code for all characters in the string and then perform calculations. The result of the checksum is then placed at the end of the string and transmitted. An example string could be s0DFFID1234562.7 and I can form this okay and place it in a String variable.

I cannot find how to place the ASCII code for each character into a variable so I can do the number crunching. Have Googled etc but still struggling.
Logged

Manchester (England England)
Online Online
Brattain Member
*****
Karma: 619
Posts: 33962
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Use a char* array instead of a string, then you just pick them off one at a time:-

Code:
char* myArray [] = "This is my string";
sum = int(myArray[0]) + int(myArray[1]) + int(myArray[2]) ......

Well you will use a loop and index but you get the idea.
« Last Edit: January 26, 2013, 04:29:33 pm by Grumpy_Mike » Logged

New Zealand
Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Excellent, thank you.
Logged

Offline Offline
Faraday Member
**
Karma: 62
Posts: 3007
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Many applications also use the bitwise exclusive or to form the checksum.  You can run into
issues over whether the char is signed or unsigned  and  how much of the checksum you actually use.
Logged

New Zealand
Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I have data stored in String variables already that I would like to move one character at a time into the array but get the message "cannot convert 'String' to 'char' in assignment". Does that mean that I can never extract a single substring character and assign it to an array location.
Logged

Central MN, USA
Offline Offline
Tesla Member
***
Karma: 73
Posts: 7188
Phi_prompt, phi_interfaces, phi-2 shields, phi-panels
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

can you just not use String and use char array instead?
Logged


New Zealand
Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I am working to just use the array but am new to this so solving the problems one by one. I am using an array to hold most of the individual characters that make up the whole command but I am struggling with getting the voltage value into the array as each number being a character. Please see my code below which gets the three digits from the voltage I am reading.

Code:
 // calculate first voltage digit
  int digit1Int = int(voltage);
  Serial.print("Voltage digit one :");
  Serial.println(digit1Int);
  //Serial.println();
  
  // calculate second voltage digit
  int digit2Int = int(10*(voltage-digit1Int));
  Serial.print("Voltage digit two :");
  Serial.println(digit2Int);
  //Serial.println();  
  
  // calculate third voltage digit
  int digit3Int = int((100*voltage)-(int(voltage*10)*10));
  Serial.print("Voltage digit three :");
  Serial.println(digit3Int);
  
// set array values locations to hold the command
  commandArray[0] = 's';    // s for send sdm
  commandArray[1] = '0';    // command size
  commandArray[2] = 'D';    // command size
  commandArray[3] = '1';    // delay
  commandArray[4] = '9';    // delay
  commandArray[5] = '1';    // destination
  commandArray[6] = '2';    // destination
  commandArray[7] = '3';    // destination
  commandArray[8] = '4';    // destination
  commandArray[9] = '5';    // destination
  commandArray[10] = '6';    // destination
  commandArray[11] = '7';    // destination
  commandArray[12] = '8';    // destination
  commandArray[13] = digit1Int;    // voltage
  commandArray[14] = digit2Int;    // voltage
  commandArray[15] = digit3Int;    // voltage
  
  // print out the values
  for (int loopCntr=0; loopCntr <= 15; loopCntr++)
  {
    Serial.println(commandArray[loopCntr]);
    Serial.println(int(commandArray[loopCntr]));

    
  }

 When I run this it does not print the voltage information correctly. Thank you for your help.


Moderator edit: [code] [/code] tags added.
« Last Edit: January 26, 2013, 07:48:51 pm by Coding Badly » Logged

Pittsburgh, PA, USA
Offline Offline
Faraday Member
**
Karma: 98
Posts: 4801
I learn a bit every time I visit the forum.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
 // calculate first voltage digit

// set array values locations to hold the command
  commandArray[0] = 's';    // s for send sdm
  commandArray[1] = '0';    // command size
  commandArray[2] = 'D';    // command size
  commandArray[3] = '1';    // delay
  commandArray[4] = '9';    // delay
  commandArray[5] = '1';    // destination
  commandArray[6] = '2';    // destination
  commandArray[7] = '3';    // destination
  commandArray[8] = '4';    // destination
  commandArray[9] = '5';    // destination
  commandArray[10] = '6';    // destination
  commandArray[11] = '7';    // destination
  commandArray[12] = '8';    // destination
  commandArray[13] = digit1Int;    // voltage
  commandArray[14] = digit2Int;    // voltage
  commandArray[15] = digit3Int;    // voltage
  

Is commandArray[] ints or chars?

« Last Edit: January 26, 2013, 08:00:04 pm by GoForSmoke » Logged

I find it harder to express logic in English than in Code.
Sometimes an example says more than many times as many words.

New Zealand
Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello GoForSmoke, please see the code below where the array is char.

void setup() {
  // initialize both serial ports. Serial prints to PC
  Serial.begin(9600);
 
  // initialise the serial port to send data to the radio
  Serial1.begin(9600);
}

void loop() {
  // initialise variables
  String voltageStr;
  String sizeStr;
  int commandSizeInt;
  String commandStr;
  String commandSizeHex;
  String commandSizeStr;
  int loopCounter;
  int commandSubStringInt;
  String commandSubStringString;
  char commandChr;
  char commandArray[20];
 
  // read the input on analog pin 0:
  int sensorValue = analogRead(A0);
 
  // calculate the actual voltage
  float voltage = sensorValue * (5.0 / 1023.0);
 
  // print out the values above:
  Serial.print("Sensor value 0-1023 :");
  Serial.println(sensorValue);
  Serial.print("Calculated voltage :");
  Serial.println(voltage);
 
  // calculate first voltage digit
  int digit1Int = int(voltage);
  Serial.print("Voltage digit one :");
  Serial.println(digit1Int);
  //Serial.println();
 
  // calculate second voltage digit
  int digit2Int = int(10*(voltage-digit1Int));
  Serial.print("Voltage digit two :");
  Serial.println(digit2Int);
  //Serial.println(); 
 
  // calculate third voltage digit
  int digit3Int = int((100*voltage)-(int(voltage*10)*10));
  Serial.print("Voltage digit three :");
  Serial.println(digit3Int);
 
  // set array values locations to hold the command
  commandArray[0] = 's';    // s for send sdm
  commandArray[1] = '0';    // command size
  commandArray[2] = 'D';    // command size
  commandArray[3] = '1';    // delay
  commandArray[4] = '9';    // delay
  commandArray[5] = '0';    // destination
  commandArray[6] = '8';    // destination
  commandArray[7] = '0';    // destination
  commandArray[8] = '0';    // destination
  commandArray[9] = 'T';    // destination
  commandArray[10] = 'E';    // destination
  commandArray[11] = 'S';    // destination
  commandArray[12] = 'T';    // destination
  commandArray[13] = digit1Int;    // voltage
  commandArray[14] = digit2Int;    // voltage
  commandArray[15] = digit3Int;    // voltage
 
  for (int loopCntr=0; loopCntr <= 15; loopCntr++)
  {
    Serial.println(commandArray[loopCntr]);
    Serial.println(int(commandArray[loopCntr]));
  }

  delay(5000);
 
}
Logged

UK
Offline Offline
Shannon Member
****
Karma: 223
Posts: 12631
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

How about:

Code:
    sprintf(commandArray, "s0D1912345678%03d", voltage);

The %03d is replaced with a three digit decimal representation of the value of voltage, padded with leading zeros if necessary.
Logged

I only provide help via the forum - please do not contact me for private consultancy.

New Zealand
Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thank you to all that have replied. I will experiment with your advice.
Logged

Offline Offline
Faraday Member
**
Karma: 62
Posts: 3007
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

int voltage=240 ;
char buffer[4] ;
sprintf(buffer,"%03d",voltage);
buffer[3]='\0' ;

// And to calculate the checksum
byte cs=0 ;
char* p=buffer ;
while(*p != '\0' )
{
    cs ^= *p ;
    p++ ;
}
Logged

Pittsburgh, PA, USA
Offline Offline
Faraday Member
**
Karma: 98
Posts: 4801
I learn a bit every time I visit the forum.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello GoForSmoke, please see the code below where the array is char.
Code:
  commandArray[13] = digit1Int;    // voltage
  commandArray[14] = digit2Int;    // voltage
  commandArray[15] = digit3Int;    // voltage
}
[/quote]

Then stuffing 16-bit ints into 8-bit chars is probably not a good idea if you want the answers to make sense.

Logged

I find it harder to express logic in English than in Code.
Sometimes an example says more than many times as many words.

New Zealand
Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thank you to all who have responded with advice and code samples. I have solved the problem using your responses. Best regards.
Logged

Pages: [1]   Go Up
Jump to: