Blog coding and discussion of coding about JavaScript, PHP, CGI, general web building etc.

Monday, February 29, 2016

How to repeat given HEX string

How to repeat given HEX string


Suppose that I have a 7 byte file, named test.dat. If I open test.dat with a HEX editor, the code will be

1F 2E 3D 4C 5B 6A 70

(The code has no significant meaning. Just for test!)

Now, what I want generate 7000000 byte file, it may be called milliontest.dat, by repeating this hex code 1000000 times. Batch, or bash is welcomed.

Thank you for helping me!

Answer by npocmaka for How to repeat given HEX string


@echo off    setlocal    set "hex_string=1F 2E 3D 4C 5B 6A 70"    (for /l %%# in (1;1;1000000) do (      (break|set /p=%hex_string% )   ))>>hex_file  

this will print the hex string into file 1000000 times.If you want to read a file as hex you can check certutil

Answer by Shenghua Jiang for How to repeat given HEX string


With Guxutils sed

for /L %a in (1,1,999999) do @(sed.exe -ri "s/(.{7})(.*)/\1\2\1/" "test.dat")

sed.exe -ri "s/(.{7})(.*)/\1\2\1/" "test.dat" will get the first 7 HEX in test.dat, the rest HEX and add the first 7 HEX to the end.Then replace test.dat.

But this will re-write the test.dat to the disk 999999 times.

Updated,

@echo off  setlocal enabledelayedexpansion    set "s_1="  for /L %%a in (1,1,100) do (set "s_1=!s_1!+")  for /L %%a in (1,1,3) do (    "%sed_path%\sed" -ri "s/.*/%s_1:+=&%/" "test.dat"  )  exit /b  

The Updated code can do the funtion quickly and only write to the disk 3 times.

Or @echo off setlocal enabledelayedexpansion

set "s_1="  for /L %%a in (1,1,1000) do (set "s_1=!s_1!+")  for /L %%a in (1,1,2) do (    "%sed_path%\sed" -ri "s/.*/%s_1:+=&%/" "test.dat"  )  exit /b  

This will write 2 times.

But according to the limitation of command length. It cannot run as

set "s_1="  for /L %%a in (1,1,1000000) do (set "s_1=!s_1!+")  "%sed_path%\sed" -ri "s/.*/%s_1:+=&%/" "test.dat"  exit /b  

If "test.dat" have to keep,

@echo off  setlocal enabledelayedexpansion    set "s_1="  for /L %%a in (1,1,1000) do (set "s_1=!s_1!+")  for /L %%a in (1,1,2) do (    "%sed_path%\sed" -r "s/.*/%s_1:+=&%/" "test.dat"  )>>test2.dat  exit /b  

Answer by dbenham for How to repeat given HEX string


A pure batch solution is likely to be fairly slow.

You could use my JREPL.BAT regex text processing utility to achieve a simple and speedy solution. It is pure script (hybrid JScript/batch) that runs natively on any Windows machine from XP onward. Full documentation is available from the command line via jrepl /?, or jrepl /?? for paged output.

jrepl ".*" "Array(1000000+1).join($0)" /m /j /f "test.dat" /o "millionTest.dat"  

Answer by Einstein1969 for How to repeat given HEX string


The Easy-Way

@echo off      (  For /l %%n in (1,1,1000000) do copy milliontest.dat /B + test.dat /B milliontest.dat /B  ) >nul 2>nul  

The Fast-Way

This work very fast (0.2 seconds on my old machine) and it is pure dos batch solution.

@echo off    setlocal EnableDelayedExpansion    >test.dat set /p ".=1234567" nul    set Times=1000000    (   for /l %%n in (1,1,31) do (      set /A "bit=Times %% 2, Times/=2"      If !bit! equ 1 copy milliontest.dat /B + tmp.dat /B milliontest.dat /B      if !Times! gtr 0 (          copy tmp.dat /B + tmp.dat /B tot.dat /B           del tmp.dat          ren tot.dat tmp.dat      ) >nul 2>nul   )  ) > milliontest.dat    del tmp.dat    exit /b  

As Aacini as noted, I explain this method. This method as how multiply the bytes of file test.dat for one million. This use the Bynary Multiplication.

"In base 2, long multiplication reduces to a nearly trivial operation. For each '1' bit in the multiplier, shift the multiplicand an appropriate amount and then sum the shifted values. Depending on computer processor architecture and choice of multiplier, it may be faster to code this algorithm using hardware bit shifts and adds rather than depend on multiplication instructions, when the multiplier is fixed and the number of adds required is small."

More simple way

This way reduce the number of copy command. Every 10 copy reuse the result. For achieve the result this use 6 cycles for one million as the number of zero is six.

copy test.dat /B tmp.dat >nul /B  copy tmp.dat /B milliontest.dat /B     For /l %%d in (1,1,6) do (    For /l %%n in (1,1,9) do copy milliontest.dat /B + tmp.dat /B milliontest.dat /B    copy milliontest.dat /B tmp.dat /B  ) >nul  

For doubling file I have probed to use:

type tmp.dat>>tmp.dat  

But an error occours, than I used COPY instead.

EDIT: Substitute the TYPE command with COPY Always. The TYPE work on Text File. New permonce enhacement 0.2 second on my old machine.

Answer by pjh for How to repeat given HEX string


The question is a bit unclear. I think the stuff about HEX is irrelevant, and the requirement is to copy 'test.dat' into 'milliontest.dat' one million times. A moderately efficient way to do that in Bash, with cat and xargs is:

for i in {1..1000000} ; do echo test.dat ; done | xargs cat >milliontest.dat  


Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72

0 comments:

Post a Comment

Popular Posts

Powered by Blogger.