Search code examples
batch-filedecimalascii

Converting decimal 100 to 31 30 30 in Batch


I'm trying to convert a number from a text file - lets say number.txt - from decimal 100 to ASCII 31 30 30.

Is there something I can do in batch?

I tried several things I've found on SO already - but never had the right output.

Later after I have converted that - I need to add one number up after some execution. So lets say after 109 / 31 30 39 - it should be 110 31 31 30 and not 31 30 3a for example.

Can you give me a hint?

setlocal enabledelayedexpansion
for /f "tokens=*" %%a in (nummer.txt) do (
    set line=%%a
    set a=!line:~0,1!
    set line=%%a
    set b=!line:~1,1!
    set line=%%a
    set c=!line:~2,1!
    set /a mitte=3!a!3!b!3!c!
    echo 0F0900000A00143030303030303030303030303030303030!mitte!00>com3

Solution

  • Please see if the following `Rem`arked example helps you out.
    @Echo Off
    SetLocal EnableDelayedExpansion
    Rem Undefine any existing variable named inputNum.
    Set "inputNum="
    Rem Read input number from first line of input file.
    Set /P "inputNum="<"nummer.txt"
    Rem Exit script if inputNum was not defined.
    If Not Defined inputNum GoTo :EOF
    Rem Define a number of additional iterations [number results required - 1].
    Set "#=9"
    Rem Define an increment size.
    Set "i=5"
    
    Rem Define a variable for total increase, [# x i].
    Set /A $=#*i
    Rem Undefine an existing variable named arg.
    Set "arg="
    Rem Begin looping for stated iterations.
    For /L %%G In (0,%i%,%$%)Do (
        Set /A arg=inputNum+%%G
        Rem Call subfunction passing the input number as an argument.
        Call :Sub "!arg!"
        Rem If successful,
        If Defined mitte (
            Rem Output resultant number sequence.
            Echo !mitte!
        ) Else (
            Rem Exit if no result sequence.
            GoTo EndIt
        )
    )
    
    :EndIt
    Rem Allow time to read output before ending.
    Pause
    EndLocal
    GoTo :EOF
    
    :Sub
    Rem Undefine any existing variable named mitte.
    Set "mitte="
    Rem Split each character of passed argument.
    For /F Delims^=^ EOL^= %%G In ('"Cmd /U/C Echo(%~1|Find /V """')Do (
        Rem prefix each digit of inputNum with 3.
        If Not Defined mitte (Set "mitte=3%%G")Else Set "mitte=!mitte!3%%G")
    Rem return to next command line after previous Call
    Exit /B
    

    For testing, you should only need to ensure that your input file path is correct, (line 6). I have additionally include a facility to output a specific number of additional iterations, (line 10), with a fixed increment, (line 12). Please adjust those as necessary.

    What that does if nummer.txt contains only 401, with 9 additional iterations incrementing by 5 should output:

    343031
    343036
    343131
    343136
    343231
    343236
    343331
    343336
    343431
    343436
    

    After testing, you'll probably want to replace Echo !mitte! with (Echo 0F0900000A00143030303030303030303030303030303030!mitte!00)>COM3, (line 26)