Showing posts with label convert. Show all posts
Showing posts with label convert. Show all posts

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

Thursday, December 28, 2006

Error proof way to convert an enum value into a custom string in C#

Here is a code snippet I ran into the other day:

string strMsgType = "";
switch (messageType)
{
case MessageTypes.GeneralMessage:
strMsgType = "0";
break;
case MessageTypes.TargetMessage1On1:
strMsgType = "1";
break;
case MessageTypes.GroupMessage:
strMsgType = "2";
break;
case MessageTypes.BroadcastMessage:
strMsgType = "3";
break;
case MessageTypes.CallBackMessage:
strMsgType = "4";
break;
case MessageTypes.NoticeMessage:
strMsgType = "5";
break;
case MessageTypes.PaperMessage:
strMsgType = "6";
break;
case MessageTypes.DigitalMessage:
strMsgType = "8";
break;
case MessageTypes.LimitedMessage:
strMsgType = "9";
break;
case MessageTypes.SaleMessage:
strMsgType = "A";
break;
case MessageTypes.AdMessage:
strMsgType = "B";
break;
case MessageTypes.RadioAdMessage:
strMsgType = "C";
break;
case MessageTypes.TvAdMessage:
strMsgType = "D";
break;
case MessageTypes.OtherMessage:
strMsgType = "E";
break;
}


It corresponds to the following enum:

public enum MessageType
{
GeneralMessage = 0,
TargetMessage1On1 = 1,
GroupMessage,
BroadcastMessage,
CallBackMessage,
NoticeMessage,
PaperMessage,
DigitalMessage,
LimitedMessage,
SaleMessage,
AdMessage,
RadioAdMessage,
TvAdMessage,
OtherMessage
}


As you can see, there is a bug in the code as it did not have a case statement for enum value 7. It turns out that, that was as designed, i.e. there should never be an enum value 7, which means the enum declaration is incorrect. Either way, there is a bug in the above code.

It is not uncommon to have specific literal values for the enum strings and trying to correlate those literal values with a switch statement elsewhere is tedious and could be bug prone. Instead a better way to convert an enum value to a custom string would be to do something like this:


string s = Convert.ToInt32(messageType).ToString("X0");

I would love to hear what you think?