Pages: [1]   Go Down
Author Topic: Concatenate byte array of values into a char variable..  (Read 1010 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Full Member
***
Karma: 0
Posts: 141
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Could someone en lighting me in a problem...

i  have function that returns a char of 40 characters , this function gets a byte array of 8 values ,
I want to take this 8 values and comma separate them and in the end put them into a variable that my function will return it..

My problem is that i get random character values instead of numbers that i expect.

Code:
char* returnTableRowLedValues(byte rownumber)
{
    char* exports ;
    byte* s;
    s=returnTableRowLed(rownumber);       // THIS WILL RETURN SOMETHING LIKE THIS (99,22,33,44,55,66,77,88)
    char* ledreturn;
    
    for(unsigned int i = 0; i < 8; i++){
      ledreturn[i] = (char)s[i];
          strcpy(exports, (char*)s[i]);
          //exports=exports + (char)s[i];
          
          if (i<8){
            strcpy(exports, (char*) ',');
           //  exports= exports +  (char) ',' ;
          }
          
    }
  
    return exports;
}
« Last Edit: March 24, 2013, 03:43:39 pm by gc9n » Logged

Sweden
Offline Offline
Sr. Member
****
Karma: 11
Posts: 434
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Avoid: strcpy(exports, (char*) ',');

Instead write: strcat(exports, ",");

You cannot case (char*) a character and get a string. The generated code is actually the address that corresponds to the value ',' is used.

Check the difference between strcpy() and strcat().

To fix more of your code you will have to post more ;-)

Cheers!

Logged

Offline Offline
Edison Member
*
Karma: 58
Posts: 2078
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The characters must be somewhere, for example in a buffer.
In your function, you have pointer, but you have no location where the characters can be stored.

Are you sure the function calls itself ?
I doubt if that is good programming. A function can call itself, but I would never do it like that.

I personally like to solve it like this:

declare the buffer and call the function:
Code:
// declare the variables, on stack or in ram.
char buffer[40];
int error;

// calling the function, the returned error is optional
error = TableRows( buffer, 10);

// The function itself uses a pointer to the buffer.
// The function can read and write buffer.
int TableRows( char *buf, byte row)
{
  ...
  strcpy( buf, "Hello"); // Write something in the buffer.
  return (0);
}
Logged

Offline Offline
Full Member
***
Karma: 0
Posts: 141
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Erdin, its not calling it self   smiley

