Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why WScript.StdOut.Write method behaves differently in VBS vs. JScript?

I have two small programs in VBS and JScript:

VBScript.vbs:

For i=0 To 255
   WScript.StdOut.Write Chr(i)
Next

JScript.js:

for ( var i=0; i <= 255; ++i )
   WScript.StdOut.Write(String.fromCharCode(i));

When I execute they in the command-prompt, they show different results:

C:>cscript /nologo VBScript.vbs
 ☺☻♥♦♣
♫☼►◄↕‼¶§▬↨↑↓→←∟↔▲▼ !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]
^_`abcdefghijklmnopqrstuvwxyz{|}~⌂??'ƒ".┼╬^%S<O?Z??''""--~Ts>o?zY ¡¢£¤¥¦§¨©ª«¬­®
¯°±²³´µ¶·¸¹º»¼½¾¿ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖ×ØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõö÷øùúûüýþ
ÿ

C:>cscript /nologo JScript.js
 ☺☻♥♦♣
♫☼►◄↕‼¶§▬↨↑↓→←∟↔▲▼ !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]
^_`abcdefghijklmnopqrstuvwxyz{|}~⌂???????????????????????????????? ¡¢£¤¥¦§¨©ª«¬­
®¯°±²³´µ¶·¸¹º»¼½¾¿ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖ×ØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõö÷øùúûüý
þÿ

Previous output may be explained because some operative difference in JScript's fromCharCode method vs. VBScript's Chr function. However, if a try to redirect the output to a disk file, the result is very different:

C:>cscript /nologo VBScript.vbs > VBScript.txt

C:>cscript /nologo JScript.js > JScript.txt
C:\JScript.js(2, 4) Microsoft JScript runtime error: Invalid procedure call or argument

C:>dir *.txt

15/01/2013  05:48 p.m.               128 JScript.txt
15/01/2013  05:48 p.m.               256 VBScript.txt

In this case, both programs use the same WSH method under the same conditions, so I don't understand why VBS correctly generate the file with 256 characters, but JScript issue an error and just generate the first 128 characters.

What happens here? Is there a way to correctly generate the same file with 256 different characters in JScript? TIA

Antonio

like image 256
Aacini Avatar asked Jan 16 '13 00:01

Aacini


1 Answers

first,

the logic of your codes are not the same.

the counting on your VBScript.vbs starts at ZERO(0) while your JScript.js starts at ONE(1).

change the increment operator in your JScript to make them the same:

for ( var i=0; i <= 255; i++ )
    WScript.StdOut.Write(String.fromCharCode(i));

second,

the functions you used are not the same because they both return different result on a per character context.

the Chr() function returns ASCII characters,

while the fromCharCode() function returns Unicode characters that may have 1 to 4 bytes per character.

therefore, the Å character from the ASCII set is different from the Å character from the Unicode set.

like image 125
Open Technologist Avatar answered Oct 10 '22 22:10

Open Technologist