0
Offline
Full Member
Karma: 0
Posts: 122
Arduino rocks
|
 |
« on: January 07, 2011, 02:22:42 pm » |
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
Brattain Member
Karma: 316
Posts: 35593
Seattle, WA USA
|
 |
« Reply #1 on: January 07, 2011, 02:33:53 pm » |
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. 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
Full Member
Karma: 0
Posts: 122
Arduino rocks
|
 |
« Reply #2 on: January 07, 2011, 04:22:05 pm » |
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: 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
Brattain Member
Karma: 316
Posts: 35593
Seattle, WA USA
|
 |
« Reply #3 on: January 07, 2011, 04:57:00 pm » |
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
Full Member
Karma: 0
Posts: 122
Arduino rocks
|
 |
« Reply #4 on: January 07, 2011, 07:13:13 pm » |
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
Brattain Member
Karma: 316
Posts: 35593
Seattle, WA USA
|
 |
« Reply #5 on: January 07, 2011, 07:54:05 pm » |
// Create an array to hold the new IP string byte textCopy[16]; // Copy the array for(int i=10, j=0; i<26; i++, j++) { textCopy[j] = textBuff[i]; } // Replace the dots with NULLs textCopy[3] = '\0'; textCopy[7] = '\0'; textCopy[11] = '\0'; textCopy[15] = '\0'; // Pointer to each field byte *ptr = textCopy[0]; // Point to first octet int octets[4]; // Array of octets // 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
Full Member
Karma: 0
Posts: 122
Arduino rocks
|
 |
« Reply #6 on: January 08, 2011, 02:29:47 am » |
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
Brattain Member
Karma: 316
Posts: 35593
Seattle, WA USA
|
 |
« Reply #7 on: January 08, 2011, 08:26:28 am » |
Try this: 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
Full Member
Karma: 0
Posts: 122
Arduino rocks
|
 |
« Reply #8 on: January 08, 2011, 01:28:48 pm » |
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
Brattain Member
Karma: 316
Posts: 35593
Seattle, WA USA
|
 |
« Reply #9 on: January 08, 2011, 02:04:12 pm » |
char *ptr = textCopy[0]; Remove the 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
Full Member
Karma: 0
Posts: 122
Arduino rocks
|
 |
« Reply #10 on: January 08, 2011, 03:01:21 pm » |
it all works wonderfully now...thank you so much for all of your help. these forums are so great!!!!
|
|
|
|
|
Logged
|
|
|
|
|
|