Page 1 of 1

hextostr.exe - convert a passed HEX string into a string of characters

Posted: 09 May 2022 13:57
by aGerman
A few days ago @lazna asked for a tool to convert a HEX string to text. So, I wrote a few lines in C.
Pass the HEX string as argument to hextostr.exe. The converted text is written to the standard output stream.

The lightweight 32 bit executable is highly liberal in what it accepts. Every found pair of contiguous HEX digits is converted into a character. Lone-standig HEX digits, spaces, etc. are just ignored. Additionally, anything passed to the tool is treated as a single argument, regardless of whether it was enclosed in quotes.
Some examples, all converted to "AZ"

Code: Select all

hextostr 415A
hextostr " 41 5A "
hextostr   41 5a 
hextostr 0x41 0x5a
hextostr "&H41 &H5a"
hextostr \x41\x5A
hextostr %%41%%5A
for /f "delims=" %%s in ('hextostr "415a"') do echo captured: %%s
However, the fact that almost no rules are defined also means that some outcomes may not seem obvious.

Code: Select all

hextostr foobar
This is not entirely ignored because "foobar" contains "ba" which is a pair of valid hex digits.

Note: The amount of data which can be converted is limited by the maximum length of a command line because the HEX string is to be passed as argument. Consider to use CERTUTIL -DECODEHEX for the conversion of a HEX-encoded file.

Steffen

Re: hextostr.exe - convert a passed HEX string into a string of characters

Posted: 12 May 2022 03:06
by lazna
thanks @aGerman,

just tested in complex script where HEX_2_STR conversion is done 20 times, and your program is a very little faster than XXD use (6.1 vs 6.2 seconds).

Re: hextostr.exe - convert a passed HEX string into a string of characters

Posted: 12 May 2022 06:08
by aGerman
Thanks for your feedback @lazna!
Yeah, I didn't expect the tool to be much faster than others. The algorithms are more or less always the same. I just tried to be smart by avoiding memory allocations and by using a lookup array. However, the improvement is likely only a few milliseconds in the end. I guess the measurable difference you are seeing is that you use a pipe to pass the string to XXD. This means that additional processes are created for both sides of the pipe.

Steffen