Solved

Help converting a line of VBScript to Javascript,.

Posted on 2001-06-08
13
1,920 Views
Last Modified: 2007-12-19
I am having a problem converting a line of VBscript to Javscript.  This is one line of code from a program which reads image dimensions from gif and jpeg files.

The original VBscript: (where strTemp always has length 2)
 foo = clng(asc(right(strTemp, 1)) + (asc(left(strTemp, 1)) * 256))

This Javascript yields incorrect results:
 return strTemp.charCodeAt(1) + (strTemp.charCodeAt(0) * 256);

Interestingly THIS VBScript (which uses ascW rather than asc) yields the EXACT SAME incorrect result as the Javascript version above!:
 foo = clng(ascW(right(strTemp, 1)) + (ascW(left(strTemp, 1)) * 256))


Both the ascW version of the VBscript and the Javascript version give the proper result with one set of test files and conversely both give the same incorrect result with other files.

Since asc/ascW give different results, and charCodeAt()/ascW give the same incorrect results, I am assuming that this is a Unicode/Ansii issue, but that's as far as I can get.  

Can someone tell me how to properly convert the original line of VBscript to Javascript?  Thanks

emichael
0
Comment
Question by:EMichael
  • 6
  • 3
  • 2
  • +2
13 Comments
 
LVL 5

Expert Comment

by:djbusychild
ID: 6169985
could you post what strTemp is ?
0
 
LVL 4

Expert Comment

by:ykf2000
ID: 6169988
try this:


return strTemp.charCodeAt(1) + (parseInt(strTemp.charCodeAt(0)) * 256);
0
 

Author Comment

by:EMichael
ID: 6170913
to djbusychild: strTemp is 2 bytes long and is the height or width of the gif or jpg.  Or are you asking for something more?

to vkf2000: I tried your suggestion but it made no difference.

Any more thoughts?  thanks.

emichael
0
 
LVL 9

Expert Comment

by:TTom
ID: 6171308
On the JavaScript side, the analogy to clng seems to be ceil.

I would try:

ceil(strTemp.charCodeAt(1) + (strTemp.charCodeAt(0) * 256));

or

Math.ceil(strTemp.charCodeAt(1) + (strTemp.charCodeAt(0) * 256));

Have you determined (by comparing them) that the elements, i.e., strTemp.charCodeAt(1) = asc(right(strTemp, 1)) and strTemp.charCodeAt(0) * 256 = asc(left(strTemp, 1)) * 256))

