473,397 Members | 2,116 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

How do I convert this VBScript to Javascript?

Below is some VB Script - I can't figure out how to convert it to javascript. Below that, I've put my best attempt at as far as I got - can anyone help me out correcting and completing it?

Thanks,
Iain

The VB Script:
Expand|Select|Wrap|Line Numbers
  1. const BASE_64_MAP_INIT ="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
  2. dim arrBase64EncMap(63)
  3. dim arrBase64DecMap(127)
  4. strNewLine = "<P>" & vbCRLF
  5. for iLoop = 0 to len(BASE_64_MAP_INIT) - 1
  6.     arrBase64EncMap(iLoop) = mid(BASE_64_MAP_INIT, iLoop + 1, 1)
  7. next
  8. for iLoop = 0 to len(BASE_64_MAP_INIT) - 1
  9.     arrBase64DecMap(ASC(arrBase64EncMap(iLoop))) = iLoop
  10. next
  11.  
  12. public function base64Encode(strPlain)
  13.     if len(strPlain) = 0 then
  14.         base64Encode = ""
  15.         exit function
  16.     end if
  17.  
  18.     '** Work out rounded down multiple of 3 bytes length for the unencoded text **
  19.     iBy3 = (len(strPlain) \ 3) * 3
  20.     strReturn=""
  21.  
  22.     '** For each 3x8 byte chars, covert them to 4x6 byte representations in the Base64 map **
  23.     iIndex = 1
  24.     do while iIndex <= iBy3
  25.         iFirst  = asc(mid(strPlain, iIndex+0, 1))
  26.         iSecond = asc(mid(strPlain, iIndex+1, 1))
  27.         iiThird  = asc(mid(strPlain, iIndex+2, 1))
  28.         strReturn = strReturn & arrBase64EncMap((iFirst \ 4) AND 63 )
  29.         strReturn = strReturn & arrBase64EncMap(((iFirst * 16) AND 48) + ((iSecond \ 16) AND 15 ) )
  30.         strReturn = strReturn & arrBase64EncMap(((iSecond * 4) AND 60) + ((iiThird \ 64) AND 3 ) )
  31.         strReturn = strReturn & arrBase64EncMap(iiThird AND 63)
  32.         iIndex = iIndex + 3
  33.     loop
  34.  
  35.     '** Handle any trailing characters not in groups of 3 **
  36.     '** Extend to multiple of 3 characters using = signs as per RFC **
  37.     if iBy3 < len(plain) then
  38.         iFirst  = asc(mid(strPlain, iIndex+0, 1))
  39.         strReturn = strReturn & arrBase64EncMap((iFirst \ 4) AND 63 )
  40.         if (len(strPlain) MOD 3 ) = 2 then
  41.             iSecond = asc(mid(strPlain, iIndex+1, 1))
  42.             strReturn = strReturn & arrBase64EncMap(((iFirst * 16) AND 48) + ((iSecond \ 16) AND 15 ) )
  43.             strReturn = strReturn & arrBase64EncMap((iSecond * 4) AND 60)
  44.         else
  45.             strReturn = strReturn & arrBase64EncMap((iFirst * 16) AND 48)
  46.             strReturn = strReturn & "="
  47.         end if
  48.         strReturn = strReturn & "="
  49.     end if
  50.  
  51.     '** Return the encoded result string **
  52.     base64Encode = strReturn
  53. end function
  54.  
My attempt at javascript:
Expand|Select|Wrap|Line Numbers
  1. var BASE_64_MAP_INIT ="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
  2. var arrBase64EncMap = new Array(63);
  3. var arrBase64DecMap = new Array(127);
  4.  
  5. for (var i = 0; i < BASE_64_MAP_INIT.length; i++) {
  6.     arrBase64EncMap[i] = BASE_64_MAP_INIT.charAt(i);
  7. }
  8. for (var i = 0; i < BASE_64_MAP_INIT.length; i++) {
  9.     arrBase64DecMap[ASC(arrBase64EncMap[i]] = i
  10.     arrBase64DecMap[arrBase64EncMap[i].charCodeAt(0)] = i
  11. }
  12.  
  13. function base64Encode(strPlain) {
  14.     if (strPlain.length == 0) {
  15.         return "";
  16.     }
  17.  
  18.     // Work out rounded down multiple of 3 bytes length for the unencoded text
  19.     iBy3 = strPlain.length - (strPlain.length % 3);
  20.     var strReturn = "";
  21.  
  22.     // For each 3x8 byte chars, covert them to 4x6 byte representations in the Base64 map
  23.     var iIndex = 1;
  24.     while (iIndex <= iBy3) {
  25.         var iFirst  = strPlain.charCodeAt(iIndex+0);
  26.         var iSecond = strPlain.charCodeAt(iIndex+1);
  27.         var iiThird = strPlain.charCodeAt(iIndex+2);
  28.  
  29.         //**** i'm totall lost here - what's the AND doing, and how do I do it in JS?   ****//
  30.         var strReturn += arrBase64EncMap((iFirst - (iFirst % 4) AND 63);        
  31.  
Oct 3 '07 #1
1 2298
pbmods
5,821 Expert 4TB
Heya, iporter. Welcome to TSDN!

Are you getting an error? Is the code not doing what you want it to?
Oct 4 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: John Davis | last post by:
When I create new documents in Dreamweaver, there are several choices for ASP creation: ASP JavaScript: run at client side?? ASP VBScript: run at server side?? ASP.NET C# ASP.NET VB I don't...
29
by: Christopher Brandsdal | last post by:
If I have a .ASP page that runs JScript code - is it possible to include an ..ASP page that runs VBscript???
5
by: Bill | last post by:
I need to convert a variable, nNum, into a two-character string. nNum is always less than 100. If nNum is 0, the string needs to be "00", if it's 1, it needs to be "01", if it's 34, it needs to...
2
by: davidgordon | last post by:
Hi, I have some pages with this VBScript code, which obviously does not work in Firefox. How can I convert this to Javascript in order for my web page to work in Firefox ? It basically fills a...
1
by: darcykahle | last post by:
Is there any way of converting an existing access form into html format? The form in question has built-in visual-basic code, which gets commented out when you use the "save as" method, instead of...
10
by: thinktwice | last post by:
in my script file , i need call a method of a atl com module(implemented in vc++), which returan an safearray. i don't know how to convert it into array in jscript. i have tried serveral ways to...
3
by: rishabhshrivastava | last post by:
Hello All, How can I convert a value to Double in JavaScript??? In vbscript i believe its done as cDbl(Value) I tried lots of way but getting a value of "NaN". Any suggestions/ideas will...
28
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - How do I convert a Number into a String with exactly 2 decimal places?...
1
by: sathyan8294 | last post by:
what is websites for convert javascript to vbscript?
2
by: murugavelmsc | last post by:
Hi, i have a javascript code. I want to convert into VBscript. pls help me Javascript Code:
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.