-1

I am trying to make a decoder from a batch file. It takes my input and turns it into an output by looking at each character. Here is what I have.

@Echo Off
:StartUpConfiguration
Cls
Color 0A
Mode Con Cols=60 Lines=35
Title Decoder

:Input
Set /P Input=[Enter Input Here]

At this point I want it to read the first three characters which will either be "(0)" or "(1)" and then from there make the fourth character set 0's or 1's corresponding to the first three characters. Ex) If my input is "(1)3241", i want my output to be "1110011110" which makes the first numbers 1's from the "(1), then making three of that number, then alternate to a zero and make two 0's and so forth. The pattern tells it what number to start with (1 or 0), and then create how ever many of that number corresponding to the next number in the input.

I have seen tokens used to pull a certain area from a string of text.

Sam Hosseini
  • 813
  • 2
  • 9
  • 17
  • How many digits might the string of numbers be to decode into 0's and 1's? What happens if there needs to be 10+ 1's or 0's? How is the *greater than 9* represented in the input number string? And, what do you want to do with the generated 1's and 0's? Do you want to display them on the screen, save them to a text file, etc.? These are just some of the questions that come to mind. It will also help for you to finish fleshing out your batch file and ask specific questions about problem areas instead of hoping that someone will write all the code for you... – James L. Dec 12 '13 at 18:03
  • You can use the `for` command to separate the input string into two parts, breaking on the `)` character (`for "tokens=1,2* delims=)" %%i in (%Input%) do`). And you can use the `set somevar=%var:~#,#%` to extract characters from a string. Combining the `for` and `set` commands together is a little tricky, but not impossible. It would better to eliminate the need to separate the `(1)` from the remaining digits by asking two questions in the batch-file. Then it becomes simple to generate the 1's and 0's string. – James L. Dec 12 '13 at 18:10
  • It binary and I have never seen 10+ 0s or ones. – TheMountainFurnaceGabriel Dec 13 '13 at 03:47
  • Any binary file with nulls (`00h`) is likely to have two nulls in a row (`00h` `00h`), which would be sixteen 0's as bits. I looked at EXEs, DLLs, and many image types (bmp, jpg, png, ico, wmf, emf, psd, gif, tif). All of these have long sequences of nulls. I think the only way you could have a max of nine 0 bits is when the binary data only represents *text* data (e.g., ASCII characters and the like). – James L. Dec 13 '13 at 18:21
  • I know, it is a simple decoder for some puzzles. It doesn't include anything but text. – TheMountainFurnaceGabriel Dec 15 '13 at 04:06

1 Answers1

3
@echo off
setlocal EnableDelayedExpansion

Set /P Input=[Enter Input Here]

for /F "tokens=1,2 delims=()" %%a in ("%input%") do (
   set bit=%%a
   set "output="
   for /F "delims=" %%c in ('cmd /D /U /C echo %%b^| find /V ""') do (
      for /L %%i in (1,1,%%c) do set "output=!output!!bit!"
      set /A "bit=^!bit"
   )
)
echo %output%
Aacini
  • 65,180
  • 12
  • 72
  • 108
  • +1 for using `cmd /u | find` to separate each digit onto a separate line for processing. – James L. Dec 12 '13 at 20:04
  • yes, nice++! This does not work with `|findstr /v ^^$` or `|findstr /v "[]"`. – Endoro Dec 12 '13 at 22:46
  • Just one thing : a traditional loop to split the string is more than 5 times quicker. But very good code +1 – SachaDee Dec 12 '13 at 23:02
  • @sachadee: Excuse me. How a traditional loop may be used to split the string in individual characters? Do you refer to a loop assembled with `goto`? – Aacini Dec 13 '13 at 03:02
  • Sorry for any trouble, but if you could, is there a way to reverse the process? – TheMountainFurnaceGabriel Dec 13 '13 at 04:41
  • Yes Aacini, a loop assembled with goto. – SachaDee Dec 13 '13 at 10:01
  • @sachadee: Then your timing is wrong: I modified this program to use a fixed string 70 digits long and repeat the process 250 times; it took 23 seconds. Then, I modified the program to split the string this way: take the first character, eliminate it from the string and repeat the cycle via a `goto`; it took 81 seconds... – Aacini Dec 14 '13 at 05:22
  • OK @Aacini take a look at this test: I modified your code with a normal ´goto:loop´ and make a test on 1000 iteration. Result : 20 seconds for `goto:loop` method and 69 seconds for the `cmd /u` Method. Here is the testing code. [link](http://paste.ubuntu.com/6572347/) the `goto:loop` is 3 times faster (not 5). – SachaDee Dec 14 '13 at 13:50