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

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

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

#1 Post by aGerman » 09 May 2022 13:57

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
Attachments
hextostr_1.2.zip
(2.44 KiB) Downloaded 190 times
Last edited by aGerman on 19 May 2022 12:47, edited 2 times in total.
Reason: even fewer processor instuctions in the executable

lazna
Posts: 53
Joined: 27 Dec 2012 10:54

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

#2 Post by lazna » 12 May 2022 03:06

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).

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

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

#3 Post by aGerman » 12 May 2022 06:08

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

Post Reply