Convert 1111 2222 3333 in \x11\x11\x22\x22\x33\x33
Moderator: DosItHelp
-
- Posts: 22
- Joined: 23 Oct 2018 17:28
Convert 1111 2222 3333 in \x11\x11\x22\x22\x33\x33
Hi how can I convert a text.txt file containing the strings
1111 2222 3333
to
\x11\x11\x22\x22\x33\x33
1111 2222 3333
to
\x11\x11\x22\x22\x33\x33
Re: Convert 1111 2222 3333 in \x11\x11\x22\x22\x33\x33
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!
)
)
)
Steffen
-
- Posts: 22
- Joined: 23 Oct 2018 17:28
Re: Convert 1111 2222 3333 in \x11\x11\x22\x22\x33\x33
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
-
- Posts: 22
- Joined: 23 Oct 2018 17:28
Re: Convert 1111 2222 3333 in \x11\x11\x22\x22\x33\x33
I would need a generic script to transform even long strings
Re: Convert 1111 2222 3333 in \x11\x11\x22\x22\x33\x33
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
Batch has so many limitations. The code we are able to write is only as good as the information you give.
Steffen
-
- Posts: 22
- Joined: 23 Oct 2018 17:28
Re: Convert 1111 2222 3333 in \x11\x11\x22\x22\x33\x33
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
Re: Convert 1111 2222 3333 in \x11\x11\x22\x22\x33\x33
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.
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.
Re: Convert 1111 2222 3333 in \x11\x11\x22\x22\x33\x33
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:
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.
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
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 -
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 -
Dave Benham
Re: Convert 1111 2222 3333 in \x11\x11\x22\x22\x33\x33
... and you also should read the very first post in this forum...
Antonio
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