Populating an array of bytes.....

Hi, I expect this is one of those trivial questions that are blindingly obvious to most, but I just can't find a simple way of doing this.

If I have a character array I can populate it very simply with something like this.....

strcpy(myCharArray,"fred");

However, the code I'm playing with uses a byte array so the above won't work. Instead I have to do something a lot more laborious to set up test data...

myByteArray[0]=102; // f
myByteArray[1]=114; // r
myByteArray[2]=101; // e
myByteArray[3]=100; // d
myByteArray[4]=0; // null

This works, but is a bit tedious, is there a way of either converting/copying from the char array to the byte array? Or maybe a way of directly populating the byte array with the ascii equivalent of each character?

Thanks

byte x [] = "fred";


Rob

However, the code I'm playing with uses a byte array so the above won't work.

 strcpy((char*)myCharArray,"fred");

Thanks Rob,

that works when I create x[], but what about when I want to overwrite or change the values?

Cheers

See reply #2

BigusDickus:
I have to do something a lot more laborious to set up test data...

myByteArray[0]=102; // f
myByteArray[1]=114; // r
myByteArray[2]=101; // e
myByteArray[3]=100; // d
myByteArray[4]=0; // null

Well, Bigus, slightly less laborious would be:

        myByteArray[0]= 'f';
        myByteArray[1]= 'r';
        myByteArray[2]= 'e';
        myByteArray[3]= 'd';
        myByteArray[4]= 0;    // null

However see the other replies.

strcpy((char*)myCharArray,"fred");

That works a treat.

Annoyingly, I thought I'd tried every possible combination of (byte*) and (char*) whilst working on this. Obviously I tried every possible combination except the right one!

Thanks guys!

but what about when I want to overwrite or change the values?

Sorry, wasn't paying attention. See above posts :slight_smile:


Rob