6

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

Aacini
  • 59,374
  • 12
  • 63
  • 94
  • Interesting, then redirect to file both vbs/js print 128 bytes and fail here with the same error. +1 for this question. – seeker Apr 13 '13 at 18:44

2 Answers2

6

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.

0

Regarding what @OpenTechnologist said, how about you using ChrW() instead of Chr() in the VBScript as that will come closer to the essence of fromCharCode(), i.e. outputting Unicode.

bugmagnet
  • 7,233
  • 7
  • 57
  • 121
  • I see your point. However, I am interested in the _opposite_ question, that is: how to _directly_ generate Ascii characters in JScript? I solved this problem already adjusting the generated codes with a factor that is variable for each character beyond 127... – Aacini Apr 13 '13 at 13:35