ASCIIcharToNum Javascript

I have some old data that relies on a few ASCII alpha to numeric conversions.

Since I’m using JavaScript for this particular issue, I found it easier to use SuperCard to generate this function than it was to get the C# ASCII thing to work. I couldn’t find one of these elsewhere, so I wrote this one, and it does the trick nicely for me. So if you need a simple JavaScript version of charToNum, well, here ya go!

-Chilton


// ASCIIcharToNum by Chilton Webb
// (chilton@me.com)
// 
// This is a javascript function that 
// takes a single character string and returns 
// the numeric equivalent, as if the string was
// ASCII instead of Unicode.
//
// This works for alpha letters only at this time.
// This was autogenerated by a SuperCard script,  
// so if you need it to do all of them, just ask.
// At this time, it returns 65 (A) if it doesn't
// match something.

function ASCIIcharToNum (myChar : String) : int
{

    if (myChar=="A") return 65;
	else if (myChar=="B") return 66;
	else if (myChar=="C") return 67;
	else if (myChar=="D") return 68;
	else if (myChar=="E") return 69;
	else if (myChar=="F") return 70;
	else if (myChar=="G") return 71;
	else if (myChar=="H") return 72;
	else if (myChar=="I") return 73;
	else if (myChar=="J") return 74;
	else if (myChar=="K") return 75;
	else if (myChar=="L") return 76;
	else if (myChar=="M") return 77;
	else if (myChar=="N") return 78;
	else if (myChar=="O") return 79;
	else if (myChar=="P") return 80;
	else if (myChar=="Q") return 81;
	else if (myChar=="R") return 82;
	else if (myChar=="S") return 83;
	else if (myChar=="T") return 84;
	else if (myChar=="U") return 85;
	else if (myChar=="V") return 86;
	else if (myChar=="W") return 87;
	else if (myChar=="X") return 88;
	else if (myChar=="Y") return 89;
	else if (myChar=="Z") return 90;
	else if (myChar=="[") return 91;
//	else if (myChar=="\") return 92;
	else if (myChar=="]") return 93;
	else if (myChar=="^") return 94;
	else if (myChar=="_") return 95;
	else if (myChar=="`") return 96;
	else if (myChar=="a") return 97;
	else if (myChar=="b") return 98;
	else if (myChar=="c") return 99;
	else if (myChar=="d") return 100;
	else if (myChar=="e") return 101;
	else if (myChar=="f") return 102;
	else if (myChar=="g") return 103;
	else if (myChar=="h") return 104;
	else if (myChar=="i") return 105;
	else if (myChar=="j") return 106;
	else if (myChar=="k") return 107;
	else if (myChar=="l") return 108;
	else if (myChar=="m") return 109;
	else if (myChar=="n") return 110;
	else if (myChar=="o") return 111;
	else if (myChar=="p") return 112;
	else if (myChar=="q") return 113;
	else if (myChar=="r") return 114;
	else if (myChar=="s") return 115;
	else if (myChar=="t") return 116;
	else if (myChar=="u") return 117;
	else if (myChar=="v") return 118;
	else if (myChar=="w") return 119;
	else if (myChar=="x") return 120;
	else if (myChar=="y") return 121;
	else if (myChar=="z") return 122;
	
	return 65; // This is the fail state, so put whatever you want here.
}

Just do

var myChar = "z"[0];
Debug.Log (System.Convert.ToInt32(myChar));

I’m not sure what you mean about C# since there aren’t any C#-specific ASCII functions that I know of; it uses .NET like Javascript does. To use a backslash character you need to escape it: “\”.

–Eric

Hi Eric!

Thanks for your reply. I’m lazy.

At the time I wrote the above function, I needed something immediately, so I looked in the docs, found nothing, then online. I searched Google and here in the forums, and didn’t see an example of this that worked in Javascript, despite everyone’s clamoring that it should work ‘the same way’ in Javascript. I could find no examples that weren’t done in C#. Also, the answer below is not something that I would have thought to just type initially, or I probably would have found it. I’m hoping the use of every possible variation of numToChar, charToNum, etc., in this thread will save someone else some time in the future :sunglasses:

I probably spent 2 minutes reading through the examples and testing things in Unity. :rage:

It took me roughly 20 seconds and 3 lines of SuperTalk to generate the script above. :smile:

And it’s fast enough for me. In the future, I’ll use the below solution…

I just tried

var iii : int = System.Convert.ToInt32("A");

And that seems to be what I needed. Awesome.

Also, I left that one line uncommented because Unitron in 2.6.1 doesn’t properly catch the escaped character, and it doesn’t format the line coloring correctly after that. I don’t know how it is in 3.0, since I haven’t time to update any of my commercial work to it yet.

-Chilton

"" is an escape character, so to use a literal "" you have to use “\”. It’s still one character, the same way that “\n” is a single character even though you write it with two.

–Eric

I realize that.

The Unitron editor does not properly catch the escape character, in that case. As a result, syntax coloring goes nuts after that.
I only needed the alpha letters, so I just commented out the line that was causing the syntax coloring to go nuts.

-Chilton

I honestly can’t believe I just typed that. I had a lengthy conversation with another programmer about how we were so glad syntax hiliting was going away. Fourth gen languages would remove the need to have variable, keywords, and comments colored separately, because they relied on existing human languages. And we all knew fourth gen languages would negate the need for unnecessarily ugly ‘languages’ in the future. Who would be using dot notation and brackets for anything, in the future? Languages were being invented that were so close to English that you could write your software in a normal text editor, and use the spell checker to be your ‘syntax guide’. And bytecode compilers were being made that could compress that human language down to machine code so efficiently that you didn’t really need a runtime compiler or a true ‘compile’ cycle. After all, why the heck would you use a bytecode compiler, if you were just going to write something like C to begin with? Why not use a ‘real’ compiler?

Finally, programmers could stop writing…

for (i=0;i<myString.length;i++)

… and start writing…

repeat with every character in myString

… but we were wrong. Despite those languages’ popularity, somehow, and I’m not sure how, we ‘devolved’ to a point where we have bytecode compiled languages that conform more to C conventions than ‘talk’ conventions.

That conversation was almost 20 years ago. Sheesh. SuperTalk, DreamFactory, and LiveCode are the only remaining products of that effort.

-Chilton