How to write these functions in VBScript?

Anything VBScript-related, including Windows Script Host, WMI, ADSI, and more.
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 12 years and 3 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
hackoo
Posts: 103
Last visit: Tue Apr 26, 2016 9:02 am

How to write these functions in VBScript?

Post by hackoo »

Hi !

I would build a HTA "ASCII to HEX to Unicode Converter Utility" Thanks to these functions written in javascript from this site

Ok This is quite simple i build into HTA with Javascript Functions and it works as a charm !

But my aim is to translate these functions written in javascript to Vbscript and here is my tries :

"ASCII to HEX to Unicode Converter Utility with Javascript"

Code: Select all

<html>
<head> 
<title>ASCII to HEX to Unicode Converter Utility</title>
<HTA:APPLICATION ICON="Explorer.exe" ID="bbceditor" APPLICATIONNAME="Converter Tool" WINDOWSTATE="maximize"> 
<link rel="stylesheet" media="screen" type="text/css" title="design_encoder" />
<script language="javascript" type="text/javascript"> 
<!-- 

function decode(){ 
  if(document.forms[0].hex.value != ''){ 
        var vText = document.forms[0].hex.value; 
        document.forms[0].ascii.value = unescape(vText); 
  } 
} 

function encode(){ 
  if(document.forms[0].ascii.value != ''){ 
     var vText = document.forms[0].ascii.value; 
     document.forms[0].hex.value = convertToHex(vText); 
         var vEncoded = convertToUnicode(vText); 
     document.forms[0].unicode.value = vEncoded; 
         document.getElementById("unicodeHTML").innerHTML = vEncoded;
     document.forms[0].ascii.focus();
    document.forms[0].ascii.blur();
    document.forms[0].ascii.select();
  } 
} 

function convertToUnicode(source) { 
  result = ''; 
  for (i=0; i<source.length; i++) 
    result += '&#' + source.charCodeAt(i) + ';'; 
  return result; 
} 

               
function convertToHex(num) { 
  var hex = ''; 
  for (i=0;i<num.length;i++) 
    hex += "%" + num.charCodeAt(i).toString(16).toUpperCase(); 
  return hex; 
} 

function convertToASCII() {
    if (document.forms[0].unicode.value != '') {
        var uniText = document.forms[0].unicode.value;
        var testText = uniText.substring(2,uniText.length-1).split(";&#")
        var resultString = ""
        for (i=0;i<testText.length;i++)
            resultString += "%" + dec2hex(testText<em>)
        document.forms[0].ascii.value = unescape(resultString);
    }
}

function dec2hex(n){
var hex = "0123456789ABCDEF";
var mask = 0xf;
var retstr = "";
    while(n != 0){
        retstr = hex.charAt(n&mask) + retstr;
        n>>>=4;
    }

 return retstr.length == 0 ? "0" : retstr;
}

</script> 
</head> 

<body> 

<center><h1>ASCII Converter</h1>
<p> 
A utility to convert ASCII characters to Hexadecimal and Unicode 
</p> 

<form name="blah" action=""> 
<br/> 

<b><font color="white">ASCII Text:</font></b><br/> 
<input type="text" name="ascii" size="75" />

<input type="button" value="Encode" onclick="encode()" />
<br/><br/><br/> 
<b><font color="white">Hex Value:</font></b><br/> 
<input type="text" name="hex" size="75" />

<input type="button" value="Decode Hex to ASCII" onclick="decode()" />
<br/><br/><br/> 
<b><font color="white">Unicode Value:</font></b><br/> 
<input type="text" name="unicode" size="75" />


<input type="button" value="Decode Unicode to ASCII" onclick="convertToASCII()" />


 
<b><font color="white">Unicode will display in HTML as : </font><font color="Red"><span id="unicodeHTML"></span></font></b> 
<br/><br/><br/> 
<input type="reset" value="Clear" onclick="document.getElementById('unicodeHTML').innerHTML = ''" />
</form></body>
</html>
and this with "ASCII to HEX to Unicode Converter Utility with Vbscript"

Code: Select all

