Putting binary data into a variable

Go To Last Post
10 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I have an AVRravenLcd board and I want to send machine code for a Zolertia Z1 board over radio using it(the Z1 uses a MSP430X microcontroller).

I cannot declare a string variable to hold the binary data, as avrgcc will complain of stray "\xxx" characters.

In your opinion, how can I do this easily?

The workaround I imagine is to take the binary, link it and store it to the raven's flash, then read it using pgm_read_... and send it. It seems like a big detour :(

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

How about a byte array?

It's like a string, except for the way you treat the data. Something like this:

uint8_t binary_data[4] =
{
   0x02, 0x6d, 0xa8, 0xbc
};

Note that you cannot count on a NUL character to denote the end of the array, so you'll have to keep track of the array's length separately.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

*EDIT*

actually, the problem lies within these NUL characters.
I used an online char-to-hex converter, but it stops whenever it finds a NUL :(

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

then use another converter .. what is the source of the data you need to include in the code ? a binary file ?

"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it"

Last Edited: Tue. Mar 1, 2011 - 02:13 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

thygate wrote:
then use another converter .. what is the source ? a binary file ?

the source is a standard ELF-format *.o file.
What other converter do you propose?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

No, I meant, what is the source of the data that you need to include in code (as an array) ?

Quote:
I used an online char-to-hex converter, but it stops whenever it finds a NUL

What stops ? The online converter (what are you feeding it ?) Or your code when parsing the array ?

EDIT:

Quote:
I want to send machine code

Do you need to convert the *.o file to a byte array ?

"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it"

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

thygate wrote:
No, I meant, what is the source of the data that you need to include in code (as an array) ?

The source of the data is the object file itself.

thygate wrote:
What stops ? The online converter (what are you feeding it ?) Or your code when parsing the array ?

The copy-paste operation into the browser. It copy-pastes everything until the first NUL.

thygate wrote:
Do you need to convert the *.o file to a byte array ?

If this would solve my question, then why not?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I use "xxd -i" for this. xxd comes as part of a VIM installation so is already on most Linux systems and download packages are available for Win32 (you don't have to install the entire vim - just get the .zip and take xxd.exe from it).

Here's an example of using it with a small binary file:

D:\test>dir *.o
 Volume in drive D is DATA
 Volume Serial Number is 3636-53FD

 Directory of D:\test

23/09/2010  20:23               876 asm.o
17/10/2010  13:56             6,928 beacon.o
26/10/2010  18:48             2,612 foo.o
26/10/2010  18:48             2,408 foo2.o
25/01/2011  14:03            10,872 lcd.o
22/11/2010  09:25             2,220 main.o
10/11/2010  15:33             2,328 mainplus.o
10/11/2010  15:42             2,544 plus.o
25/02/2011  16:13             2,552 test.o
31/01/2011  11:49             2,600 test2.o
20/10/2010  14:56             3,916 uart_test.o
              12 File(s)         46,656 bytes
               0 Dir(s)   7,702,372,352 bytes free

D:\test>xxd -i asm.o asm.c

D:\test>type asm.c
unsigned char asm_o[] = {
  0x7f, 0x45, 0x4c, 0x46, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x53, 0x00, 0x01, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x28, 0x01, 0x00, 0x00,
  0x85, 0x00, 0x00, 0x00, 0x34, 0x00, 0x00, 0x00, 0x00, 0x00, 0x28, 0x00,
  0x0a, 0x00, 0x07, 0x00, 0x85, 0xe5, 0x88, 0xbb, 0x08, 0x95, 0x00, 0x00,
  0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x05, 0x00, 0x61, 0x00, 0x00, 0x00,
  0x2e, 0x00, 0x00, 0x00, 0x64, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x5b, 0x00, 0x00, 0x00, 0x84, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x44, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x44, 0x00, 0x09, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x44, 0x00, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x43, 0x3a, 0x5c, 0x55, 0x73, 0x65, 0x72, 0x73, 0x5c, 0x43, 0x6c,
  0x69, 0x66, 0x66, 0x5c, 0x41, 0x70, 0x70, 0x44, 0x61, 0x74, 0x61, 0x5c,
  0x4c, 0x6f, 0x63, 0x61, 0x6c, 0x5c, 0x54, 0x65, 0x6d, 0x70, 0x2f, 0x63,
  0x63, 0x32, 0x35, 0x4c, 0x76, 0x6d, 0x6f, 0x2e, 0x73, 0x00, 0x43, 0x3a,
  0x5c, 0x55, 0x73, 0x65, 0x72, 0x73, 0x5c, 0x43, 0x6c, 0x69, 0x66, 0x66,
  0x5c, 0x41, 0x70, 0x70, 0x44, 0x61, 0x74, 0x61, 0x5c, 0x4c, 0x6f, 0x63,
  0x61, 0x6c, 0x5c, 0x54, 0x65, 0x6d, 0x70, 0x2f, 0x63, 0x63, 0x32, 0x35,
  0x4c, 0x76, 0x6d, 0x6f, 0x2e, 0x73, 0x00, 0x61, 0x73, 0x6d, 0x2e, 0x53,
  0x00, 0x00, 0x2e, 0x73, 0x79, 0x6d, 0x74, 0x61, 0x62, 0x00, 0x2e, 0x73,
  0x74, 0x72, 0x74, 0x61, 0x62, 0x00, 0x2e, 0x73, 0x68, 0x73, 0x74, 0x72,
  0x74, 0x61, 0x62, 0x00, 0x2e, 0x74, 0x65, 0x78, 0x74, 0x00, 0x2e, 0x64,
  0x61, 0x74, 0x61, 0x00, 0x2e, 0x62, 0x73, 0x73, 0x00, 0x2e, 0x72, 0x65,
  0x6c, 0x61, 0x2e, 0x73, 0x74, 0x61, 0x62, 0x00, 0x2e, 0x73, 0x74, 0x61,
  0x62, 0x73, 0x74, 0x72, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x1b, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x34, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x21, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3a, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x27, 0x00, 0x00, 0x00,
  0x08, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x3a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x31, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x00, 0x48, 0x00, 0x00, 0x00,
  0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00,
  0x0c, 0x00, 0x00, 0x00, 0x2c, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 0x03, 0x00, 0x00,
  0x3c, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00,
  0x04, 0x00, 0x00, 0x00, 0x0c, 0x00, 0x00, 0x00, 0x37, 0x00, 0x00, 0x00,
  0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x84, 0x00, 0x00, 0x00, 0x61, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x11, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0xe5, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xb8, 0x02, 0x00, 0x00,
  0x70, 0x00, 0x00, 0x00, 0x09, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00,
  0x04, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x09, 0x00, 0x00, 0x00,
  0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x28, 0x03, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x02, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x03, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x06, 0x00,
  0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x10, 0x00, 0x01, 0x00, 0x00, 0x66, 0x6f, 0x6f, 0x00, 0x00, 0x00, 0x00,
  0x14, 0x00, 0x00, 0x00, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x20, 0x00, 0x00, 0x00, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x2c, 0x00, 0x00, 0x00, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x38, 0x00, 0x00, 0x00, 0x01, 0x01, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
  0x44, 0x00, 0x00, 0x00, 0x01, 0x01, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00
};
unsigned int asm_o_len = 876;

D:\test>
================================================================================

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Here's some C# code to convert any file to an ASCII representation of a byte array.

untested!

public string DoMagic(string fn)
{
    string dout = "uint8_t binary_data[] = {";

    byte[] data = FileToByteArray(fn);
    for (int i=0; i

EDIT:
Good tip, clawson. I usually end up coding something to do the job. I will be using xxd as well in the future.

"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it"

Last Edited: Tue. Mar 1, 2011 - 02:37 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

awesome, clawson.
I owe you a beer bigtime.

Thank you all for your help!