[Batch] Opinions Wanted

Discussion in 'Scripting' started by Phazor, Dec 21, 2010.

  1. Phazor

    Phazor MDL Expert

    Sep 1, 2009
    1,145
    517
    60
    #1 Phazor, Dec 21, 2010
    Last edited by a moderator: Apr 20, 2017
    This is my attempt at a random hex generator.

    It generates the hex values of chars between 0 and z.

    Code:
    @echo off
    
    :rndhexgen
    setlocal enabledelayedexpansion
    :rndval
    set chars=0123456789abcdefghijklmnopqrstuvwxyz
    set count=0
    set /a count+=1
    set rnd=%random%
    set /a rnd=rnd%%35
    set rval=!chars:~%rnd%,1!
    if !rval!==0 set pval=30
    if !rval!==1 set pval=31
    if !rval!==2 set pval=32
    if !rval!==3 set pval=33
    if !rval!==4 set pval=34
    if !rval!==5 set pval=35
    if !rval!==6 set pval=36
    if !rval!==7 set pval=37
    if !rval!==8 set pval=38
    if !rval!==9 set pval=39
    if !rval!==a set pval=61
    if !rval!==b set pval=62
    if !rval!==c set pval=63
    if !rval!==d set pval=64
    if !rval!==e set pval=65
    if !rval!==f set pval=66
    if !rval!==g set pval=67
    if !rval!==h set pval=68
    if !rval!==i set pval=69
    if !rval!==j set pval=6a
    if !rval!==k set pval=6b
    if !rval!==l set pval=6c
    if !rval!==m set pval=6d
    if !rval!==n set pval=6e
    if !rval!==o set pval=6f
    if !rval!==p set pval=70
    if !rval!==q set pval=71
    if !rval!==r set pval=72
    if !rval!==s set pval=73
    if !rval!==t set pval=74
    if !rval!==u set pval=75
    if !rval!==v set pval=76
    if !rval!==w set pval=77
    if !rval!==x set pval=78
    if !rval!==y set pval=79
    if !rval!==z set pval=7a
    echo.
    echo The generated char is !rval!
    echo.
    echo The corresponding hex value is %pval%
    echo.
    endlocal
    pause >nul
    goto rndhexgen
    
    Can anyone think of a way to make that shorter?
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  2. Calistoga

    Calistoga MDL Senior Member

    Jul 25, 2009
    420
    198
    10
    #2 Calistoga, Jan 15, 2011
    Last edited by a moderator: Apr 20, 2017
    I've been looking for a way to shorten your code, but I don't think it is possible without using an external executable. I think someone managed to do it through debug.exe by writing temporary files etc. I don't think that's "a clean approach" though.

    You could write a helper executable to carry out that specific task, for example in AutoIt you could use the function _StringToHex() in the String.au3 library. Or you could migrate your whole project to AutoIt! :D

    Anyways, I wrote a short script in AutoIt that utilizes the function mentioned above. It can process all characters and string lengths. Download compiled version (/? at the command prompt for usage). It prints the hex value of any string to the command prompt.

    If someone got a better solution on how to do this in pure batch, I'd love to see it myself :)

    In AutoIt:
    Code:
    #Include <String.au3>
    
    ConsoleWrite(_StringToHex($CmdLineRaw))
     
  3. Boerge

    Boerge MDL Novice

    Jul 11, 2013
    2
    0
    0
    @echo off


    :rndhexgen
    setlocal enabledelayedexpansion
    :rndval
    set chars=abcefghijklmnopqrstuvwxyz & set numbers=0123456789
    set count=0
    set /a count+=1
    set rnd=%random%
    set /a rnd=rnd%%35
    set rval=!chars:~%rnd%,1!
    set pval=!numbers:~%rnd%,1!
    echo.
    echo The generated char is !rval!
    echo.
    echo The corresponding hex value is !pval!
    echo.
    endlocal
    pause >nul
    goto rndhexgen

    As small as the way i think you'd like it :)
     
  4. Humphrey

    Humphrey MDL Expert

    Dec 13, 2011
    1,458
    982
    60
    #4 Humphrey, Jul 11, 2013
    Last edited by a moderator: Apr 20, 2017
    I get mixed results.


    Code:
    
    The generated char is u
    The corresponding hex value is
    
    The generated char is w
    The corresponding hex value is
    
    The generated char is
    The corresponding hex value is
    
    The generated char is
    The corresponding hex value is
    
    The generated char is
    The corresponding hex value is
    
    The generated char is a
    The corresponding hex value is 0
    
    The generated char is t
    The corresponding hex value is
    
    The generated char is
    The corresponding hex value is
    
    The generated char is k
    The corresponding hex value is 9
    
    The generated char is
    The corresponding hex value is
    
    The generated char is o
    The corresponding hex value is
    
    The generated char is y
    The corresponding hex value is
    
    The generated char is
    The corresponding hex value is
    
    The generated char is z
    The corresponding hex value is
    
    The generated char is c
    The corresponding hex value is 2
    
    The generated char is q
    The corresponding hex value is
    
    The generated char is i
    The corresponding hex value is 7
    
    The generated char is
    The corresponding hex value is
    
    The generated char is i
    The corresponding hex value is 7
    
    The generated char is v
    The corresponding hex value is
    
    The generated char is
    The corresponding hex value is
    
    The generated char is v
    The corresponding hex value is
    
    The generated char is i
    The corresponding hex value is 7
    
    The generated char is j
    The corresponding hex value is 8
    
    The generated char is n
    The corresponding hex value is
    
    The generated char is i
    The corresponding hex value is 7
    
    The generated char is t
    The corresponding hex value is
    
    The generated char is h
    The corresponding hex value is 6
    
    The generated char is t
    The corresponding hex value is
    
    
    
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  5. Boerge

    Boerge MDL Novice

    Jul 11, 2013
    2
    0
    0
    I think it's because it generates a number that controles witch to choose etc
    1. a
    2. b
    3. c
    ...

    and when it generates a number that is more than there is of characters its blank.
    The same with numbers.
    So i made more characters and numbers...

    code:
    @echo off




    :rndhexgen
    setlocal enabledelayedexpansion
    :rndval
    set chars=abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz & set numbers=0123456789012345678901234567890123456789
    set count=0
    set /a count+=1
    set rnd=%random%
    set /a rnd=rnd%%35
    set rval=!chars:~%rnd%,1!
    set pval=!numbers:~%rnd%,1!
    echo.
    echo The generated char is !rval!
    echo.
    echo The corresponding hex value is !pval!
    echo.
    endlocal
    pause >nul
    goto rndhexgen