Welcome to HBH! If you have tried to register and didn't get a verification email, please using the following link to resend the verification email.

data converter - Javascript Code Bank


data converter
convert back and forth between ascii, hex, and base64. should work wherever you put it up at.
                <html>
<head>
<title>convertr8</title>
<script src="http://cdn.jquerytools.org/1.2.3/jquery.tools.min.js"></script>
<script src="http://malformedxmlelement.com/base64.js"></script>
</head>
<body>
let's add functionality<br />
ctrl+e: change to encode mode.<br />
ctrl+d: change to decode mode.<br />
<br />

<form id="form">
<table>
<tr><th>Input</th><th>Encoding type</th><th>Method</th></tr>
<tr>
	<td>
		<textarea rows="10" cols="30" name="data" id="data" /></textarea>
	</td>
	<td>
		<table>

			<tr>
				<td>
					<input type="radio" name="act" id="act" value="encode" checked="checked" /> encode
				</td>
			</tr>
			<tr>
				<td>
					<input type="radio" name="act" id="act" value="decode" /> decode
				</td>

			</tr>
		</table>
	</td>
	<td>
		<table>
			<tr>
				<td>
					<input type="radio" name="encoding" id="encoding" value="hex" checked="checked" /> hex
				</td>

			</tr>
			<tr>
				<td>
					<input type="radio" name="encoding" id="encoding" value="base64" /> base64
				</td>
			</tr>
		</table>
			
</tr>
</table>

</form>
<br /><br />
<div id="out" style="font-family: monospace; width: 500px;"></div>
<script type="text/javascript">
function act() {
return ($("input:checked").attr("id"));
}
last = "";
var STR_PAD_LEFT = 1;
var STR_PAD_RIGHT = 2;
var STR_PAD_BOTH = 3;
function pad(str, len, pad, dir) {

if (typeof(len) == "undefined") { var len = 0; }
if (typeof(pad) == "undefined") { var pad = ' '; }
if (typeof(dir) == "undefined") { var dir = STR_PAD_RIGHT; }

if (len + 1 >= str.length) {

	switch (dir){

		case STR_PAD_LEFT:
			str = Array(len + 1 - str.length).join(pad) + str;
		break;

		case STR_PAD_BOTH:
			var right = Math.ceil((padlen = len - str.length) / 2);
			var left = padlen - right;
			str = Array(left+1).join(pad) + str + Array(right+1).join(pad);
		break;

		default:
			str = str + Array(len + 1 - str.length).join(pad);
		break;

	} // switch

}

return str;

}
function encodeChars(txt,encoding) {
	if(encoding == "hex") {
		return txt.replace(/(.)/g,function(t,p1) { 
			return "%"+pad(p1.charCodeAt(0).toString(16),2,"0",1);
		});
	} else if(encoding == "base64") {
		return Base64.encode(txt);
	}
}

function decodeChars(txt,encoding) {
	if(encoding == "hex") {
		return txt.replace(/\%(..)/g,function(t, p1) {
			return (String.fromCharCode(eval("0x"+p1))).replace(/</g,"&lt;").replace(/>/g,"&gt;") });
	} else if(encoding == "base64") {
		return Base64.decode(txt);
	}
}

$(document).ready(function() {
last=$("input#act:checked").attr("value");
lastact=$("input#encoding:checked").attr("value");
function doit() {
$("#form").submit(function() { return false; });
	data = $("#data").val();
	act = $("input#act:checked").attr("value"); 
	encoding = $("input#encoding:checked").attr("value");
	if(act == "encode") {
		$("#out").html(encodeChars(data,encoding));
	} else if(act == "decode") {
		$("#out").html(decodeChars(data,encoding));
	} else { 
	}
}
$("#data").bind("keyup",function() {
	doit()
});
$("input#encoding").bind("click",function() {
	test = $(this).attr("value") == lastact
	if(test) {
		return false;
	} else {
		lastact = $(this).attr("value");
	}
	doit(); });
$("input#act").bind("click",function() {
	test = $(this).attr("value") == last
	if(test) {
		return false;
	} else {
		last = $(this).attr("value");
	}
	$("#data").val($("#out").text());
	doit(); });
});

$("body").bind("keypress",function(e) {
	if(e.ctrlKey) {
		if(e.which == 100) {
			e.preventDefault();
			e.stopPropagation();
			$("input[value=decode]").click();
		} else if(e.which == 101) {
			e.preventDefault();
			e.stopPropagation();
			$("input[value=encode]").click();
		} else { }
	}
});

</script>
</body>

            
Comments
Sorry but there are no comments to display