Program Struck in for loop

the following program is struck in for loop()

char a[] = { 
  '1','A','2','C','3','5','8','9'};
void setup(){
  Serial.begin(9600);
  int val = calculate();
  Serial.println(val);
  convert_to_hex(val);
}
void loop(){
  //do nothing here
}

int calculate(){
  int j = 0;
  int val[strlen(a)/2];
  for (int i = 0, j = 0 ; i < strlen(a); i+2, j++ ){
    val[j] = conv(a[i],a[i+1]);

  }//end of for loop

  int sum = find_sum(val,sizeof(val));
  return sum;
}//end of calculate() function

int find_sum(const int * val,int length){
  int sum = 0;
  for(int i=0;i<= length;i++){
    sum = sum + val[i];
  }//end of for loop
  return sum; 
}//end of find_sum() function


int conv(char val1,char val2){
  int val_a = toDec(val1);
  int val_b = toDec(val2);
  return (val_a*16) + val_b;
}//end of conv() function

int toDec(char val){
  if(val<='9'){
    return val - '0';
  }
  else {
    return val - '0'-7;
  }

}//end of toDec() function

void convert_to_hex(int val){
  char hex[5];
  utoa((unsigned)val,hex,16);
  int num = Serial.write(hex);
  Serial.println(num);

}//end of convert_to_hex() fucntion

what iam doing wrong in this code ?

anilkunchalaece:
the following program is struck in for loop()

char a[] = { 

‘1’,‘A’,‘2’,‘C’,‘3’,‘5’,‘8’,‘9’};
void setup(){
 Serial.begin(9600);
 int val = calculate();
 Serial.println(val);
 convert_to_hex(val);
}
void loop(){
 //do nothing here
}

int calculate(){
 int j = 0;
 int val[strlen(a)/2];
 for (int i = 0, j = 0 ; i < strlen(a); i+2, j++ ){
   val[j] = conv(a[i],a[i+1]);

}//end of for loop

int sum = find_sum(val,sizeof(val));
 return sum;
}//end of calculate() function

int find_sum(const int * val,int length){
 int sum = 0;
 for(int i=0;i<= length;i++){
   sum = sum + val[i];
 }//end of for loop
 return sum;
}//end of find_sum() function

int conv(char val1,char val2){
 int val_a = toDec(val1);
 int val_b = toDec(val2);
 return (val_a*16) + val_b;
}//end of conv() function

int toDec(char val){
 if(val<=‘9’){
   return val - ‘0’;
 }
 else {
   return val - ‘0’-7;
 }

}//end of toDec() function

void convert_to_hex(int val){
 char hex[5];
 utoa((unsigned)val,hex,16);
 int num = Serial.write(hex);
 Serial.println(num);

}//end of convert_to_hex() fucntion




what iam doing wrong in this code ?

Change

for (int i = 0, j = 0 ; i < strlen(a); i+2, j++ )

to

for (int i = 0, j = 0 ; i < strlen(a); i = i+2, j++ )

The for loop should no longer get “stuck”

strlen(a) is probably not valid.

anilkunchalaece: what iam doing wrong in this code ?

All manner of dreadful things.

What is it you're trying to do?

michinyon: strlen(a) is probably not valid.

strlen(a) is certainly not valid. strlen takes a string and a is most definitely not a string.

a[] is a char array, so it will "work".

Or at least, it is a valid function call. The problem is, the way the OP has initialised a[], there is no null char terminating it, which the strlen( ) function required to determine how long the "string" is. The byte in RAM after the byte containing the OP's character '9' might be zero, or it could be anything. And the byte after that.....

If the OP had written

char a[] = {"123456789"} ;

then the compiler would allocate ten bytes and put a byte value 0 in the tenth one.

The way he has written it, the compiler will only allocate 9 bytes. That would be OK, provided that you never use any function, including strlen() and print(), which relies on the presence of the null byte.

the problem is

