Pages: [1]   Go Down
Author Topic: combining bytes into one  (Read 1655 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Full Member
***
Karma: 0
Posts: 137
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi, for a program I'm writing, I am parsing one character at a time from an "    ".read buffer i've set up and storing those as bytes.  However where I'm getting stuck is that I need to combine say three or four bytes into one new byte.  i looked at some of the references on the arduino page and found a little promise in the byte() command but I'm not sure how to setup the syntax for that.  and i dont even know if its the right command.

so for example if i have:

byte data1[] = {1};
byte data2[] = {2};
byte data3[] = {3};

can i do something like:

byte newdata[] = {data1 & data2 & data3};

thanks
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 547
Posts: 45935
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
can i do something like:
No. You could use memcpy to copy an array, or a for loop to copy elements in the array. You need to be sure to allocate a large enough array first.

Quote
I need to combine say three or four bytes into one new byte.
This isn't going to happen. Perhaps you left the term array out of the sentence.

A little more detail (some code) on what you are doing would be helpful.

Logged

0
Offline Offline
Full Member
***
Karma: 0
Posts: 137
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

basically what i am doing is that i am trying to read in an ip address, parse it, and store it as 4 bytes into the eeprom so that it can be read upon reboot.  so what i have so far at accomplishing that is that i am reading a byte one at a time and storing it into 12 distinct bytes.  so for example:
Code:
byte newIP1 = textBuff[10];
  byte newIP2 = textBuff[11];
  byte newIP3 = textBuff[12];
  byte newIP4 = textBuff[14];
  byte newIP5 = textBuff[15];
  byte newIP6 = textBuff[16];
  byte newIP7 = textBuff[18];
  byte newIP8 = textBuff[19];
  byte newIP9 = textBuff[20];
  byte newIP10 = textBuff[22];
  byte newIP11 = textBuff[23];
  byte newIP12 = textBuff[24];

but then i want to take the first 4 bytes and create a new byte and so on and so forth until i get all four bytes.

such as:
byte1 is read as 1
byte2 is read as 2
byte3 is read as 3

and then the new byte would be:
byte1byte2byte3 otherwise known as 123
so the new byte would have a value of 123 which would then be written to eeprom

is that enough detail?  im tryin to be clear lol
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 547
Posts: 45935
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I notice that the indexes into the textBuff array are not contiguous. You could make a copy of the textBuff array from 10 through 25. Then change the value that was in 13, 17, 21, and 25 to NULL.

Then create a pointer to point to textCopy[0], and pass that pointer to atoi(). Save the returned integer. Increment the pointer by 4, and repeat, to get all 4 (byte sized) integers.
Logged

0
Offline Offline
Full Member
***
Karma: 0
Posts: 137
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

i really do appreciate your help on this but i read that last message over and over again trying to understand what you meant and i still dont fully understand.  would you mind showing me a little sample code? especially on the atoi() command, i couldnt find too much online bout that command.  what do you mean by pointer?  and by copy the buffer do you mean like a memcopy? do you have sample code for that or a place where i can read up on it?

thank you so much
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 547
Posts: 45935
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
// Create an array to hold the new IP string
byte textCopy[16];

Code:
// Copy the array
for(int i=10, j=0; i<26; i++, j++)
{
   textCopy[j] = textBuff[i];
}

Code:
// Replace the dots with NULLs
textCopy[3] = '\0';
textCopy[7] = '\0';
textCopy[11] = '\0';
textCopy[15] = '\0';

Code:
// Pointer to each field
byte *ptr = textCopy[0]; // Point to first octet

Code:
int octets[4]; // Array of octets

Code:
// Parse the data
for(int i=0; i<4; i++)
{
   octet[i] = atoi(ptr); // Convert octet to integer and store
   ptr += 4; // Advance pointer to next octet
}
Logged

0
Offline Offline
Full Member
***
Karma: 0
Posts: 137
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

  byte *ptr = textCopy[0];

I get an error for this code:  invalid conversion from byte to byte
should the asterisk be there?

int octet1[4];
  for(int i=0; i<4; i++)
  {
    octet1 = atoi(ptr);
    ptr +=4;
  }

so this above code is only for the first octet right?

thanks
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 547
Posts: 45935
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Try this:
Code:
char textCopy[] = { '1', '9', '2', '.',
                    '1', '6', '8', ',',
                    '0', '0', '0', '.',
                    '0', '0', '1', '.'};

int octet[4];

void setup()
{
  Serial.begin(9600);

  textCopy[3] = '\0';
  textCopy[7] = '\0';
  textCopy[11] = '\0';
  textCopy[15] = '\0';
  
  char *ptr = textCopy;
  for(int i=0; i<4; i++)
  {
    Serial.print("ptr points to: [");
    Serial.print(ptr);    Serial.println("]");
    
    octet[i] = atoi(ptr);
    ptr += 4;
  }
  
  for(int i=0; i<4; i++)
  {
    Serial.print("octet[");
    Serial.print(i);    Serial.print("] = ");
    Serial.println(octet[i]);
  }
}

void loop()
{
}
You may need to perform a cast when copying the byte array to the char array, in your code.
Logged

0
Offline Offline
Full Member
***
Karma: 0
Posts: 137
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

so i uploaded and tried your code and it worked flawlessly thank you.  so then using your code i put it into my main code but everytime i try and compile it i get a "invalid conversion from 'char to 'char*'" error.  heres the bit of code i entered to my main code:

char textCopy[16];
  for(int i=10, j=0; i<26; i++, j++)
  {
    textCopy[j] = textBuff;
  }
  textCopy[3] = '\0';
  textCopy[7] = '\0';
  textCopy[11] = '\0';
  textCopy[15] = '\0';
  char *ptr = textCopy[0];
  int octet[4];
  for(int i=0; i<4; i++)
  {
    ptr +=4;
  }

i woke up this morning and had an idea but im not sure it will work.  what if i did something like this:

char text1[3];
  for(int i=10, j=0; i<13; i++, j++)
  {
    textCopy[j] = textBuff;
  }

char text2[3];
  for(int i=14, j=0; i<17; i++, j++)
  {
    textCopy[j] = textBuff;
  }

char text3[3];
  for(int i=18, j=0; i<21; i++, j++)
  {
    textCopy[j] = textBuff;
  }

char text4[3];
  for(int i=22, j=0; i<25; i++, j++)
  {
    text4[j] = textBuff;
  }

would that copy the 3 values into one new value?  now giving me 4 (3 digit) values?

thanks
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 547
Posts: 45935
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
char *ptr = textCopy[0];
Remove the
  • from this line.
Code:
for(int i=0; i<4; i++)
 {
   ptr +=4;
 }
This block is missing the call to atoi() and the storage of the return value in the octet array.
Logged

0
Offline Offline
Full Member
***
Karma: 0
Posts: 137
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

it all works wonderfully now...thank you so much for all of your help.   these forums are so great!!!!
Logged

Pages: [1]   Go Up
Jump to: