11

I have a hex file that I am attempting to convert to binary. I have performed this on Linux before using the command:

xxd -r -p mykey.hex > mykey.bin

But I cannot figure out how to do this same command on Windows. I have downloaded a few hex editor tools but not really sure how to use them to convert hex into a binary representation.

Is there a way to do this in Windows, or are there any similar tools out there that would allow me to do this?

bertieb
  • 7,344
  • 36
  • 42
  • 54
Kevin
  • 311
  • 1
  • 4
  • 14
  • I have attempted the similar once. I succeeded by turning the source into a javascript-escaped string with a regex and unescaping. –  Nov 01 '12 at 17:22
  • From @fadelix: you can check this articles: http://indogeeks.com/how-to-convert-any-files-to-hex-with-script/ – fixer1234 Sep 21 '15 at 03:49
  • @fixer1234, OP wanted hex-to-binary - not the other way round. Also, the scripts you linked to appear to only produce a hex dump of the binary file. That's OK if it's all you need, but a true hex file is usually in [Motorola](https://en.wikipedia.org/wiki/SREC_(file_format)) or [Intel hex](https://en.wikipedia.org/wiki/Intel_HEX) format, with framing and checksums added. – rossmcm Nov 17 '15 at 22:57
  • So the ge.tt link broke in my answer and I just fixed it. Why did you just change your accepted answer, to an answer that points to unxutils that many including you yourself even saw doesn't contain xxd? Even the author of that answer apologised for his link not having xxd. That is extremely irresponsible. Accepting answers you know are wrong while also not commenting when an issue occurs with an answer, is very irresponsible, I have downvoted you accordingly. If others had known you'd do that they would not have upvoted you. – barlop Nov 06 '18 at 09:14

9 Answers9

19

VIM 7.x contains xxd for Windows

https://ftp.nluug.nl/pub/vim/pc/gvim73_46_s.zip

C:\Program Files (x86)\Vim\vim74>.\xxd -v
xxd V1.10 27oct98 by Juergen Weigert (Win32)

C:\Program Files (x86)\Vim\vim74>dir xxd.exe
10/08/2013  12:33 PM            70,144 xxd.exe

C:\Program Files (x86)\Vim\vim74>file xxd.exe
xxd.exe; PE32 executable for MS Windows (console) Intel 80386 32-bit

Cygwin has one also

C:\cygwin\bin>.\xxd.exe -v
xxd V1.10 27oct98 by Juergen Weigert

C:\cygwin\bin>dir xxd.exe
18/09/2015  05:44 AM            18,963 xxd.exe

C:\cygwin\bin>file xxd.exe
xxd.exe: PE32+ executable (console) x86-64, for MS Windows

added

xxd is available https://sourceforge.net/projects/xxd-for-windows/

xxd v1.11, 8 jun 2013 by Juergen Weigert et al. (Win32)

C:\Users\User>C:\xxd1p11\xxd-1.11_win32\xxd.exe -v
xxd v1.11, 8 jun 2013 by Juergen Weigert et al. (Win32)

C:\Users\User>
barlop
  • 23,380
  • 43
  • 145
  • 225
  • note- my old cygwin xxd.exe was "26/08/2014 12:00 AM 17,437 xxd.exe" I doubt even that the diff file size is relevant though. I didn't run the file.exe command on it though I'm sure it was 64 bit 'cos the -v output was the same. Odd that the file size was different, not that it matters. I just updated the answer to have the file.exe command run on it, just to show whether 32 bit or 64 bit though that's shown with the version info anyway with -v. The 32bit one says 32bit with the -v and the 64bit doesn't say 32bit which is meant to imply that it's 64bit since the 32bit xxd -v says 32bit. – barlop Nov 12 '15 at 12:57
  • Chrome blocks `xxd.exe` from http://ge.tt/5jfutZq/v/0 as malware. – rossmcm Nov 17 '15 at 23:00
  • @rossmcm chrome is being ridiculous.. you can go to the downloads section in chrome and click "recover malicious file" http://i.imgur.com/6vBebqL.png Or, get it from cygwin or vim. – barlop Nov 18 '15 at 00:01
  • OK, `xxd.exe` was only flagged by 1 out of 40 at VirusTotal so I'm happy. Note that it doesn't seem to convert *hex files* to binary however. I was looking for something that took [Intel](https://en.wikipedia.org/wiki/Intel_HEX) or [Motorola](https://en.wikipedia.org/wiki/SREC_(file_format)) hex files and converted them to a binary image. This [hex2bin](https://sourceforge.net/projects/hex2bin/) at SourceForge did the trick. – rossmcm Nov 18 '15 at 06:06
  • @rossmcm the question has a use case of xxd converting hex to binary `xxd -r -p mykey.hex > mykey.bin` – barlop Dec 24 '16 at 20:21
  • @barlop if rossmcm meant hex to 101010101101 kind of binary then no. To executable kind of binary, yes. – barlop Nov 06 '18 at 09:51
  • 1
    xxd is also https://archive.org/details/hexviewer_Xxd_x86 – barlop Nov 06 '18 at 09:51
  • VIM's xxd.exe 1.11 for Windows is now available standalone there: https://sourceforge.net/projects/xxd-for-windows/ – Jean-François Larvoire Dec 30 '20 at 17:40
2

use powershell format-hex command:

PS C:\path\path\Documents\blah> format-hex Util3.class


           Path: C:\blah\blah\Documents\blah\Util3.class

           00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F

00000000   CA FE BA BE 00 00 00 34 00 1F 0A 00 07 00 10 09  Êþº¾...4........
etc...
mcgyver5
  • 1,101
  • 3
  • 11
  • 19
  • This question is about converting a hex dump into a binary, not vice versa! – Monkey Supersonic Dec 03 '18 at 09:46
  • 2
    According to the doc, that is supposed to be supported. But it seems like this tool only returns a pretty formatted string that looks like your typical hex editor. You can't do anything with the 3 columns returned (ignoring metadata). Interested if someone sees it differently and has an example converting from hex back to binary. – TamusJRoyce Apr 20 '19 at 20:54
2

To convert a hex string into a binary file, this works in PowerShell Core 7.2.1:

$hex_string = '48656C6C6F2C20576F726C6421'
$hex_string_spaced = $hex_string -replace '..', '0x$& '
$byte_array = [byte[]] -split $hex_string_spaced
Set-Content -Path 'out.bin' -Value $byte_array -AsByteStream

That last line won't work in PowerShell 5.1, but you can do this instead:

Set-Content -Path 'out.bin' -Value $byte_array -Encoding Byte

PowerShell comes pre-installed with Windows.

durette
  • 343
  • 2
  • 6
1

WinHex is the best for you to kick off stupid DOS command.

  1. Open Hex file
  2. Edit>>Converter File
  3. In the dialog box, Select Intel Hex to Binary
  4. Hit OK button.
  5. Round up file Size? Click No.
  6. SAVE File as Bin.
1

There is a native port of xxd to Win32.

See unxutils here for downloads.

Another source of Win32 ports of common GNU and/or *nix utilities is GnuWin32.

Added By Barlop

This answer is incorrect, see comments, xxd is not in unxutils, and not in gnuwin32 either.

barlop
  • 23,380
  • 43
  • 145
  • 225
Greg Jandl
  • 144
  • 3
  • 1
    I have downloaded this package but I do not see the xxd.exe anywhere in the package.. –  Nov 01 '12 at 17:40
  • My apologies. I swear that's where I fetched it from long ago. –  Nov 01 '12 at 17:44
  • @Kevin, *which* one did you get and not find it in? It’s not in GnuWin32, it’s in the first paged that Greg linked to, in the table towards the middle of the page (just use your browser’s search function and type `xxd`). Also, typing `xxd` into the search-field of the GnuWin32 page brings up the [reversable hexdump](http://gnuwin32.sourceforge.net/packages/hextools.htm) that it includes. – Synetech Aug 30 '13 at 17:07
  • OK @barlop, I'm _really_ confused. I just downloaded [UnxUtlis.zip] (http://www.weihenstephan.de/~syring/win32/UnxUtils.zip) and I can't find `xxd` in there, or anything else that looks like it might be a hex-to-binary converter. (FWIW I can't get link markup to work either. Not my day). – rossmcm Nov 17 '15 at 22:30
  • @rossmcm I think I was just interpreting synetech and taking what he said as correct..when I commented about it being in unxutils. I've just deleted that comment of mine 'cos it's misleading. Maybe Synetech didn't check either(beyond the webpage that listed). But indeed, I don't see xxd in unxutils either, or in any unxutils that i'd extracted in the past either. – barlop Nov 18 '15 at 00:03
  • @Synetech ross and I don't see it in unxutils (or unxupdates), it's not there. – barlop Nov 18 '15 at 00:06
  • @Synetech while it's listed in the table on the unxutils webpage, it's not in the unxutils zip file (so doesn't address the question of where to get it). And you mention gnuwin32, though you agree it's not in gnuwin32. You say "typing xxd into the search-field of the GnuWin32 page brings up the reversable hexdump that it includes" <-- no xxd there in that gnuwin32 hexdump zip file. – barlop Aug 29 '20 at 18:46
  • @barlop, it's been almost a decade; the files have likely changed. Also, I never said xxd was in the hexdump file, I said that searching for xxd on the GnuWin32 site, brings up a search results for hexdump which does a similar function. If a Windows port of xxd is necessary, you can probably get it with MinGW or MSYS2. – Synetech Aug 30 '20 at 19:17
  • @Synetech here is a directory listing from my `C:\msys64\usr\bin` directory https://pastebin.com/raw/AhZijKtb no xxd there – barlop Aug 30 '20 at 22:50
  • @Synetech read the comments. You were wrong in 2012, when you wrote that it is in unxutils, it wasn't in unxutils zip file then. – barlop Aug 30 '20 at 22:52
  • @barlop, well, I'll just have to take your word for it since the distributions have changed a lot in the past eight years, and I can't be arsed to bother to see if the WBM has a copy from 2012. And even if it wasn't, that's obviously an error since it was listed, so take it up with them. I'm not sure why this is so important to you, but did you check MinGW or try using hexdump? Regardless, here's a [Windows port of xxd](https://helloacm.com/c-coding-exercise-xxd-make-hex-dump-on-windows/). (Oh, I just realized why it's so important to you. ) – Synetech Aug 31 '20 at 23:09
  • I confirm it's listed in the unxutils list of programs, but actually missing from both UnxUtils.zip and UnxUpdates.zip. I searched in my archives, and found versions I had downloaded in 2004, which were already missing xxd.exe. The good news is that a new full-featured port of xxd.exe for Windows is now available there: https://sourceforge.net/projects/xxd-for-windows/ – Jean-François Larvoire Dec 30 '20 at 17:37
0

I wrote this in C and tested it in Cygwin.

Usage:

gcc stdin_hex_to_stdout_bin.c -o stdin_hex_to_stdout_bin
echo '48656C6C6F2C20576F726C6421' | ./stdin_hex_to_stdout_bin
cat some_hex_text_file.txt | ./stdin_hex_to_stdout_bin

stdin_hex_to_stdout_bin.c

#include <stdio.h>
#include <stdbool.h>

int input_char_to_mask(int input_char);

int main() {
   int inputc;
   int outputc;
   int outputmask;
   int mask;
   bool high = false;
   do {
      inputc = getc(stdin);
      outputmask = input_char_to_mask(inputc);
      if (outputmask != 0xFF) {
         if (!high) {
            outputc = outputmask * 16;
         } else {
            outputc = outputc | outputmask;
            putc(outputc, stdout);
         }
         high = !high;
      }
   } while (inputc != EOF);
   return 0;
}

int input_char_to_mask(int input_char) {
   switch (input_char) {
      case '0': return 0x00;
      case '1': return 0x01;
      case '2': return 0x02;
      case '3': return 0x03;
      case '4': return 0x04;
      case '5': return 0x05;
      case '6': return 0x06;
      case '7': return 0x07;
      case '8': return 0x08;
      case '9': return 0x09;
      case 'A': return 0x0A;
      case 'B': return 0x0B;
      case 'C': return 0x0C;
      case 'D': return 0x0D;
      case 'E': return 0x0E;
      case 'F': return 0x0F;
      case 'a': return 0x0A;
      case 'b': return 0x0B;
      case 'c': return 0x0C;
      case 'd': return 0x0D;
      case 'e': return 0x0E;
      case 'f': return 0x0F;
      default : return 0xFF;
   }
}
phuclv
  • 26,555
  • 15
  • 113
  • 235
durette
  • 343
  • 2
  • 6
0

If you are nuts, you could try processing the characters via input and then outputting the values 8 bits at a time using a programming language

Joe Plante
  • 111
  • 3
0

Use this hex to bin converted: (works in browser, no software install necessary)

http://matrixstorm.com/avr/hextobin/ihexconverter.html

gronostaj
  • 55,965
  • 20
  • 120
  • 179
vouvoume
  • 1
  • 1
-1

You can use the free Mcumall GQ4X programmer software without the programmer. Select the device and load the hex file. Save as a bin file and that is it.

Paul
  • 4,764
  • 29
  • 41