Arpanet a binary trip.
How do we translate the sign? Light on is a one and light off is a zero.
There is an old joke that says: "There are only 10 types of people in the world: those who understand binary and those who do not." In any case, it does not hurt to at least look at binary once in a while. Ascii (American standard code for information interchange) is usually eight binary characters of ones or zeroes. So you can take the first 8 ones and zeros for the first character.
Then to further split out the whole code sequence:
01000001 01010010 01010000 01000001 01001110 01000101 01010100
Then you can convert on character at a time. You can just look at a table or write a program to do it.
Datafile af:
01000001 01010010 01010000 01000001 01001110 01000101 01010100
$ ./b2a1.sh af
ARPANET
[code]
#!/bin/bash
chrbin() {
echo $(printf \\$(echo "ibase=2; obase=8; $1" | bc))
}
ordbin() {
a=$(printf '%d' "'$1")
echo "obase=2; $a" | bc
}
ascii2bin() {
echo -n $* | while IFS= read -r -n1 char
do
ordbin $char | tr -d '\n'
echo -n " "
done
}
bin2ascii() {
for bin in $*
do
chrbin $bin | tr -d '\n'
done
}
while read z
do
bin2ascii $z
done < $1
echo
[/code]
[code]
<html>
<head>
<script type="text/javascript">
var input_id = "bin_text";
var answer_id = "answer";
function convertToASCII() {
var bin_text = document.getElementById(input_id);
var answer = document.getElementById(answer_id);
if (!answer) {
alert("Error: No element with id \""+answer_id+"\".");
return;
}
if (bin_text)
var text = bin_text.value;
else {
error("No element with id \""+input_id+"\".");
return;
}
var divisible = text.length % 8;
var nonBinary = /[^0|1]/.test(text);
if (text.length > 0 && divisible == 0 && !nonBinary) {
var regex = /[0|1]{8}/g;
var str = text.match(regex);
var code = 0;
var placeVal, exp, digit;
var ascii = '';
while (str.length > 0) {
code = 0;
for (var i=0; i<str[0].length; i++) {
placeVal = 7-i;
exp = Math.pow(2, i);
digit = str[0].charAt(placeVal);
code += exp*digit;
}
str.shift();
ascii += String.fromCharCode(code);
}
answer.innerHTML = "<p class=\"binary\">" + ascii + "</p>";
}
else {
error("Malformed binary.");
return;
}
function error(errText) {
answer.innerHTML = "<span class=\"error\">Error: " + errText + "</span>";
}
}
</script>
<style type="text/css">
.block {
width: 45%;
border: 1px solid #000000;
padding: 10px;
}
.binary {
background-color: #C6FFC7;
padding: 3px;
}
.error {
background-color: #FFC6C6;
padding: 3px;
}
</style>
</head>
<body>
<div style="float:left;" class="block">
<form onSubmit="convertToASCII(); return false;">
<p>Enter some binary to decode:</p>
<input type="text" id="bin_text"/>
</form>
</div>
<div style="float:right;" class="block">
<p id="answer"><br/></p>
</div>
</body>
</html>
[/code]
Comments
Post a Comment