Friday, May 9, 2008

Convert an integer to an ascii character in Javascript #2


In my previous post titled Convert an integer to an ascii character in Javascript, I said, I could not find any built-in methods in Javascript to convert a char into an integer and vice versa, well, it turns out that I have not looked hard enough. There are two methods on (one static and one instance as expected) String class that would do this conversion and these method are available since Javascript 1.2. I am sorry about the overlook. Here are sample functions to illustrate how these methods can be used in your code.

// Converts an integer (unicode value) to a char
function itoa(i)
{
   return String.fromCharCode(i);
}


// Converts a char into to an integer (unicode value)
function atoi(a)
{
   return a.charCodeAt();
}

Thanks,


Sonny

Wednesday, May 7, 2008

Convert an integer to an ascii character in Javascript


See my 2nd post for the revised code using built-in Javascript methods.

I was looking for a function to convert an integer into an ascii character in Javascript and could not find one. This is because Javascript does not support explicit character types, and to Javascript character is nothing but a string of length 1. This is what I have come up with. First, you define a string constant consists of all the ascii-7 characters. For non-printable characters you simply use a "?":

//reference: http://www.asciitable.com/
var asciiTable =
// 0 thru 9
"??????????" +
// 10 thru 19
"??????????" +
// 20 thru 29
"??????????" +
// 30 thru 39
"?? !\"#$%&'" +
// 40 thru 49
"()*+,-./01" +
// 50 thru 59
"23456789:;" +
// 60 thru 69
"<=>?@ABCDE" +
// 70 thru 79
"FGHIJKLMNO" +
// 80 thru 89
"PQRSTUVWXY" +
// 90 thru 99
"Z[\\]^_`abc" +
// 100 thru 109
"defghijklm" +
// 110 thru 119
"nopqrstuvw" +
// 120 thru 127
"xyz{|}~?";

Then you could have a function like this:
function itoa(i)
{
   return asciiTable.substr(i, 1);
}

Similarly, for printable characters you can have a function like this to convert character into an integer:
function atoi(c)
{
   return asciiTable.indexOf(c);
}

Hope you find this useful, and please let me know if you know any other elegant or efficient ways to do this in Javascript.

Good scripting,

Sonny

Wednesday, April 23, 2008

Extract URLs from Internet Explorer(IE) History cache


The other day I was trying to extract bookmarks from my IE's history cache, but could not find any tools in IE to do that. Looked around on Google and found a VB Project, which is fine, but I am looking for something using C#. Then, I found this article on CodeProject titled The tiny wrapper class for URL history interface in C#, which would work, but I want a simple script that I can tweak and just run from the command prompt. Again, it would be nice if it is in C# as I have been using this C# Script tool for a while and I really love it. It is called The C# Script Engine by Oleg Shilo and I would highly recommend you explore it if you are not already aware of this gem. It is really clean and lean and does not require any special syntax or setup and could simply use a file that was created with Visual Studio. If you want to automate something and want to do it in C#, this is the tool to use. I would have more to write about this tool later.

Back to our original task, searching on the API calls that are referenced in the above articles, I found the following two links (one on MSDN and one on Google's Code project) which looked promising:

HOW TO: Clear the Cache When Your Application Hosts a WebBrowser Control in Visual C# .NET

WinApis.cs file on Google Code project called csexwb2 - The most complete .NET Webbrowser control wrapper

Mostly based on the code given in WinApis.cs, I written a C# Script that can be run using the C# Script Engine mentioned above or can also be run using csc.exe that comes with the .NET Framework. You could also add this to any C# IDE Project file and run it from there. It writes the entries from IE's cache to a text file called "ieHistory.txt".

The code in the above script file is pretty self explanatory, but I would like to mention a few things that might come handy if you like to experiment with or tweak the script more.

On the MSDN page, it declares the lpszSourceUrlName and lpszLocalFileName as IntPtr and I had problem converting these fields to Strings, so I used the struct definition given in csexwb2.

Using the PInvoke declarations on MSDN page, I was not able to get it recognize the urlPattern argument, and I had to use the declarations given in csexwb2.

Hope you find this useful.

Best Wishes,

Sonny