This is definitely an ANSI vs. Unicode problem (JS uses Unicode for strings and the VBS functions return ANSI values.

More help may be available from:

http://developer.netscape.com/docs/manuals/communicator/jsref/js13.html#Unicode

which discusses the Unicode issue.

Tom
0
 

Author Comment

by:EMichael
ID: 6171337
TTom-
Thanks for your thoughtful analysis and help.  Unfortunately Math.ceil ( and the cLNG conversion ) do not seem to be the issue.  

I have spent considerable time trying to determine that I am indeed feeding the same values to these functions, and as best I can tell I am. The glimmer of uncertainty resides in the fact that I am just using a ASP and a browser via response.write to view the results and there are so many non-printable characters that it is very hard.  As I received your Comment I was just installing a hex editor to see if that would help at all.

I also have tested and found that the problem is not with clng but is at the asc vs charCodeAt by noting the results just one byte at a time.  Also, as I noted earlier, that ascW gives the EXACT same incorrect values in the EXACT same files as charCodeAt is another strong indicator that I am indeed feeding in the same values in each language.  And also that it is a Unicode vs Ansii issue (as you said).

I have reviewed the link you sent, and altho enlightening me on Unicode, did not help here.

Your further help would be very much appreciated.  THAnks for your efforts so far.

emichael
0
 

Author Comment

by:EMichael
ID: 6171349
Having now examined a gif file with a hex editor I am satisfied that the VBScript and Javascript functions are indeed being fed the same strTemp's.  

My gut tells me the answer lies hidden somewhere in the observation that ascW and charCodeAt each give identical errors.

emichael
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:EMichael
ID: 6171534
Just looked at a Unicode table.  Take a look at http://www.unicode.org/Public/MAPPINGS/VENDORS/MICSFT/WINDOWS/CP936.TXT

My hex editor displays one of the problematic bytes as 96 and translates that to ansii "-" .

In the Unicode table it says 0x96 is "#DBCS LEAD BYTE" rather than a real chraracter ???  Is this the issue - asc reads it as "-" (how?) and ascW and charCodeAt read it as "#DBCS LEAD BYTE" and so mess it up ?  

If so, then how to I deal with this?  How do the hex editor and asc know that 0x96 is a "-" ?

emichael
0
 
LVL 25

Expert Comment

by:clockwatcher
ID: 6171537
The answers lies hidden within MSDN.  The ASC function actually maps unicode characters to their closest ASCII equivalent.  See the following:

  http://support.microsoft.com/support/kb/articles/Q145/7/45.asp

The following unicode character Ā (a capital letter A with a bar over it, in the Latin-Extended A set-- http://www.hclrss.demon.co.uk/unicode/latin_extended_a.html) is mapped by the VB ASC function to 65-- a plain capital A.  If you wanted to do the same thing in javascript, you'd have to build a mapping table and remap the characters yourself.  To display the mapping table:


<script language=vbscript>

function vbASC(strTemp)
  vbASC = asc(strTemp)
end function

function vbCHR(val)
  vbCHR = chr(val)
end function

function vbASCW(strTemp)
  vbASCW = ascW(strTemp)
end function

</script>

<script language=javascript>

function jCharAt(strTemp)
{
  return strTemp.charCodeAt(0);
}
</script>
<html>
<body>
<table border=1 cellspacing=2>
<tr><td>Unicode value</td><td>Character</td><td>VB ASC val</td><td>ASC Map</td></tr>

<script language="javascript">

  var c,a;

  for (var i=256; i < 4096; i++) {
    c = String.fromCharCode(i);
    document.write("<tr>");
    document.write("<td>" + i + "</td>");
    document.write("<td>" + c + "</td>");
    document.write("<td>" + (a = vbASC(c)) + "</td>");
    document.write("<td>" + vbCHR(a) + "</td>");
    document.write("</tr>");
  }

</script>
</table>
</body>
</html>
0
 
LVL 25

Accepted Solution

by:
clockwatcher earned 200 total points
ID: 6171645
It seems odd that you're getting the correct numbers with Asc (since it's doing translation).  I think you'd be getting the correct numbers with javascript and AscW which aren't doing any translation.  It almost sounds like you're retrieving the data in the file with a FileSystemObject TextStream.  TextStream performs conversion before you even see the data; if you're using a TextStream are you opening your file as unicode?

I believe the following demonstrates the problem.  Does it appear to be what is going on?

<script language=vbscript>

function vbfoo(strTemp)
  vbfoo = clng(asc(right(strTemp, 1)) + (ascW(left(strTemp, 1)) * 256))
end function

function vbfoo1(strTemp)
  vbfoo1 = clng(ascW(right(strTemp, 1)) + (ascW(left(strTemp, 1)) * 256))
end function

</script>

<script language="javascript">

// Map Unicode to closest ASCII equivalent
var translationmap=Array();
translationmap[256] = 65;  // capital 'A'
translationmap[257] = 97;  // lowercase 'a'
// ... and on and on and on...

function jsfoo(strTemp) {
  var c0 = strTemp.charCodeAt(0);
  var c1 = strTemp.charCodeAt(1);
  return (c0 << 8) + c1;
}

function jsfoo1(strTemp) {
  var c = Array();
  c0 = strTemp.charCodeAt(0);
  c1 = strTemp.charCodeAt(1);
  if (translationmap[c1]) c1 = translationmap[c1];
  return (c0 << 8) + c1;
}
</script>

<html>
<body>
<script language="javascript">
  var str = "\u0101\u0101";
  document.write("VB ASC: " + vbfoo(str) + "<BR>");
  document.write("VB ASCW: " + vbfoo1(str) + "<BR>");
  document.write("JS No Map: " + jsfoo(str) + "<BR>");
  document.write("JS w/map: " + jsfoo1(str) + "<BR>");
</script>
</body>
</html>

BTW, if you're using ASP, I'm not sure why you don't use a vbscript function for that particular purpose anyway.  Just mix scripting languages.

E.g.,

<% @language="javascript" %>

<script language="vbscript" runat="server">
function vbfoo(strTemp)
  vbfoo = clng(asc(right(strTemp, 1)) + (ascW(left(strTemp, 1)) * 256))
end function
</script>

<html>
<body>
<%
  var c = "\u0100\u0100";
  Response.write(vbfoo(c));
%>
</body>
</html>
0
 
LVL 9

Expert Comment

by:TTom
ID: 6171685
According to my research (and the link I posted above), javascript deals with charCodeAt in Unicode terms.  VBS asc is returning the ANSI value.

That is the crux of your problem, and, if clockwatcher has provided a method for returning the ANSI value using JavaScript, that will be your solution, since that's what you apparently need to do.

If you are using this as part of an ASP script to write the contents of your page, VBScript on the server side is probably the way to go.

If you need to do it on the client side, you have a problem.

Sorry!

Tom
0
 

Author Comment

by:EMichael
ID: 6171922
clockwatcher, you hit it all right on the nose.  My script was indeed using textstream. I was unaware that it does a conversion.  Your detailed explanation gave me a far better grasp of this entire subject and I am very grateful.

Your recommendation to simply mix scripts enlightens me further - never knew this was allowed.  I have tested it here using PWS and it works like a charm.  I am now testing whether I can do a #include to insert the VBscript into the otherwise Jscript-asp page.  Whether this is allowed or not, you have solved the problem for me and I thank you so much.

I would also like to thank all the others who helped as well, particularly TTom, but all others as well.
0
 
LVL 9

Expert Comment

by:TTom
ID: 6172080
You can use the #include with a VBS file in the JS page.  Be careful in that the #include file is processed into the stream before anything else is done.  That's why it is NOT possible to use include files "dynamically".

Other than that, the only thing you MIGHT need to worry about is the order in which things are processed.

While I never get them straight, there is a defined sequence for processing <%%> blocks and <script> blocks.  The order in which <script> blocks are processed has to do with the default language for the page.  If you need to know more, there is documentation on the Microsoft web site which covers this.

One very interesting thing to note is that variables and function values are free to pass between script blocks and languages.  I.e. a global variable is a global variable whether it was "var"'d in a JavaScript block or "Dim"'d with VBScript.  That's one of the best things about the cross language thing.  Sometimes, it's easier to do something in one language than the other.

Good luck.

Tom
0
 

Author Comment

by:EMichael
ID: 6172419
Thank you Tom for teaching me even more.  This has been my first experience asking a question at this forum, and I must say it has been very rewarding and enlightening,  Thank you again.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Auto Submit on dropdown box 14 32
Remove 'disabled' attribute 6 28
es6 typescript how to filter an array 10 40
Create a new template 9 22
Avoid defining the variables in the global scope; trying to define them in a local function scope. Because:   • Look-up is performed every time a variable is accessed.   • Variables are resolved backwards from most specific to least specific scope…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now