Hex to bin linux

Converting from *.hex to *.bin for ARM on Linux

I want to upload program to my STM32F4 Discovery board using st-flash command. Problem is when I try to upload *.hex or *.elf file it is just not working. I tried many ways ( like using xxd ) of converting from *.elf or *.hex to *.bin but it is still not working when I upload it. And yes, I tried uploading hex file from other Windows computer and it works. Sample ( first three lines, just to show you how it looks inside ) of hex file:

:020000040800F2 :100000000000022099020008A1020008A5020008D1 :10001000A9020008AD020008B102000800000000BB 

5 Answers 5

I assume you have linux and you have installed binutils , so you just do:

objcopy --input-target=ihex --output-target=binary code00.hex code00.bin 

Beware: this garbles the loading base address. Will work for OPs case though, as the ihex data bytes start at 0000. Also, any toolchain is good for this operation, not necessarily ARM.

@ulidtko: 1. the question is for ARM, hence the answer is for ARM; 2. the bin files do not contain loading base address which you claim will be garbled. you specify the address when you load the .bin

How do you change the start segment address? I don’t want it to base itself at 0x00000000 but instead 0x80000000 for example.

Have you considered using arm-none-linux-gnueabi-objcopy (or similar) instead of xxd? This can be found in any ARM toolchain.

Yes I have, but there is a possibility I did it wrong way. Now I am away from my computer. (using my mobile to post it) I think i used something like arm-none-eabi-objcopy myhex.hex -O binary mybin.bin but I’m not sure now. I’ll let you know when I come home.

Ok, I can access my PC now. So I’ve tried both options: arm-none-eabi-objcopy «F4 Discovery test.elf» -Obinary «out.bin» and arm-none-eabi-objcopy «F4 Discovery test.elf» -Oihex «newout.bin» — they don’t work, at least for me. I’m actually trying to understand OpenOCD and I’ve uploaded my *.hex and it works.

.hex file format is documented on the web. You need a loader program capable to understand it, as it has several kinds of registers to control the loading process. Some of the registers control entry point address. Others are data to be loaded at some fixed address.

Читайте также:  Disk info linux console

You can get information at the wikipedia (I have found it there) for Intel Hex format (that’s how it is called). If all the data is on only one segment and no entry point is specified, theoretically you can convert it to binary data to be loaded, but that’s improbable.

It is a text file made of lines beginning with ‘:’ character, then comes a two field hex number representing the number of bytes of data this record has, then the address this data is to be loaded on, then the type of file, it can be one of:

  • 00 This value is for a bunch of data, normally 16 bytes (0x10)
  • 01 End of file. It has no data, so always is codified as :00000001FF
  • 02 Extended segment address, to allow addresses with more than 16bit.
  • 03 Start Entry point address, to register the initial CS:IP address in 0x86 architecture.
  • 04 Extended Linear Address, to specify 32bit addresses. This specifies the upper 16bit address part of 00 registers.
  • 05 Start Entry point Linear Address. This is the 32 bit linear entry point address.

Then comes n bytes (n is the value of the first field) of data (hex coded) to be loaded and finally a checksum byte (the sum in two’s complement of all the record bytes from the colon up).

So what do you propose for me? I was using st-flash write bingotfromhex.bin 0x8000000 . I am actually learning openOCD little steps so I can upload *.elf files. Thanks for explaining how hex files work!

I don’t know. Intel hex format is a general, very extended format. I don’t work with ARMs. I cannot give you much more help. Sorry. Perhaps you have to use an option for st-flash to be able to parse hex files. Have you tried st-flash —help .

I present a function below to allow this:

hex2bin path/to/myfirmware1.hex hex2bin path/to/myfirmware1.hex path/to/myfirmware2.hex hex2bin myfirmware1.hex myfirmware2.hex myfirmware3.hex myfirmware4.hex # etc. 

Bash function to mass-convert Intel *.hex firmware files to *.bin firmware files, and to *.xxd.hex files for comparison in meld

To add onto @A. Genchev’s answer: I find it tedious to have to have such a long command when I want to convert many hex files at once, so I wrote this helper function.

Читайте также:  Ошибка при сборке линукс

Copy and paste this into the bottom of your ~/.bashrc file, then run . ~/.bashrc to re-source your ~/.basrhc file and make this function available to you:

