Go Down

Topic: Copy byte array to another array. (Read 2139 times) previous topic - next topic

nathanas

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: [Select]

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!

drab

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

Arrch

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

nathanas

#3
May 27, 2012, 06:25 am Last Edit: May 27, 2012, 06:29 am by nathanas Reason: 1

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


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


Code: [Select]
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?

Arrch


&dht11_dat
  • [/b]


The bolded is redundant.

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

nathanas

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

Arrch


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


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: [Select]
&dht11_dat[0] you remove the redundancy to make it
Code: [Select]
dht11_dat and then type cast it into a char pointer instead of byte pointer:
Code: [Select]
(char*) dht11_dat

GoForSmoke

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.
Nick Gammon on multitasking Arduinos:
1) http://gammon.com.au/blink
2) http://gammon.com.au/serial
3) http://gammon.com.au/interrupts

drab

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

nathanas

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: [Select]
byte temp1[5];
int temp2=0;

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

PaulS

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.

majenko

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

Just use

Code: [Select]

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.

nathanas

#12
May 27, 2012, 01:52 pm Last Edit: May 27, 2012, 02:05 pm by nathanas Reason: 1
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:

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy