SystemCrash
05-02-2003, 05:58 PM
/**
*/
function base64ToAscii(c)
{
var theChar = 0;
if (0 <= c && c <= 25)
{
theChar = String.fromCharCode(c + 65);
}
else if (26 <= c && c <= 51)
{
theChar = String.fromCharCode(c - 26 + 97);
}
else if (52 <= c && c <= 61)
{
theChar = String.fromCharCode(c - 52 + 48);
}
else if (c == 62)
{
theChar = '+';
}
else if( c == 63 )
{
theChar = '/';
}
else
{
theChar = String.fromCharCode(0xFF);
}
return theChar;
}
/**
* A helper function for converting a hex digit into the corresponding
* integer value. I really don't know much about JavaScript, so the
* the conversion is a little laborious. There probably is an easier
* way (including a built-in function), but I don't know it.
*/
function hexval(c) {
if (String('0').charCodeAt(0) <= c && c <= String('9').charCodeAt(0))
return c - String('0').charCodeAt(0);
if (String('A').charCodeAt(0) <= c && c <= String('F').charCodeAt(0))
return c - String('A').charCodeAt(0) + 10;
if (String('a').charCodeAt(0) <= c && c <= String('f').charCodeAt(0))
return c - String('a').charCodeAt(0) + 10;
return 0;
}
/**
* Decodes a string that has been previously encoded according to the
* myUrlDecode() function.
*@see myUrlEncode
*/
function myBase64Decode(str, is_binary) {
var result = "";
var i = 0;
var x;
var shiftreg = 0;
var count = -1;
for (i=0; i < str.length; i++) {
c = str.charAt(i);
if ('A' <= c && c <= 'Z')
x = str.charCodeAt(i) - 65;
else if ('a' <= c && c <= 'z')
x = str.charCodeAt(i) - 97 + 26;
else if ('0' <= c && c <= '9')
x = str.charCodeAt(i) - 48 + 52;
else if (c == '+')
x = 62;
else if (c == '/')
x = 63;
else
continue;
count++;
switch (count % 4)
{
case 0:
shiftreg = x;
continue;
case 1:
v = (shiftreg<<2) | (x >> 4);
shiftreg = x & 0x0F;
break;
case 2:
v = (shiftreg<<4) | (x >> 2);
shiftreg = x & 0x03;
break;
case 3:
v = (shiftreg<<6) | (x >> 0);
shiftreg = x & 0x00;
break;
}
if (!is_binary && (v < 32 || v > 126) && (v != 0x0d) && (v != 0x0a))
{
result = result + "<";
result = result + "0123456789ABCDEF".charAt((v/16)&0x0F);
result = result + "0123456789ABCDEF".charAt((v/1)&0x0F);
result = result + ">";
}
else
result = result + String.fromCharCode(v);
}
return result.toString();
}
Hello Everyone!
I'm in need of assistance. The above js is used to decode a base64 string. A user may enter the encoded string in the,
<TEXTAREA name=text1 size=100 style="FONT-SIZE: smaller" cols=60 rows=10></textarea>, and press the button
<INPUT name=button2 onclick="myUrlDecode(this.form.text1.value, this.form.text1, document.links[0]);" type=button value=Decode> to find the decoded value of a base64 string.
What I would like to do is have the encoded value set inside the code as a var (no users involved). I want the script to decode that value automatically (on page load perhaps). Then... I would like the decoded value to be saved as a different var.
I've been trying for days to convert this code to get it to do what I want it to. I haven't had any luck in doing so. Could any of you please help me out? Thank you for your time.
Halo.
*/
function base64ToAscii(c)
{
var theChar = 0;
if (0 <= c && c <= 25)
{
theChar = String.fromCharCode(c + 65);
}
else if (26 <= c && c <= 51)
{
theChar = String.fromCharCode(c - 26 + 97);
}
else if (52 <= c && c <= 61)
{
theChar = String.fromCharCode(c - 52 + 48);
}
else if (c == 62)
{
theChar = '+';
}
else if( c == 63 )
{
theChar = '/';
}
else
{
theChar = String.fromCharCode(0xFF);
}
return theChar;
}
/**
* A helper function for converting a hex digit into the corresponding
* integer value. I really don't know much about JavaScript, so the
* the conversion is a little laborious. There probably is an easier
* way (including a built-in function), but I don't know it.
*/
function hexval(c) {
if (String('0').charCodeAt(0) <= c && c <= String('9').charCodeAt(0))
return c - String('0').charCodeAt(0);
if (String('A').charCodeAt(0) <= c && c <= String('F').charCodeAt(0))
return c - String('A').charCodeAt(0) + 10;
if (String('a').charCodeAt(0) <= c && c <= String('f').charCodeAt(0))
return c - String('a').charCodeAt(0) + 10;
return 0;
}
/**
* Decodes a string that has been previously encoded according to the
* myUrlDecode() function.
*@see myUrlEncode
*/
function myBase64Decode(str, is_binary) {
var result = "";
var i = 0;
var x;
var shiftreg = 0;
var count = -1;
for (i=0; i < str.length; i++) {
c = str.charAt(i);
if ('A' <= c && c <= 'Z')
x = str.charCodeAt(i) - 65;
else if ('a' <= c && c <= 'z')
x = str.charCodeAt(i) - 97 + 26;
else if ('0' <= c && c <= '9')
x = str.charCodeAt(i) - 48 + 52;
else if (c == '+')
x = 62;
else if (c == '/')
x = 63;
else
continue;
count++;
switch (count % 4)
{
case 0:
shiftreg = x;
continue;
case 1:
v = (shiftreg<<2) | (x >> 4);
shiftreg = x & 0x0F;
break;
case 2:
v = (shiftreg<<4) | (x >> 2);
shiftreg = x & 0x03;
break;
case 3:
v = (shiftreg<<6) | (x >> 0);
shiftreg = x & 0x00;
break;
}
if (!is_binary && (v < 32 || v > 126) && (v != 0x0d) && (v != 0x0a))
{
result = result + "<";
result = result + "0123456789ABCDEF".charAt((v/16)&0x0F);
result = result + "0123456789ABCDEF".charAt((v/1)&0x0F);
result = result + ">";
}
else
result = result + String.fromCharCode(v);
}
return result.toString();
}
Hello Everyone!
I'm in need of assistance. The above js is used to decode a base64 string. A user may enter the encoded string in the,
<TEXTAREA name=text1 size=100 style="FONT-SIZE: smaller" cols=60 rows=10></textarea>, and press the button
<INPUT name=button2 onclick="myUrlDecode(this.form.text1.value, this.form.text1, document.links[0]);" type=button value=Decode> to find the decoded value of a base64 string.
What I would like to do is have the encoded value set inside the code as a var (no users involved). I want the script to decode that value automatically (on page load perhaps). Then... I would like the decoded value to be saved as a different var.
I've been trying for days to convert this code to get it to do what I want it to. I haven't had any luck in doing so. Could any of you please help me out? Thank you for your time.
Halo.