# Function to convert .hex firmware files to .bin # # Example usage: # # # this produces "path/to/myfile.bin" from "path/to/myfile.hex" # hex2bin path/to/myfile.hex # # # you can pass multiple paths at once too # hex2bin path/to/myfile1.hex path/to/myfile2.hex path/to/myfile3.hex # hex2bin() < # treat all input args as file paths for filepath_hex in "$@"; do # See: https://stackoverflow.com/a/965072/4561887 filepath_hex_no_extension="$" filepath_bin="$.bin" # debugging # echo "filepath_hex_no_extension = $filepath_hex_no_extension" # echo "filepath_bin = $filepath_bin" echo "Converting \"$filepath_hex\" to \"$filepath_bin\"." objcopy --input-target=ihex --output-target=binary \ "$filepath_hex" "$filepath_bin" done > # (Optional) add an alias prefixed with your initials so you can find all your # custom aliases and functions easily by typing your initials followed by an # underscore and hitting Tab Tab. alias gs_hex2bin="hex2bin" 

If you have a different toolchain, just replace objcopy with the version of objcopy from your toolchain. Ex: for the Microchip MPLAB XC32 compiler toolchain for PIC microcontrollers, use xc32-objcopy instead of objcopy .

objcopy --input-target=ihex --output-target=binary \ path/to/myfirmware1.hex path/to/myfirmware1.bin objcopy --input-target=ihex --output-target=binary \ path/to/myfirmware2.hex path/to/myfirmware2.bin 
hex2bin path/to/myfirmware1.hex path/to/myfirmware2.hex 

You can pass in as many pathnames as you want all at once.

Going further: comparing and analyzing hex/binary file differences using objcopy , xxd , and meld

What if you want to compare two Intel hex firmware files to look for differences? Perhaps two hex firmware images are nearly identical, but differ only in some strings, IP addresses, or timestamps stored inside of them. That would be nice to know.

# - for "path/to/myfirmware1.hex", produce both "path/to/myfirmware1.bin" # and "path/to/myfirmware1.xxd.hex" # - for "path/to/myfirmware2.hex", produce both "path/to/myfirmware2.bin" # and "path/to/myfirmware2.xxd.hex" hex2xxdhex "path/to/myfirmware1.hex" "path/to/myfirmware2.hex" # now compare the two `.xxd.hex` output files in `meld` meld "path/to/myfirmware1.xxd.hex" "path/to/myfirmware2.xxd.hex" 

enter image description here

enter image description here

Here is the definition of my hex2xxdhex function:

# Function to convert .hex firmware files to .bin and then to a # human-compare-friendly .xxd.hex, so you can easily compare two files with # `diff` or `meld`. # - See my answer here: https://superuser.com/a/1790518/425838 # # Example usage: # # # this produces both "path/to/myfile.bin" and "path/to/myfile.xxd.hex" # # from "path/to/myfile.hex" # hex2xxdhex path/to/myfile.hex # # # you can pass multiple paths at once too # hex2xxdhex path/to/myfile1.hex path/to/myfile2.hex # # then compare the two output ".xxd.hex" files with `meld` # meld path/to/myfile1.xxd.hex path/to/myfile2.xxd.hex # hex2xxdhex() < # treat all input args as file paths for filepath_hex in "$@"; do # See: https://stackoverflow.com/a/965072/4561887 filepath_hex_no_extension="$" filepath_bin="$.bin" filepath_xxdhex="$.xxd.hex" echo "Converting \"$filepath_hex\" to \"$filepath_bin\" and to"\ "\"$filepath_xxdhex\"." objcopy --input-target=ihex --output-target=binary \ "$filepath_hex" "$filepath_bin" xxd "$filepath_bin" "$filepath_xxdhex" done > alias gs_hex2xxdhex="hex2xxdhex" 

Источник

Convert hexadecimal to binary on Linux CLI

I have a text file with lines (each of 631 characters) consisting of 16 hexadecimal numbers 0,1,2. 9,A,B. F . I would like to replace each number with the corresponding 4 bits of 0 and 1, i.e. 0 with 0000, 1 with 0001, . F with 1111 and then save the file as .bin. How can I do so in Linux terminal? Some lines of my text file are:

