Pages: [1]   Go Down
Author Topic: Copy byte array to another array.  (Read 1391 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Full Member
***
Karma: 1
Posts: 179
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi there...
 I am looking for the easiest way to copy an existing byte array value, that I successfully print on the serial monitor, to another array for later use.
I am printing the right values using:

Code:
byte dht11_dat[5];
Serial.print(dht11_dat[2], DEC);
Serial.print(".");
Serial.print(dht11_dat[3], DEC);

I want to capture (save) in another array the results.I cannot use atoi or strncpy commands.
Please help...
   Thanks in advance!
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 96
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The best way i have found to compare is to set up an array with the values u want to match with then when u bring ur serial data in set it up in a if statement and compare it there look at my last post with code on fingerprint scanner with uart communication post the way i set it up should help you out
Logged

California
Offline Offline
Faraday Member
**
Karma: 92
Posts: 3442
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Use a for loop or strcpy if it's null terminated.
Logged

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

Use a for loop or strcpy if it's null terminated.

I am having errors with the strncpy command....


Code:
char temp1[5];
byte dht11_dat[5];
Serial.print(dht11_dat[2], DEC); // I am getting this correctly
Serial.print(".");
Serial.print(dht11_dat[3], DEC); // I am getting this correctly

strncpy (temp1,&dht11_dat[0],1)  // cannot compile error in converting byte to char
Serial.print(temp1);

Please let me know how to copy as an integer or string the values... I am using the DHT11 sensor and I am trying to build a sketch that works without delay.
I need the variables so that I can use them every time I want.


Can someone also tell me how can I declare an array that is used in a function for public use?
« Last Edit: May 26, 2012, 11:29:23 pm by nathanas » Logged

California
Offline Offline
Faraday Member
**
Karma: 92
Posts: 3442
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

&dht11_dat
  • [/b]
The bolded is redundant.

To get it to compile, you just need to typecast it to a char pointer
Logged

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

Can you please give me the code line that makes that happen? I don't know any of what you say... smiley-sad
Logged

California
Offline Offline
Faraday Member
**
Karma: 92
Posts: 3442
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Can you please give me the code line that makes that happen? I don't know any of what you say... smiley-sad

strncpy expects a pointer to a character that is assumed to be in an array. Since you have dh11_dat as a pointer to a byte (that is part of an array), you need to type cast it into a char pointer when you passing it into strncpy.
So from
Code:
&dht11_dat[0]
you remove the redundancy to make it
Code:
dht11_dat
and then type cast it into a char pointer instead of byte pointer:
Code:
(char*) dht11_dat
Logged

Pittsburgh, PA, USA
Offline Offline
Faraday Member
**
Karma: 98
Posts: 4816
I learn a bit every time I visit the forum.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

strcpy() copies a C string (byte or char array) and puts a terminating zero at the end.
strncpy() copies without putting a terminating zero at the end.

strncpy() lets you copy into an existing C string, to replace some characters.
strcpy() would mess that up.

You would do fine to use a while-loop to make your array copy. Then you would know how it works.
Logged

I find it harder to express logic in English than in Code.
Sometimes an example says more than many times as many words.

Offline Offline
Jr. Member
**
Karma: 0
Posts: 96
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

true that i just learned all that this week the really hard way only to find out it was my preset array that was the issue lol
Logged

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

I made it by saaving to another array...

Now I need a byte stored in a specific place in an array to be converted to integer....   But atoi doesn't work.

How can I convert to integer the below?

Code:
byte temp1[5];
int temp2=0;

temp2=atoi(temp1[0],1);
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 644
Posts: 50457
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Now I need a byte stored in a specific place in an array to be converted to integer.
What, exactly is in this array? Are there characters stored in the array? If so, and the character represents a numeric digit, simply subtract '0' from the value to get the digit ('8' - '0' = 8).

If not, then the value is already a (less than) int-sized value, so just store it in an int.
Logged

UK
Offline Offline
Faraday Member
**
Karma: 100
Posts: 4153
Where is your SSCCE?!?!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

A byte is a number.  An integer is a number.  atoi() converts a string representation of a number into a number.

Just use

Code:
temp2 = temp1[0];

and temp2 will then contain the value that is stored in slice 1 of temp1.  If temp1[0] contains 53, then temp2 will contain 53.
Logged

Get 10% off all 4D Systems TFT screens this month: use discount code MAJENKO10

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

Thanks!

I figured that out at the time you posted...
It was pretty simple, but had beeen programming for 8 hours last night.
As soon as I woke up it took me about 20 minutes to find out my problem. And last night I'd been struggling for over 2 hours!
Programming can mess your brain up!  smiley-eek-blue
« Last Edit: May 27, 2012, 07:05:26 am by nathanas » Logged

Pages: [1]   Go Up
Jump to: