Inserting values in BYTE array
Hi everyone,
I have a byte array of 1000 bytes and it is supposed to have a static content. What I want to do is put a whole string of hex values into it all at once rather than do something like:
byte[0] = 0x95;
byte[1] = 0x78... and so on..
What I want to do is byte = ox95 0x78...etc
Is it possible to initialize it at once with all the content?
Thanks,
Pankaj
[416 byte] By [
xargon] at [2007-11-18 22:12:38]

# 1 Re: Inserting values in BYTE array
You could combine declaration and initialisation in the following manner:
unsigned char bytes[] = {0x95, 0x78, 0x00, ...}
# 2 Re: Inserting values in BYTE array
If your string of hex data is already in a 'string' form in memory, then simply use memcpy function. What is the problem? What is the format of the hex data that you have?
# 3 Re: Inserting values in BYTE array
As it is an array of characters you can use quotes.
\x is an escape sequence that allows ascii characters and you can embed 0s but then you must use memcpy rather then strcpy.
You can also use std::copy which works with pointers even if you have an array and not a vector. Thus
const unsigned char byteSrc[] = "\x95\x78\0"; // etc
std::copy( byteSrc, byteSrc + sizeof(byteSrc)-1, bytes );
If you have vector<unsigned char> or even basic_string<unsigned char> you are better off because you can use back_inserter. Thus:
std::copy( byteSrc, byteSrc + sizeof(byteSrc)-1, back_inserter( bytes ) );
(Note I did sizeof(byteSrc)-1 to remove the nul terminator, which I presume you don't want). Using back_inserter means you do not need to pre-allocate, although it is more efficient if you do so using "reserve".