<html>
<head> 
<title>ASCII to HEX to Unicode Converter Utility</title>
<HTA:APPLICATION ICON="Explorer.exe" ID="bbceditor" APPLICATIONNAME="Converter Tool" WINDOWSTATE="maximize"> 
<link rel="stylesheet" media="screen" type="text/css" title="design_encoder" />
<script language="Vbscript" type="text/Vbscript"> 
<!-- 

Sub decode()
Dim vText
If document.forms[0].hex.value <> "" then
 vText = document.forms[0].hex.value
 document.forms[0].ascii.value = unescape(vText) 
end if
End Sub

Sub encode() 
Dim vText,Vencoded
  If document.forms[0].ascii.value <> "" Then
   vText = document.forms[0].ascii.value 
   document.forms[0].hex.value = convertToHex(vText) 
   vEncoded = convertToUnico
	de(vText)
   document.forms[0].unicode.value = vEncoded 
   document.getElementById("unicodeHTML").innerHTML = vEncoded
     document.forms[0].ascii.focus()
    document.forms[0].ascii.blur()
    document.forms[0].ascii.select()
  End If 
End Sub 

function convertToUnicode(source)  
result = "" 
For i=0 To Unbound(source.length) 
    result = "&#" & source.charCodeAt(i) & ";" 
  return result 
Next 
End Function
               
function convertToHex(num)
 hex = ""
 For (i=0 To Unbound(num.length) 
  hex = "%" & num.charCodeAt(i).toString(16).toUpperCase() 
  return hex 
 Next 
End Function


function convertToASCII()
    if document.forms[0].unicode.value <> "" Then
        uniText = document.forms[0].unicode.value
        testText = uniText.substring(2,uniText.length-1).split(";&#")
        resultString = ""
        for i=0 To unbound(testText.length
        resultString = "%" & dec2hex(testText<em>)
        document.forms[0].ascii.value = unescape(resultString)
        Next
    End If
End Function    
    

function dec2hex(n)
hex = "0123456789ABCDEF"
mask = 0xf
retstr = ""
while(n <> 0) do
        retstr = hex.charAt(n&mask) & retstr
        n>>>=4
loop
return retstr.length = 0 ? "0" : retstr
End Function

</script> 
</head> 

<body> 

<center><h1>ASCII Converter</h1>
<p> 
A utility to convert ASCII characters to Hexadecimal and Unicode 
</p> 

<form name="blah" action=""> 
<br/> 

<b><font color="white">ASCII Text:</font></b><br/> 
<input type="text" name="ascii" size="75" />

<input type="button" value="Encode" onclick="encode()" />
<br/><br/><br/> 
<b><font color="white">Hex Value:</font></b><br/> 
<input type="text" name="hex" size="75" />

<input type="button" value="Decode Hex to ASCII" onclick="decode()" />
<br/><br/><br/> 
<b><font color="white">Unicode Value:</font></b><br/> 
<input type="text" name="unicode" size="75" />


<input type="button" value="Decode Unicode to ASCII" onclick="convertToASCII()" />


 
<b><font color="white">Unicode will display in HTML as : </font><font color="Red"><span id="unicodeHTML"></span></font></b> 
<br/><br/><br/> 
<input type="reset" value="Clear" onclick="document.getElementById('unicodeHTML').innerHTML = ''" />
</form></body>
</html>
But i'm a little stuck to translate it in vbscript; so if anyone here can correct me and help me to do it correctly i would be grateful !

Thank you for any help !
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

How to write these functions in VBScript?

Post by jvierra »

I would sugget getting a good book on JavaScript then a good book on VBScript. Once ytou have mastered both you will be able to do this.

There are many things you do not undestand about both languages. They are not very similar syntactically.

One example is that arrays are very different. Second, strings are very different.

This is a good excercise for learning how to script.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

How to write these functions in VBScript?

Post by jvierra »

Start with a book. There are many hundreds that have been written.

For basic scripting you want the WSH flavor of JavaScript.

Here is a list:http://www.amazon.com/s/ref=nb_sb_noss? ... +beginners

This topic is 12 years and 3 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked