none
VBScript - Convert all array elements from ASCII decimal value to character value RRS feed

  • Question

  • What is the most efficient method to convert all elements in an array, stored as ASCII decimal, to a character value?

    dim str, arr()
    
    str = inputbox("Please enter character string for encryption","Encryption")
    
    y=(len(str))
    
    for i=1 to y
    	redim preserve arr(i+1)
    	arr(i-1) = asc(mid(str,i,1)) - (i-1)
    next
    
    wscript.echo(join(arr))

    The code as is displays a string of ASCII values on the wscript.echo call.

    Saturday, February 28, 2015 11:37 PM

Answers

  • I think you mean byte array:

    Try this:

    arrayOfBytes=Array(65,66,67,68,69,70)
    For i = 0 To Ubound(arrayOfBytes) -1 
        S = S & Chr(arrayOfBytes(i))
    Next
    WScript.Echo S
    

    Even easier wit PowerShell:

    [System.Text.Encoding]::ASCII.GetString($bytes)


    ¯\_(ツ)_/¯

    • Marked as answer by F_Gordon Sunday, March 1, 2015 7:50 AM
    Sunday, March 1, 2015 1:05 AM

All replies

  • I think you mean byte array:

    Try this:

    arrayOfBytes=Array(65,66,67,68,69,70)
    For i = 0 To Ubound(arrayOfBytes) -1 
        S = S & Chr(arrayOfBytes(i))
    Next
    WScript.Echo S
    

    Even easier wit PowerShell:

    [System.Text.Encoding]::ASCII.GetString($bytes)


    ¯\_(ツ)_/¯

    • Marked as answer by F_Gordon Sunday, March 1, 2015 7:50 AM
    Sunday, March 1, 2015 1:05 AM
  • OR - Are you askinghow to convert characters into numbers such as how to UUEncode a string?

    ¯\_(ツ)_/¯

    Sunday, March 1, 2015 1:06 AM
  • Okay, so I've developed this further, I have added this (rem exclusions while testing:

    x=join(arr) for each z in split(x, " ") rem n=chr(z) wscript.echo chr(z) next

    rem wscript.echo join(n)


    It sort of works, but I get one too many dialog boxes, and a Type mismatch: '[string:""]'


    • Edited by F_Gordon Sunday, March 1, 2015 1:10 AM
    Sunday, March 1, 2015 1:09 AM
  • Ok.  I think I see.  You are just playing around trying to learn.

    Try this:

    str = inputbox("Please enter character string for encryption","Encryption")
    For i=1 To Len(str)
        Wscript.Echo Asc(Mid(str,i,1))
    Next

    At a prompt as an elevated admin type this:

    WScript //H:cscript

    This will get rid of the popup boxes and output to the console.  If you want a popup use MsgBox <string>

    http://ss64.com/vb/msgbox.html


    ¯\_(ツ)_/¯


    • Edited by jrv Sunday, March 1, 2015 1:24 AM
    • Marked as answer by F_Gordon Sunday, March 1, 2015 2:02 AM
    • Unmarked as answer by F_Gordon Sunday, March 1, 2015 7:50 AM
    Sunday, March 1, 2015 1:21 AM
  • Hello jrv... sorry I've just seen your replies, will get back to you. I think your first reply has my answer in it...


    • Edited by F_Gordon Sunday, March 1, 2015 1:22 AM
    Sunday, March 1, 2015 1:22 AM
  • Thanks jrv
    Sunday, March 1, 2015 2:02 AM
  • Enjoy!

    ¯\_(ツ)_/¯

    Sunday, March 1, 2015 3:05 AM
  • If you're interested jrv, I've rewritten my code, taking into consideration some pointers, raised by a rather critical (dare I say overly pedantic), and acerbic gentleman I've encountered before on the stackoverflow forums:
     http://stackoverflow.com/questions/28785848/vbscript-pass-for-loop-iterations-into-dynamic-array/28800771#28800771 He is obviously well versed in vbscript writing, much like I gather you are, so I heeded his advice (so as to avoid bad practice). The finished code is below. I was having issues with the original code, in my original post. When it came to reversing the entire code for decryption purposes, I kept getting a random character appended to the output of the decrypted string, which was passed on from the array. Anyway here is the reworked code, what do you think?

    dim str, arr, arr2, result, ls, i, encstr, decstr
    
    do while len(str)=0
    str = inputbox("Please enter character string for encryption","Cipher")
    if isempty(str) then
    wscript.quit()
    end if
    loop
    
    ls=(len(str))
    redim arr(ls -1)
    for i=1 to ls
    arr(i-1) = chr(asc(mid(str,i,1)) - (i-1))
    next
    encstr = join(arr,"")
    rem wscript.echo encstr
    
    ls=len(encstr)
    redim arr2(ls-1)
    for i=1 to ls
    arr2(i-1) = chr(asc(mid(encstr,i,1)) + (i-1))
    next
    decstr=join(arr2,"")
    rem wscript.echo decstr
    
    result=msgbox("Encrypted string: " & encstr & vbcrlf & "Decrypted string: " & decstr,0,"Ciphe")

    I'm working on a slightly more sophisticated cipher, to learn other methods of vbscript, and cement the ones I've learnt so far... Thanks for your time... :D


    • Edited by F_Gordon Monday, March 2, 2015 12:22 AM
    Monday, March 2, 2015 12:17 AM