caplcanoecanalyzer

How to convert a byte array into a string in CAPL?


I have a byte array and I need to print the elements in a single line.

I tried using 'snprintf()' but it won't take a byte array as its input parameter.

I tried copying the byte array into an integer array and then used the snprintf(), but instead of printing the HEX values, corresponding ASCII values are printed.


Solution

  • You can try this code :

    variables
    { 
      int ar[100];
    }
    
    on diagResponse TCCM.*
    {
      char tmp[8];    // Temporary buffer containing single HEX value
      char out[301];  // Bigger output string and "local" to function
      // Better to place them there (i and response) if they are not global
      int i;
      byte response[100];
      
      out[0] = 0;     // Clear output string
      
      s1 = DiagGetPrimitiveData(this, response, elcount(response));
    
      for (i = 0; i < s1; i++)
      { 
        ar[i] = response[i];
        snprintf(tmp, elcount(tmp), "%.2X ", response[i]);  // byte to HEX convert
        strncat(out, tmp, elcount(out));  // Concatenate HEX value to output string
      }
    
      write("HEX Response : %s", out);
    }
    

    Olivier