85868D6B0FD5F3186DE92A7FC023CC848110FE61E3045B24BD905C8FDF34698794718CB2BAFF2AFC488C07B01510646141290A8FE55BF41F9D5033B13B31473FB4F7E199624370DC9C7587E5FB84B209B104B2BC194243EFC77C479BEA6778C73FB55939CB8654B1EB0B597CEA9507FE5A9636229BE0E7ECA27844D3A4AE16096D9DDC125728A1BF0C3623518311CF7CE629C15EA767E1F6C992106E86D962D47E307AE9F71FE5C6B664820E3D7521D0F783913E8A116BFD42C65FD2E28C9114CF4BFCB8A2AFD9979E7B5EBC7B59C7A31A7C12931D2C794EF5AED991B32560BBEFCC82D3A6AC6D371C7AC6C06DED6BCEFFC3D9E11A17B4738EA2ECF8CDD14805E57C50F973C6832DB94517A71C06E18329694B8CB7F7028730E54F74E27EF35B3D776A7E46A3F187691D460CBFBCD14BAC4AED32E5592AFDAEC5206 6321EB4F15D5F01D2A5F7474AC6FFCD1A8B52E680AC52CA9B500C956FD269DB8107EFCF6EC0B23B2C54B3A727CC53F6793019BF0D95A037485413576E4A0172A597027ED71D6E785430FA4748E164E5290214B01740736C0EC79D286692170B6EFBA21592EC485350F0DC6C635CD3FA7DAED258405C4247BFA68613B5F7CBEC903E16EA2E780D0934F0A2918F837CF0115B3D76AAFD3F37D905F9572D334D959DDD22C37EBA7707A76333A491F79B9C53A7912AEA38ED400949EB9AD9FCEE71765F4F4A2E21C481A509AB6807DDF5374D9A1A4D0C589E628CAA3C6DC9E4A5C1A7EF3C36A4D2231E2C1E856A19B6F7F4DE713D5BE8A4EFA750FB29A34F00754D875C22C2FC981FAECCC7113D1B0D6FF3BCC747852789A662D104DF1A8C90EB4DDFA48502A3BA9AAE70E7229E8A3D6FBBD00C19D99765BAFC9B09D547 2F75AC10A9E20C07DD4686C5706F66EB1AB33ED9DEDAD69079B6EC0C24C8DF24CB1855825D80CD484E5623BB246041D29240720500EA87586CC4CE5A00990EF8B5B7E2A3CF3E997323870499451E7BFA7F23A2208523B84A98DDC0763701C1AB292D854ADA8FB2E060088D7B3436571745DF25182D7F5A646A6F7DD830B9CCC12C37D416882DD7AAA0E8CB2CE50CE33AD1674C0E78E0F171DEC2D087C6CF2E711FAC2548CCB8C0312D59055A0A3A145B402A749F768F637549E0749215B5542BBC9D07EF9416C1BAB1FBEAC4709C458E7C266342908421BC8F67B7E21ED228D13A0F2750C290EFF8CAF7D2AD6D6C2CB12B81570190E1BC555E0CFBA4951BEAD3740D3BEBF2C7BF2AB64C7F046E5B865D85A1411F854906C05785A0A3684EC0F69D9BAB047A9EC17F973E78A92838C73270F97E54CEF4FABAA82876F 

Источник

Читайте также:  Checking folder permissions linux

Linux shell scripting: hex number to binary string

I am looking for some easy way in shell script for converting hex number into sequence of 0 and 1 characters. Example:

5 Answers 5

echo "ibase=16; obase=2; 5F" | bc 

This is not the same as the selected answer. This has 5F , whereas the selected answer has 5f . This answer works in bc 1.06.95, wheras the selected answer does not.

I used ‘bc’ command in Linux. (much more complex calculator than converting!)

ibase parameter is the input base (hexa in this case), and obase the output base (binary).

Set obase before ibase . If ibase is defined first, bc will try to interpret obase as if it is written in ibase , with possibly erroneous results. See this question.

Using bc version 1.06.95 echo ‘ibase=16;obase=2;5f’ | bc produces error: (standard_in) 1: syntax error . However, echo ‘ibase=16;obase=2;5F’ | bc works and produces 1011111 .

$ printf '\x5F' | xxd -b | cut -d' ' -f2 01011111 
  • The i command will pop the top of the stack and use it for the input base.
  • Hex digits must be in upper case to avoid collisions with dc commands and are not limited to A-F if the input radix is larger than 16 .
  • The o command does the same for the output base.
  • The p command will print the top of the stack with a newline after it.

Thanks for response, it’s great to have so many ways of achieving this task 🙂 I’ve chosen the «bc» variant

Perl’s printf already knows binary:

$ perl -e 'printf "%08b\n", 0x5D' 01011101 
echo -n 5f5f5f5f5f | cryptocli dd -decoders hex -encoders binary_string 
0101111101011111010111110101111101011111 

NB: It’s not perfect and much work needs to be done but it is working.

Linked

Hot Network Questions

Subscribe to RSS

To subscribe to this RSS feed, copy and paste this URL into your RSS reader.

Site design / logo © 2023 Stack Exchange Inc; user contributions licensed under CC BY-SA . rev 2023.7.14.43533

By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.

Источник

Оцените статью
Adblock
detector