Hex To Little Endian

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
jbgtenn
Posts: 5
Joined: 11 Feb 2017 13:01

Hex To Little Endian

#1 Post by jbgtenn » 11 Feb 2017 13:17

I was needing help with a batch file that would ask for user Input and the script would reverse byte order and save as a variable to be used later in another script.

For example :

User Input : 1B C9 6F 9A Would need it to converted to 9A 6F C9 1B with spaces.

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

Re: Hex To Little Endian

#2 Post by aGerman » 11 Feb 2017 17:52

You could use a FOR /F loop.

Code: Select all

set /p "BE=Enter four bytes: "
for /f "tokens=1-4" %%i in ("%BE%") do set "LE=%%l %%k %%j %%i"
echo %LE%


Steffen

jbgtenn
Posts: 5
Joined: 11 Feb 2017 13:01

Re: Hex To Little Endian

#3 Post by jbgtenn » 11 Feb 2017 18:07

Worked Perfect, Thanks!!

Sounak@9434
Posts: 100
Joined: 16 Dec 2016 22:31

Re: Hex To Little Endian

#4 Post by Sounak@9434 » 16 Feb 2017 00:42

I'm a bit late, am not I?
Though if you don't know the number of arguments you can also use this method.

Code: Select all

:reverse
set reargs=
call :reargs %*
echo(%reargs%
exit /b
:reargs
set "reargs=%1 %reargs%"
shift
if not "%1"=="" goto reargs

Input wrote:call :reverse 1 2 3 4 5 6 7 8 9 10

Output wrote:10 9 8 7 6 5 4 3 2 1

Hope this helps.

Sounak

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

Re: Hex To Little Endian

#5 Post by aGerman » 16 Feb 2017 15:19

Sounak@9434 wrote::reverse

Actually reverting is not the same as changing endianness (byte order). The base type has a width of powers of 2. If the number of entered bytes exceeds the width of the type you may rather assume it's an array of elements with type width each.

Code: Select all

@echo off &setlocal EnableDelayedExpansion
set "BE=" &set "LE=" &set "unit="

choice /c 248 /m "Width of one unit"
set /a "width=1<<%errorlevel%"

set /p "BE=Enter the bytes: "

:loop
for /f "tokens=1-%width%*" %%i in ("%BE%") do (
  set "b1=%%i" &set "b2=%%j" &set "b3=%%k" &set "b4=%%l" &set "b5=%%m" &set "b6=%%n" &set "b7=%%o" &set "b8=%%p" &set "rm=%%q"
)

if not defined b1 set "b1=00"
if not defined b2 set "b2=00"
set "unit=%b2% %b1%"
set "BE=%b3%"

if %width% gtr 2 (
  if not defined b3 set "b3=00"
  if not defined b4 set "b4=00"
  set "unit=!b4! !b3! %unit%"
  set "BE=%b5%"
)

if %width% gtr 4 (
  if not defined b5 set "b5=00"
  if not defined b6 set "b6=00"
  if not defined b7 set "b7=00"
  if not defined b8 set "b8=00"
  set "unit=!b8! !b7! !b6! !b5! %unit%"
  set "BE=%rm%"
)

set "LE=%LE% %unit%"
if defined BE goto loop

echo %LE:~1%
pause


Code: Select all

Width of one unit [2,4,8]?4
Enter the bytes: 00 01 02 03 04 05 06 07
03 02 01 00 07 06 05 04
Drücken Sie eine beliebige Taste . . .

Steffen

Sounak@9434
Posts: 100
Joined: 16 Dec 2016 22:31

Re: Hex To Little Endian

#6 Post by Sounak@9434 » 16 Feb 2017 22:40

@aGerman: Okay, my answer was actually made around the request and I thought a simple reversing tool could do the work. Maybe multiple calls could do the work but definitely it is slower as it uses 'call :label' .
Your new answer solves the problem. Great work Steffen. :D

Post Reply