char* returnTableRowLedValues(byte rownumber)
{
    char* exports ;
    byte* s;
    s=returnTableRowLed(rownumber);  


OK..i will show yo my entire problem! smiley-kiss
i have this table of 96 values , its value is 15minutes of one day.
Code:
byte Mainleds[96] = {
   0, 0, 0, 0, 0, 0, 0, 0,  //0 - 1
  0, 0, 0, 0, 0, 0, 0, 0,  //2 - 3
  0, 0, 0, 0, 0, 0, 0, 0,  //4 - 5
  0, 0, 0, 0, 0, 0, 0, 0,  //6 - 7
  0, 0, 0, 0, 0, 0, 0, 0, //8 - 9
  0, 0, 0, 5, 16, 32, 55, 80,  //10 - 11
   110, 140, 190, 200, 200, 200, 200, 200,  //12 - 13
   220, 255, 255, 255, 255, 255, 255, 255,  //14 - 15
  255, 255, 255, 255, 255, 255, 255, 255,  //16 - 17
  230, 220, 200, 200, 200, 200, 200,200,  //18 - 19
  200, 200, 90, 60, 30, 0, 0, 0,  //20 - 22
  0 ,0, 0, 0, 0, 0, 0, 0     //22 - 23   12 rows
};  //White LED10000K array in RAM
 

i am writing a code for a light controller that the UI will be from ethernet shield  , so in order to save some eeprom etc. i decide to make
12 text boxes , and get 8 values for every text box.. and not a huge text box because user will be confused.
(i hear any other ways!)

so i call this function to get me the rownumber of for example 10am - 11am

returnTableRowLed(2);
this will return me (0, 0, 0, 5, 16, 32, 55, 80)  byte array of 8 values

Code:
byte*  returnTableRowLed(byte rownumber)
{
    byte* chars;
   byte row=0;
   byte y=0;
   for(int i = 0; i<96; i++){
     if (i % 8 == 0)
     {
       row=row+1;        
     }
     if (row==rownumber)
           {
            chars[y] =Mainleds[i];
            // Serial.println(Mainleds[i]);
            y=y+1;
           }
   }
 
  
   return chars;
}

now i want those values  in a variable in order to show it to user like this

Code:
                               client.println(F("<input name='PWM_1_1' size='64'type='text' maxlength='3' value='"));  client.print(returnTableRowLedValues(1)); client.println(F("'/>"));   client.println(F("</td> </tr> <td style='background-color: #C0C0C0' class='style1'>2 - 3</td><td > "));
                                client.println(F("<input name='PWM_1_2' size='64'type='text' maxlength='3' value='"));  client.print(returnTableRowLedValues(2)); client.println(F("'/>")); client.println(F("</td></tr><tr><td style='background-color: #C0C0C0' class='style1'> "));client.println(F("4 - 5</td> "));client.println(F("<td >"));
                                client.println(F("<input name='PWM_1_3' size='64'type='text' maxlength='3' value='"));  client.print(returnTableRowLedValues(3)); client.println(F("'/>"));  client.println(F("</td></tr><tr> ")); client.println(F("<td style='background-color: #C0C0C0' class='style1'>"));  client.println(F("6 - 7</td> <td >"));
                                client.println(F("<input name='PWM_1_4' size='64'type='text' maxlength='3' value='"));  client.print(returnTableRowLedValues(4)); client.println(F("'/>"));  client.println(F("</td></tr><tr> ")); client.println(F("<td style='background-color: #C0C0C0' class='style1'>"));    client.println(F("8 - 9</td> "));     client.println(F("<td >"));
                                client.println(F("<input name='PWM_1_5' size='64'type='text' maxlength='3' value='"));  client.print(returnTableRowLedValues(5)); client.println(F("'/>"));    client.println(F("</td></tr><tr> ")); client.println(F("<td style='background-color: #C0C0C0' class='style1'>"));  client.println(F("10 - 11</td> "));   client.println(F("<td >"));
                                client.println(F("<input name='PWM_1_6' size='64'type='text' maxlength='3' value='"));  client.print(returnTableRowLedValues(6)); client.println(F("'/>"));   client.println(F("</td></tr><tr> "));   client.println(F("<td style='background-color: #C0C0C0' class='style1'>"));                                                      client.println(F("12 - 13</td> ")); client.println(F("<td >"));
                                client.println(F("<input name='PWM_1_7' size='64'type='text' maxlength='3' value='"));  client.print(returnTableRowLedValues(7)); client.println(F("'/>"));  client.println(F("</td></tr><tr> "));client.println(F("<td style='background-color: #C0C0C0' class='style1'>"));                                                      client.println(F("14 - 15</td> "));                                                      client.println(F("<td >"));
                                client.println(F("<input name='PWM_1_8' size='64'type='text' maxlength='3' value='"));  client.print(returnTableRowLedValues(8)); client.println(F("'/>"));     client.println(F("</td></tr><tr> "));    client.println(F("<td style='background-color: #C0C0C0' class='style1'>"));      client.println(F("16 - 17</td> "));   client.println(F("<td >"));
                                client.println(F("<input name='PWM_1_9' size='64'type='text' maxlength='3' value='"));  client.print(returnTableRowLedValues(9)); client.println(F("'/>"));   client.println(F("</td></tr><tr> "));  client.println(F("<td style='background-color: #C0C0C0' class='style1'>"));   client.println(F("18 - 19</td> "));                                                     client.println(F("<td >"));
                                client.println(F("<input name='PWM_1_10' size='64'type='text' maxlength='3' value='")); client.print(returnTableRowLedValues(10)); client.println(F("'/>"));   client.println(F("</td></tr><tr> "));   client.println(F("<td style='background-color: #C0C0C0' class='style1'>"));   client.println(F("20 - 21</td> "));      client.println(F("<td >"));  
                                client.println(F("<input name='PWM_1_11' size='64'type='text' maxlength='3' value='")); client.print(returnTableRowLedValues(11)); client.println(F("'/>"));   client.println(F("</td></tr><tr> "));  client.println(F("<td style='background-color: #C0C0C0' class='style1'>"));    client.println(F("22 - 23</td> "));                                             client.println(F("<td >"));
                                client.println(F("<input name='PWM_1_12' size='64'type='text' maxlength='3' value='")); client.print(returnTableRowLedValues(12)); client.println(F("'/>"));  client.println(F("</td></tr>"));  client.println(F(" </table>"));        client.println(F("</table> "));  client.println(F("<br />")); client.println(F("</td></tr><tr>"));  client.println(F("<td ><br></td></tr><tr>")); client.println(F("<td >"));
                                

if there is any other way  user can change the byte array from web interface i will be glad to hear.
« Last Edit: March 24, 2013, 04:29:01 pm by gc9n » Logged

Offline Offline
Edison Member
*
Karma: 58
Posts: 2078
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Sorry, I didn't read the code well, the function is not calling itself.

Did you understand that the characters must be at some location ?
And you can fill a string via the parameters ?

This is not the String class, you are using good old 'C' programming.
You declare a pointer on the stack and return that pointer.
But you still don't have a location where the characters are. So if you write something, you are writing in thin air.
Logged

Offline Offline
Full Member
***
Karma: 0
Posts: 141
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

i try this also but returns me nothing
char* addString(const char* addThis, const char* toThis)
{
    char* destination = (char*)malloc( strlen( addThis ) + strlen( toThis ) + 1 );
    strcpy( destination, toThis );
    strcat( destination, addThis );
    return destination;
}

Logged

Offline Offline
Faraday Member
**
Karma: 57
Posts: 2767
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
i  have function that returns a char of 40 characters , this function gets a byte array of 8 values ,

A char is a single byte location  and  holds one character.  Not 40.

Your function does not return a char.

It returns a value which is a pointer (  the memory location address ) of a char  (  which may or may not be the
address of the 0th element of an array of char variables ).
Logged

Offline Offline
Full Member
***
Karma: 0
Posts: 141
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Ok.
what is the easiest way

for a char[8]   with values (255,255,255,255,255,255,255,255)

to represent it into a string  like this '255,255,255,255,255,255,255,255'
Logged

California
Offline Offline
Faraday Member
**
Karma: 88
Posts: 3352
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Ok.
what is the easiest way

for a char[8]   with values (255,255,255,255,255,255,255,255)

to represent it into a string  like this '255,255,255,255,255,255,255,255'

sprintf() if the number of tokens
Logged

Offline Offline
Faraday Member
**
Karma: 57
Posts: 2767
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Ok.
what is the easiest way

for a char[8]   with values (255,255,255,255,255,255,255,255)

to represent it into a string  like this '255,255,255,255,255,255,255,255'

Presumably,   those values represent small unsigned integer values   and not actual characters in the textual sense.
And by unsigned, I mean they are values from the range 0 to 255  rather than -128 to 127  which a signed single-byte integer
numerical value would be.

I'd write something like this

Code:
char result[40] ;
int temp[8] ;

for ( int j=0 ; j<7 ; j++ )    temp[j] = (int) yourCharArray[j] ;

sprintf( buf,'%03d,%03d,%03d,%03d,%03d,%03d,%03d,%03d',  temp[0], temp[1], temp[2], temp[3], temp[4], temp[5], temp[6], temp[7] );


There are other ways you could tackle the problem.  The potential catch is to ensure that the bytes with all 1's in them
get interpreted as 255 and not as -1.
Logged

Pages: [1]   Go Up
Jump to: