DosTips.com

A Forum all about DOS Batch
It is currently 26 Sep 2017 14:05

All times are UTC-06:00




Post new topic  Reply to topic  [ 6 posts ] 
Author Message
 Post subject: Hex To Little Endian
PostPosted: 11 Feb 2017 13:17 
Offline

Joined: 11 Feb 2017 13:01
Posts: 5
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.


Top
   
 Post subject: Re: Hex To Little Endian
PostPosted: 11 Feb 2017 17:52 
Offline
Expert

Joined: 22 Jan 2010 18:01
Posts: 2904
Location: Germany
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


Top
   
 Post subject: Re: Hex To Little Endian
PostPosted: 11 Feb 2017 18:07 
Offline

Joined: 11 Feb 2017 13:01
Posts: 5
Worked Perfect, Thanks!!


Top
   
 Post subject: Re: Hex To Little Endian
PostPosted: 16 Feb 2017 00:42 
Offline

Joined: 16 Dec 2016 22:31
Posts: 100
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


Top
   
 Post subject: Re: Hex To Little Endian
PostPosted: 16 Feb 2017 15:19 
Offline
Expert

Joined: 22 Jan 2010 18:01
Posts: 2904
Location: Germany
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


Top
   
 Post subject: Re: Hex To Little Endian
PostPosted: 16 Feb 2017 22:40 
Offline

Joined: 16 Dec 2016 22:31
Posts: 100
@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


Top
   
Display posts from previous:  Sort by  
Post new topic  Reply to topic  [ 6 posts ] 

All times are UTC-06:00


Who is online

Users browsing this forum: Google [Bot] and 16 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Limited