for (int i = 0, j = 0 ; i < strlen(a); i+2, j++ ){

should be

for (int i = 0, j = 0 ; i < strlen(a);i= i+2, j++ ){

and it took me while to notice it.

yaafm:
All manner of dreadful things.

What is it you’re trying to do?

I think I need to travel through all those thing before learning anything.

i am trying to get the value for LRC for MODBUS ASCII protocol

evil-noxx:
Change

for (int i = 0, j = 0 ; i < strlen(a); i+2, j++ )

to

for (int i = 0, j = 0 ; i < strlen(a); i = i+2, j++ )

The for loop should no longer get “stuck”

Thankyou @evil-noxx

michinyon: a[] is a char array, so it will "work".

No it won't. OK so there's a 1 in 256 chance of it having a zero at the end. If by "work" you mean like playing Russian roulette is "work" then OK.

michinyon: Or at least, it is a valid function call.

No it isn't. Show me where strlen is defined as taking a char array with an optional null terminator.

anilkunchalaece:
the problem is

for (int i = 0, j = 0 ; i < strlen(a); i+2, j++ ){

should be

for (int i = 0, j = 0 ; i < strlen(a);i= i+2, j++ ){

and it took me while to notice it.

I think I need to travel through all those thing before learning anything.

i am trying to get the value for LRC for MODBUS ASCII protocolThankyou @evil-noxx

Your welcome

Is this …

for (int i = 0, j = 0 ; i < strlen(a);i= i+2, j++ ){

intended to do the same as this …

numRepeats = strlen(a);
int j = 0;
for (int i = 0; i < numRepeats; i= i+2 ){
   j++;
   // other stuff
}

which seems to me to be much easier to understand and debug.

…R

Completing the sketch for LRC

so for the sketch is.

/*
char a[] = ":0106200103e8"; // ASCII string lrc being added

void setup(){
  Serial.begin(9600); // start the serial communication
  Serial.print("The string to which LRC is calculated is : ");
  Serial.println(a);
  int val = calculate(); //function to calculate the decimal value
  convert_to_hex(val);// function to convert the decimal value to HEX
}//end of setup()

void loop(){
  //do nothing here
}//end of loop()

//CALCULATE() FUNCTION
int calculate(){
  int val[strlen(a)/2]; // val[] array is used to store the decimal value for two HEX digits
  for (int i = 1, j = 0 ; i < strlen(a); i = i+2, j++ ) //i = i+2 because we are finding the decimal equalent for two HEX values
  {
    val[j] = conv(a[i],a[i+1]); // function to convert Two Hex numbers into Decimal Value
    
  }//end of for loop

  int sum = find_sum(val); // function to calculate total sum of decimal equalents
  return sum;
}//end of calculate() function

//FUNCTION_SUM() FUNCTION
int find_sum(const int * val) // we are passing an array to the function
{
  int sum = 0;
  for(int i=0;i<= (strlen(a)/2) - 1;i++)
  {
    sum = sum + val[i];
  }//end of for loop
  return sum; 
}//end of find_sum() function

//FUNCTION TO CONVERT HEX TO THE DECIMAL VALUE
int conv(char val1,char val2){
  int val_a = toDec(val1); // convert to the decimal value
  int val_b = toDec(val2);
  return (val_a*16) + val_b; // converting decimal value into HEX decimal Equalent  B1*16 + B0
}//end of conv() function

int toDec(char val){
  if(val<='9')
  {
    return val - '0'; // please see the ASCII table for clarification
  }
  else 
  {
    return val - '0'-7; // 7 is offset for capital letters please see the ASCII table 
  }

}//end of toDec() function

void convert_to_hex(int val){
  char hex[5];
  utoa((unsigned)val,hex,16); // utoa() function is used to convert unsigned int to HEX array (whatever the base we specified as third argument)
  Serial.print("the LRC value is : ");
  Serial.println(hex);
}//end of convert_to_hex() fucntion

where I am getting the Hex value in String which is good thing if i wanted to use strcat().
But before that i needed to perform 2’s complement and take only 8 bits of 2’s complement result.

problem is i have a string…
So How can I perform those two on the string?

I Think this will do that work for me…

char str[] = "313";
void setup(){
Serial.begin(9600);
int num = (int)strtol(str,NULL,16);
Serial.println(((~num)+1)&0xff,HEX);


}

void loop(){

}

magic done by strtol(string to long integer)

No it isn't. Show me where strlen is defined as taking a char array with an optional null terminator.

Show me where in

int strlen( char * );

it says anything about any "terminators".

If you want to stick the hex characters you need into some other string, there is nothing stopping you from writing something like this:

char sentence[] = { "This is a long string with 2 hex values here xx" } ;

unsigned int n = 73 ;  // should be less than 255
char hex[5] ;
utoa((unsigned)n,hex,16);

sentence[45]=hex[0] ;    // make sure you check this number 45
sentence[46]=hex[1] ;

Serial.println( sentence );

michinyon: Show me where in

int strlen( char * );

it says anything about any "terminators".

Please tell me you're not serious.

I'm quite serious. You're wrong. strlen(a) in the OP's example is a valid function call. The argument is a pointer to char. As I was the first to point out, it is not a very sensible function call.

The same as the argument to a square root function is a float. The declaration of the function has no way to enforce you calling it with a positive float.

In C/C++, it is the programmer's job, not the language or the compiler, to make sure that the function is going to be called with a "sensible" argument, either by the design of the logic of the program or by checking the parameter before you call the function.

If you want to imagine you can be more prescriptive in the implementation of function declarations, you can always define your own special types, or go and learn ADA.

Well I'll just wish you luck should you ever decide to go for interview as a pro C programmer with any of the companies I've worked for.