title: How to create hex-dump .bin file from powershell or cmd?
body: <p>The main purpose is to write hex-coded string or just hex data into a main bin file, whether or not, hex-dumped.</p>
  
  <p>Using windows powershell or cmd, we're trying to create a bin file in order to concatenate it with the main one using opensource <a href="http://srecord.sourceforge.net/srecord-1.64.pdf" rel="nofollow noreferrer">srecord</a> execution scripts, like srec_cat.exe.</p>
  
  <p>We've managed to hex-dump some stuff like a string or so in powershell:</p>
  
  <p>"hello_world" would be:</p>
  
  <pre><code>PS C:\Users\user&gt; echo hello_world | format-hex &gt; output.bin
  </code></pre>
  
  <p>The concent of that "bin" file is:</p>
  
  <pre><code>           Ruta:
  
             00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F
  
  00000000   68 65 6C 6C 6F 5F 77 6F 72 6C 64                 hello_world
  </code></pre>
  
  <p>However, this output above, is string formatted, and what is needed is to dump that as a bin type file. This is because srecord's srec_cat tool needs it to be that way in order to write (concat) that data at some memory region of the main bin file that will be flashed into devices.</p>
  
  <p>The output needed would be something like:</p>
  
  <pre><code>00000000: 68 65 6C 6C 6F 5F 77 6F 72 6C 64 00 00 00 00 00  #hello_world
  </code></pre>
  
  <p>Again, as binary type file.</p>
  
  <p>Thanks a lot for your time and attention!</p>
  
  <hr>
  
  <h2>First edit:</h2>
  
  <p>So, we're using Atmel Studio to make a program that will work with SAMD21 devices. Along with SAM ICE via JTAG for flashing and debugging. The result of the compilation, and hence the final executable that is mounted for debugging is a *.bin file. The encoding maybe is UTF-8.</p>
  
  <p>What I'm trying to do, is to add data like IDs, keys, and so, into specific memory directions of the final firmware *.bin file in order to be flashed.</p>
  
  <p>Making use of srec tools, something like this is done:</p>
  
  <p>First conversion:</p>
  
  <pre><code>srec_cat data_input_file.bin -o data_input_file.srec
  </code></pre>
  
  <p>Second conversion:</p>
  
  <pre><code>srec_cat main_file.bin -binary -o main_file.srec
  </code></pre>
  
  <p>The concat at specific memory region, this command is generic (no address targeted):</p>
  
  <pre><code>srec_cat data_input_file.srec main_file.srec -o final_firmware.bin
  </code></pre>
  
  <p>I don't know how to create such binary file with hex-coded strings, starting at zero address, up to space needed to hold the data. </p>
  
  <p>With srec tools, we're able to put that data into the directions needed.</p>
  
  <hr>
  
  <h2>Second Edit</h2>
  
  <p>The sources of raw bytes that will be used are two.</p>
  
  <p>The first one is the *.bin result of the Atmel Studio's compilation that is intended to be flashed into the hardware devices we're using. </p>
  
  <p>The content of the file, for example would be something like, <code>Main_File_1.bin</code>:</p>
  
  <pre><code>00000000: 30 30 30 30 09 66 66 20 66 65 20 36 38 20 30 30  #0000.ff fe 68 00
  00000010: 20 36 35 20 30 30 20 36 63 20 30 30 20 20 36 63  # 65 00 6c 00  6c
  00000020: 20 30 30 20 36 66 20 30 30 20 35 66 20 30 30 20  # 00 6f 00 5f 00
  00000030: 37 37 20 30 30 20 20 20 2E 2E 68 2E 65 2E 6C 2E  #77 00   ..h.e.l.
  00000040: 6C 2E 6F 2E 5F 2E 77 2E 0D 0A 30 30 31 30 09 36  #l.o._.w...0010.6
  00000050: 66 20 30 30 20 37 32 20 30 30 20 36 63 20 30 30  #f 00 72 00 6c 00
  00000060: 20 36 34 20 30 30 20 20 30 64 20 30 30 20 30 61  # 64 00  0d 00 0a
  00000070: 20 30 30 20 20 20 20 20 20 20 20 20 20 20 20 20  # 00
  00000080: 20 20 6F 2E 72 2E 6C 2E 64 2E 2E 2E 2E 2E 0D 0A  #  o.r.l.d......
  </code></pre>
  
  <p>Again, is not mandatory to be in hex-dump fashion, the hex-dump is for visual reference, I know that it can be something like, <code>Main_File_1.bin</code>:</p>
  
  <pre><code>X"  ¡@  @  @                              @          @  @  @  @  @  @  e)  @  @  @  @  a/  q/  /  ‘/  ¡/  ±/  @  @  @  @  @  @  @  @  @  @  @  @  @  µL#x +ÑK +ÐH à ¿##p½Ø       ,h  µK +ÐHI à ¿Hh +ÐK + Ð˜G½ÀF    ,h  Ü   ,h      µ  K˜GLú!‰  GK`ú!‰  GK`"K`½ÀFU&gt;  E         à à0µKhJ %€!Ià ,ÐT`•`hBüÐ8öÒ0½   à àµKh + Ð˜G½ÀFô   pµ‚° #J`¬%ep£p%p&amp; !N°G% !°G' !°GK !Ya@"ZaYaZaKhBÐ
  KhšÔ    K˜G"Kp¿ó_b¶  °p½ô   å*  €D Aà à     8µ (ÐM ¨G&lt; ,úÑ8½ÀFM  µL #£a K˜G@#£a½€D A  µ„°K`¬ 
  K˜G$#“ #c`##r#ãr !K˜GH! "K˜G  !K˜G°½ÀFô   Y*  m*  y  õ(  %)  µ (Ð  !K˜Gà  !K˜G½ÀF%)  E)  0µ‘°.L##p #cp£p©"Jp‹p
  p )K˜G#xÙ    " )ÑZ  Ò&amp;H!@!˜@aªp #S`“`tSt‘tÑtu$!ST   “
  “€#[Ó`K“b#[BÓbKcKScK“aHIK˜G (ÑM,h K˜G#@#ƒ@K`*hÓi +üÑ"h#C#`K˜G K˜G   àú @²°0½¨  å*   D A      €–˜ ¬
  </code></pre>
  
  <p>The content descriptions above are just example information, not the actual bytes no be written, just theoretical context.</p>
  
  <p>Just as any device out there, the main target is to write the IDs, frequencies, data, etc, in each one of the hardware devices.</p>
  
  <p>I thought about a set of JSON files in local windows machine, that will read the content, in order to write it to the current device connected, and ready to be flashed (no fuse usage at this point).</p>
  
  <p>So, that would be, something like, <code>Data_File_1.json</code>:</p>
  
  <pre><code>{
      'device_1_data' : {
  
          'device_uuid' : {
              'low_uuid' : '0x00000001',
              'high_uuid' : '0xABCD000A'
          },
  
          'key_1' : ['0x2E', '0x34', '0x72', '0x0A', '0x45', '0x57', '0x41', '0x17', '0x07', '0x52', '0xCA', '0x4C', '0x0D', '0x7B', '0x41', '0x57'],
  
          'module_data' : "0x387A3830",
          'frequency_0' : '390200000',
          'frequency_1' : '392100000',
  
          'generator_syncword' : '0xCF'
  
      }
  }
  </code></pre>
  
  <p>The project reads specific memory addresses in order to get up and running, setting up the IDs and the information needed to work. So, making use of the information that the JSON files are holding, that data needs to be written as a binary file, in order to convert it to SREC, so it can be written into the main binary file at the memory address specified with srecord tools.</p>
  
  <p>So it's configured to read the information as it is. You can notice that the JSON holds hex data, but the JSON holds data as text, so the hex strings, need to be treated as hex, and the integers, need to be converted to hex in order to be appropiately written as expected by the application made.</p>
  
  <p>So, that would be something like, <code>Data_To_Be_Written_1.bin</code>:</p>
  
  <pre><code>00000000:   01 00 CD AB 0A 00 00 00 2E 34 72 0A 45 57 41 17  #..Í«.....4r.EWA.
  00000010:   07 52 CA 4C 0D 7B 41 57 30 38 7A 38 C0 FA 41 17  #.RÊL.{AW08z8ÀúA.
  00000020:   A0 F8 5E 17                                      #ø^.
  </code></pre>
  
  <p>Data above is taken from the JSON file, so, it's expected as that, so we can make it work with the SRecord tools in order to write that data at memory address of the main binary file holding the project that will make the hardware module work.</p>
  
  <hr>
  
  <h2>Third edit (solution and summary):</h2>
  
  <p>Thanks a lot to everyone!</p>
  
  <p>The answer of @AnsgarWiechers indeed shows what's needed to get the *.bin expected.</p>
  
  <p>If we take the example of <code>Data_File_1.json</code>:</p>
  
  <pre><code>{
      'device_1_data' : {
  
          'device_uuid' : {
              'low_uuid' : '0x00000001',
              'high_uuid' : '0xABCD000A'
          },
  
          'key_1' : ['0x2E', '0x34', '0x72', '0x0A', '0x45', '0x57', '0x41', '0x17', '0x07', '0x52', '0xCA', '0x4C', '0x0D', '0x7B', '0x41', '0x57'],
  
          'module_data' : "0x387A3830",
          'frequency_0' : '390200000',
          'frequency_1' : '392100000',
  
          'generator_syncword' : '0xCF'
  
      }
  }
  </code></pre>
  
  <p>We can proceed then to perform some operations in powershell as follows:</p>
  
  <pre><code># JSON processing.
  $json = Get-Content 'C:\&lt;path to json file&gt;\device_data.json' -Raw | ConvertFrom-Json
  </code></pre>
  
  <p>Now that we got the json object to work, we proceed to start taking the data.</p>
  
  <p>First, fetch the UUID. and declare the byte array to be used concatenating the first byte values.</p>
  
  <p><strong>BEWARE HERE, if you know a workaround here, or the best programming practice, that will be awesome, I'm starting with this, so, I came down to what is below by reading and coding on-the-go.</strong></p>
  
  <pre><code># Process the whole UUID from the two JSON fields.
  
  [int64] $device_uuid = [int64] ($json.device_1_data.device_uuid.low_uuid + (($json.device_1_data.device_uuid.high_uuid).split('x')[1]))
  
  $device_uuid_byte_array = [BitConverter]::GetBytes($device_uuid)
  
  # Final Byte array for first target address.
  
  [byte[]] $final_byte_array = $device_uuid_byte_array
  </code></pre>
  
  <p>Above I took advantage of the BitConverter function usage with int values. First a String concat is made with the high and low address, you can notice the order of the concatenation, and is because the endianness processed by powershell, so, in order to get the bytes in the following order <code>01 00 CD AB 0A 00 00 00</code>, powershell needs the string like <code>"0x00000001ABCD0001"</code>, <strong>not like</strong> <code>0xABCD00100000001</code>.</p>
  
  <p>Then it converts the value to an integer, the result of that is <code>7177306113</code>. And making use of the function:</p>
  
  <p><code>[BitConverter]::getBytes(7177306113)</code></p>
  
  <p>We can get the Byte value array of that. So it can get concatenated, or in this case, be the starting value of the byte array that will be at the content of the binary file.</p>
  
  <p>Next up is concatenation of the key string byte array of the JSON.</p>
  
  <pre><code># Now process the key byte array from JSON.
  [int[]] $key_1_value = [int[]] $json.device_1_data.key_1
  [byte[]] $key_1_byte_array = [byte[]][int[]]$key_1_value
  
  $final_byte_array += $key_1_byte_array
  </code></pre>
  
  <p>Now, can proceed with the <code>module_data</code> JSON field.</p>
  
  <pre><code>[int64] $module_data = [int64] $json.device_1_data.module_data
  
  $module_data_byte_array = [BitConverter]::GetBytes($module_data)
  
  $final_byte_array += $module_data_byte_array
  </code></pre>
  
  <p>Then the frequencies:</p>
  
  <pre><code>[int] $frequency_0 = [int] $json.device_1_data.frequency_0
  [byte[]]$frequency_0_byte_array = [BitConverter]::getBytes($frequency_0)
  $final_byte_array += $frequency_0_byte_array
  
  [int] $frequency_1 = [int] $json.device_1_data.frequency_1
  [byte[]] $frequency_1_byte_array = [BitConverter]::getBytes($frequency_1)
  $final_byte_array += $frequency_1_byte_array
  </code></pre>
  
  <p>Finally the syncword:</p>
  
  <pre><code>[int] $generator_syncword = [int] $json.device_1_data.generator_syncword
  
  $syncword_byte_array = [BitConverter]::GetBytes($generator_syncword)
  
  $final_byte_array += $syncword_byte_array
  </code></pre>
  
  <p>Once we have everything fetched and concatenated at the byte array we can perform a pipe operation to get a glimpse of what we're getting.</p>
  
  <p>That would be this preliminar output at powershell:</p>
  
  <pre><code>PS C:\Users\user&gt; $final_byte_array | format-hex
  
                 Ruta:
  
                 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F
  
      00000000   01 00 CD AB 01 00 00 00 2E 34 72 0A 45 57 41 17  ..Í«.....4r.EWA.
      00000010   07 52 CA 4C 0D 7B 41 57 30 38 7A 38 C0 FA 41 17  .RÊL.{AW08z8ÀúA.
      00000020   A0 F8 5E 17 CF 00 00 00                           ø^.Ï...
  </code></pre>
  
  <p>Now we can send that byte array to a file with extension *.bin, but as the data is appropiatelly written in bytes, there will be no issues with further steps with SRecord tools.</p>
  
  <pre><code># File storage
  [IO.File]::WriteAllBytes('C:\&lt;path desired to save&gt;\input_file.bin', $final_byte_array)
  </code></pre>
  
  <p>Once we have the file, we can work with srec_cat.exe, for illustrative purposes, let's suppose we're working with <strong>CMD</strong> at certain directory location, in which srec_cat.exe is located, as well as the <code>input_file.bin</code> we've just created.</p>
  
  <pre><code>&gt;srec_cat input_file.bin -binary -o srec_input_file.srec
  </code></pre>
  
  <p>If we open the file <code>srec_input_file.srec</code> it will show something like:</p>
  
  <pre><code>S0220000687474703A2F2F737265636F72642E736F75726365666F7267652E6E65742F1D
  S12300000100CDAB010000002E34720A455741170752CA4C0D7B415730387A38C0FA4117D5
  S10B0020A0F85E17CF000000F8
  S5030002FA
  </code></pre>
  
  <p>Then, we can convert it again, with hex-dump in order to see if we get the same byte values we saw back then at powershell output:</p>
  
  <pre><code>&gt;srec_cat srec_input_file.srec -o hex_input_file.bin -hex-dump
  </code></pre>
  
  <p>If we open <code>hex_input_file.bin</code>, we can see that:</p>
  
  <pre><code>00000000: 01 00 CD AB 01 00 00 00 2E 34 72 0A 45 57 41 17  #..M+.....4r.EWA.
  00000010: 07 52 CA 4C 0D 7B 41 57 30 38 7A 38 C0 FA 41 17  #.RJL.{AW08z8@zA.
  00000020: A0 F8 5E 17 CF 00 00 00                          # x^.O...
  </code></pre>
  
  <p>And we can use this file to concat it at the offset desire, let's say that we intend to write it from location <code>0x1A010</code>, we do something like:</p>
  
  <pre><code>&gt;srec_cat srec_input_file.srec -offset 0x1A010 -o srec_input_file_offset.srec
  </code></pre>
  
  <p>And we get something like:</p>
  
  <pre><code>S0220000687474703A2F2F737265636F72642E736F75726365666F7267652E6E65742F1D
  S22401A0100100CDAB010000002E34720A455741170752CA4C0D7B415730387A38C0FA411723
  S20C01A030A0F85E17CF00000046
  S5030002FA
  </code></pre>
  
  <p>Then we review it with:</p>
  
  <pre><code>&gt;srec_cat srec_input_file_offset.srec -o hex_input_file_offset.bin -hex-dump
  </code></pre>
  
  <p>And open it to see:</p>
  
  <pre><code>0001A010: 01 00 CD AB 01 00 00 00 2E 34 72 0A 45 57 41 17  #..M+.....4r.EWA.
  0001A020: 07 52 CA 4C 0D 7B 41 57 30 38 7A 38 C0 FA 41 17  #.RJL.{AW08z8@zA.
  0001A030: A0 F8 5E 17 CF 00 00 00                          # x^.O...
  </code></pre>
  
  <p>After this, we fill with <code>0xFF</code> the empty bytes of the <code>srec_input_file_offset.srec</code>:</p>
  
  <pre><code>&gt;srec_cat.exe srec_input_file_offset.srec -fill 0xFF 0x0001A030 0x0001A040 -output final_srec_input_file.srec
  </code></pre>
  
  <p>Finally, only append the <code>final_srec_input_file.srec</code> with the srec of the main bin file.</p>
  
  <p>Let's suppose we didn't convert the <code>main_file.bin</code>:</p>
  
  <pre><code>&gt;srec_cat main_file.bin -binary -o main_file.srec
  </code></pre>
  
  <p>Then we get everything together:</p>
  
  <pre><code>&gt;srec_cat main_file.srec srec_input_file_offset.srec -o final_file.bin -binary
  </code></pre>
  
  <p>And that's about that. Thanks a lot again for everything. Feedback and suggestions are very welcome.</p>
  
  <p>Kind regards.</p>