Convert 1111 2222 3333 in \x11\x11\x22\x22\x33\x33

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
stondesign
Posts: 22
Joined: 23 Oct 2018 17:28

Convert 1111 2222 3333 in \x11\x11\x22\x22\x33\x33

#1 Post by stondesign » 05 Apr 2019 06:21

Hi how can I convert a text.txt file containing the strings

1111 2222 3333
to
\x11\x11\x22\x22\x33\x33

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

Re: Convert 1111 2222 3333 in \x11\x11\x22\x22\x33\x33

#2 Post by aGerman » 05 Apr 2019 06:37

Code: Select all

@echo off &setlocal

set "in_file=test.txt"
set "out_file=test2.txt"

setlocal EnableDelayedExpansion
<"!in_file!" >"!out_file!" (
  for /f %%i in ('type "!in_file!"^|find /c /v ""') do for /l %%j in (1 1 %%i) do (
    set "line=" &set /p "line="
    if defined line (
      set "line=!line: =!"
      echo \x!line:~0,2!\x!line:~2,2!\x!line:~4,2!\x!line:~6,2!\x!line:~8,2!\x!line:~10,2!
    )
  )
)
If that doesn't work as expected then better provide real data.

Steffen

stondesign
Posts: 22
Joined: 23 Oct 2018 17:28

Re: Convert 1111 2222 3333 in \x11\x11\x22\x22\x33\x33

#3 Post by stondesign » 05 Apr 2019 06:44

0000 d601 ac03 7805 0cfe 0cfe 0cfe 0cfe 9808 9808 9808 280a 280a 280a 280a 1c0c 1c0c 1c0c 10c0 ac0d ac0d ac0d ac0d 100e 100e 100e 100e 100e 100e 100e 100e 100e 100e 100e 100e de0d de0d de0d de0d 0cfe 0cfe 0cfe 0cfe 0cfe 0cfe 0cfe 0cfe 0cfe 0cfe 0cfe 0cfe e803 e803 e803 e803 e803 e803 e803 e803 e803 e803 e803 e803 e803 e803 e803 e803 e803 e803 e803 e803 0a00

stondesign
Posts: 22
Joined: 23 Oct 2018 17:28

Re: Convert 1111 2222 3333 in \x11\x11\x22\x22\x33\x33

#4 Post by stondesign » 05 Apr 2019 06:46

I would need a generic script to transform even long strings

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

Re: Convert 1111 2222 3333 in \x11\x11\x22\x22\x33\x33

#5 Post by aGerman » 05 Apr 2019 06:50

Does the file consist of only one line? Have lines always the same length? What is the maximum length of a line?
Batch has so many limitations. The code we are able to write is only as good as the information you give.

Steffen

stondesign
Posts: 22
Joined: 23 Oct 2018 17:28

Re: Convert 1111 2222 3333 in \x11\x11\x22\x22\x33\x33

#6 Post by stondesign » 05 Apr 2019 06:58

The string may vary in length but the shape is 4 letters and numbers attached and then space and 4 more letters and numbers Now to overcome this problem I downloaded a tasker that stores the steps on the PC screen and repeats them over and over again

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

Re: Convert 1111 2222 3333 in \x11\x11\x22\x22\x33\x33

#7 Post by aGerman » 05 Apr 2019 07:02

aGerman wrote:
05 Apr 2019 06:50
Does the file consist of only one line? [...] What is the maximum length of a line?

Squashman
Expert
Posts: 4111
Joined: 23 Dec 2011 13:59

Re: Convert 1111 2222 3333 in \x11\x11\x22\x22\x33\x33

#8 Post by Squashman » 05 Apr 2019 07:51

On StackOverFlow, when we see a broad question with limited information, we will direct the user to read the following two links:
How to ask a good question?
How to create a Minimal, Complete, and Verifiable example.

This seems like a good time to link to this information.

dbenham
Expert
Posts: 2283
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Convert 1111 2222 3333 in \x11\x11\x22\x22\x33\x33

#9 Post by dbenham » 05 Apr 2019 09:42

Due to the many limitations and idiosyncrasies of batch, I rarely use pure batch to manipulate text files anymore.

I find it much easier to use JREPL.BAT. Once you learn regular expressions and study the JREPL options, it is usually fairly easy to get the result you need, and it is significantly faster than pure batch.

For your little problem, I would use:

Code: Select all

call jrepl "\S\S|\s+" "\$&|" /t "|" /f "text.txt" /o -
This prepends \ before each pair of non-whitespace characters, and removes all whitespace from each line.

The above has a limitation that it will corrupt the result if you use it two or more times on the same file.

Below is a variation that does the conversion only when needed, and leaves already transformed files alone.

Code: Select all

call jrepl "([0-9a-f]{2})([0-9a-f]{2})|\s+" "\$2\$3|" /i /t "|" /f "text.txt" /o -
It looks for four consecutive hex digits, and inserts the slashes before the two pairs, and also removes all whitespace from each line. Once the file has been transformed, there are no more instances of four consecutive hex digits, nor is there any whitespace, so subsequent runs make no further changes.


Dave Benham

Aacini
Expert
Posts: 1615
Joined: 06 Dec 2011 22:15
Location: México City, México

Re: Convert 1111 2222 3333 in \x11\x11\x22\x22\x33\x33

#10 Post by Aacini » 05 Apr 2019 09:44

... and you also should read the very first post in this forum...

Code: Select all

@echo off
setlocal EnableDelayedExpansion

(for /F "delims=" %%a in (input.txt) do (
   set "line="
   for %%b in (%%a) do (
      set "num=%%b"
      set "line=!line!\x!num:~0,2!\x!num:~2!
   )
   echo !line!
)) > output.txt
move /Y output.txt input.txt
Antonio

Post Reply