# Encode a string for aliens that read and write English, but don't know ASCII

Turns out there are some distant aliens who know English (and have coincidentally standardized on 8-bit computer architectures). Yet they have never heard of ASCII, nor even alphabetical order.

It is believed that these aliens are picking up information from Earth. Yet they are doing so with digital equipment that reads meaningless information when no transmission is being sent...thus the signal must be contained in a way to stand out from surrounding noise.

A new standard has been created, modeled after the Arecibo Message's use of a semiprime signal length:

# Universal Semiotic Coding for Information Interchange (USCII)

(See: http://uscii.hostilefork.com)

In USCII, each code is derived from a bitmap representing the character, that has a prime width and a prime height. For instance, this 5x7 representation of a letter A:

01110
10001
10001
10001
11111
10001
10001


That produces 35 bits. Yet in order to make even single-character messages stand out from the noise and look coherent, strings are embedded in a container format that helps reinforce the prime factors. The container has been chosen to also make it easy to manipulate the strings on architectures that operate on multiples of 8 bits.

With its container, the "A" above would look like:

0000000000000000000000000000000000000000
0000000000000000000000000000000000000000
0000000000000000000000000000000000000000
0000000000000000000000000000000000000000
0000000000000000000000000000000000000000
0000000000000000000000000000000000000000
0000000000000000000000000000000000000000
1111111111111111111111111111111111100000
1111111111111111111111111111111111100000
1111111111111111111111111111111111100000
1111111111111111111111111111111111100000
1111111111111111111111111111111111100000
0111010001100011000111111100011000100000
1111111111111111111111111111111111100000
1111111111111111111111111111111111100000
1111111111111111111111111111111111100000
1111111111111111111111111111111111100000
1111111111111111111111111111111111100000
1111111111111111111111111111111111100000
1111111111111111111111111111111111100000
1111111111111111111111111111111111111111
1111111111111111111111111111111111111111
1111111111111111111111111111111111111111
1111111111111111111111111111111111111111
1111111111111111111111111111111111111111


The characters of the message are first prefixed with five solid 5x7 bitmaps, and seven solid 5x7 bitmaps added to the end. (The "meter".) Then each of these have five 0 bits tacked onto the end to make an even 40 bits.

Finally there are 7 all-zero 40-bit runs added to the beginning, and 5 all-one 40-bit runs added to the end. (The "silence".)

You are to write an encoder. Input is a conventional ASCII string typed from the keyboard. The bitmaps you will use are from the 5x7 PIC CPU font.

(Note: It's easy enough to extract the font from the page for all the talented code golfers here, but if you want those bytes all together here they are.)

Output should be a printed string in hexadecimal.

# Examples

Input:

ABCDEFG


Output:

0000000000000000000000000000000000000000000000000000000000000000000000FFFFFFFFE0FFFFFFFFE0FFFFFFFFE0FFFFFFFFE0FFFFFFFFE074631FC620F463E8C7C074610845C0E4A318CB80FC21E843E0FC21E84200746178C5E0FFFFFFFFE0FFFFFFFFE0FFFFFFFFE0FFFFFFFFE0FFFFFFFFE0FFFFFFFFE0FFFFFFFFE0FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF

Input:

Dear aliens: Please get me off of this planet!!!


Output:

0000000000000000000000000000000000000000000000000000000000000000000000FFFFFFFFE0FFFFFFFFE0FFFFFFFFE0FFFFFFFFE0FFFFFFFFE0E4A318CB80001D1FC1E0001C17C5E0002D9842000000000000001C17C5E061084211C00100421080001D1FC1E0002D98C620001F0707C003180630000000000000F463E8420061084211C0001D1FC1E0001C17C5E0001F0707C0001D1FC1E00000000000001F1787C0001D1FC1E0213E421440000000000000375AD620001D1FC1E00000000000001D18C5C01148E210801148E210800000000000001D18C5C01148E210800000000000213E4214408421E8C6200100421080001F0707C00000000000003D1F420061084211C0001C17C5E0002D98C620001D1FC1E0213E421440210840008021084000802108400080FFFFFFFFE0FFFFFFFFE0FFFFFFFFE0FFFFFFFFE0FFFFFFFFE0FFFFFFFFE0FFFFFFFFE0FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF

# Winning Conditions

0000000000000000000000000000000000000000000000000000000000000000000000FFFFFFFFE0FFFFFFFFE0FFFFFFFFE0FFFFFFFFE0FFFFFFFFE074610845C0001D18C5C00842F8C5E0001D1FC1E00000000000746178C5E0001D18C5C061084211C01148E21080210840008021084000802108400080FFFFFFFFE0FFFFFFFFE0FFFFFFFFE0FFFFFFFFE0FFFFFFFFE0FFFFFFFFE0FFFFFFFFE0FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF

• In my opinion, aliens probably think we are crazy for sending them that stuff. – Kyle Kanos May 12 '14 at 16:02
• @KyleKanos If the aliens are smart, they'll think it's cool. The conclusion that humanity is crazy will happen shortly after someone then uses the format to transmit them information from global news outlets. – Dr. Rebmu May 13 '14 at 0:36
• The last line is not formatted, can you please format it like the other input and output lines? – A.L May 13 '14 at 17:01
• @n.1 Is it not displaying properly in your browser? It looks fine in mine. Happy to change it if there's a technical problem, but as it describes the winning condition it is technically a "use" and not a "mention" in the use-mention distinction, so I actually had it in the body text style on purpose. :-) – Dr. Rebmu May 14 '14 at 2:46
• @Dr. Rebmu: in my browser on the desktop it's OK, but with the Android app the question is too wide: meta.stackexchange.com/q/231512/237701 – A.L May 14 '14 at 17:09

# Python 2, 671669 663 bytes

D=str.decode
d=D(D("eJxNUmsSHiEIuxIgvn76sXr/IzVB26kzqyxCCBGRt0xlOM5RbXp6qv32Omu6lT22nTGiOVe05muMFXM11ZuuynwV52f8E7W11+SdzGMvTl6GHvm3ig6RRBbp3nqNqGEFeACK7m5YZYPAMMGV6qyk4ydKKD3FTPeAcQZhUAw4lh4aR8YcWaaVV480lDTJF64thyT4T+8tCjVofOtLnKInAAYDyJ0e9lxj+wJk/MYJ0nBQvYaRhnbQQEIrB5t1fX2VwdMnZF1zLF/Gf2H62PtXGYwypU5msXo2mNUvclE+QXqY3sNkg3GcKReKRTPrGnizNNCOw+tenSWaLQZf6VC926UYVw7I0y/eDEOo/LfKvvLdEQjt7NTzLXrkI2MmNrMPRYDng4a6VR0ThTKMgUA9XlYzTIsk+XtCH+y2mk1rf6VD4V7Xh2Cxb2YWkdkprlDCTR6yv5jnQSuQScEb0CBu5WXZ1ScN6gMDvWa73HNoy845VMg7wLBlA9wwhAMTiNn6ravFH00dtSc=","base64"),"zip")
i=raw_input()
m="F"*8+"E0"
o="0"*70+m*5
for c in i:x=ord(c)*9-288;o+=d[x:x+9]+"0"
print o+m*7+"F"*50


# PHP with zlib, 738716 715 bytes

<?php $d=gzinflate(base64_decode("TVJrEh4hCLsSIL5++rF6/yM1QdupM6ssQggRkbdMZTjOUW16eqr99jprupU9tp0xojlXtOZrjBVzNdWbrsp8Fedn/BO1tdfkncxjL05ehh75t4oOkUQW6d56jahhBXgAiu5uWGWDwDDBleqspOMnSig9xUz3gHEGYVAMOJYeGkfGHFmmlVePNJQ0yReuLYck+E/vLQo1aHzrS5yiJwAGA8idHvZcY/sCZPzGCdJwUL2GkYZ20EBCKwebdX19lcHTJ2Rdcyxfxn9h+tj7VxmMMqVOZrF6NpjVL3JRPkF6mN7DZINxnCkXikUz6xp4szTQjsPrXp0lmi0GX+lQvdulGFcOyNMv3gxDqPy3yr7y3REI7ezU8y165CNjJjazD0WA54OGulUdE4UyjIFAPV5WM0yLJPl7Qh/stppNa3+lQ+Fe14dgsW9mFpHZKa5Qwk0esr+Y50ErkEnBG9AgbuVl2dUnDeoDA71mu9xzaMvOOVTIO8CwZQPcMIQDE4jZ+q2rxR8="));$i=trim(fgets(STDIN));$f="str_repeat";echo$f(0,70).$f($m="FFFFFFFFE0",5);foreach(str_split($i)as$c)echo substr($d,ord($c)*9-288,9)."0";echo $f($m,7).$f("F",50);  First, your PHP needs to be configured with --with-zlib. And run this script from command line, like php 26972.php. This program takes input from STDIN. (If short_open_tag=On, I can save 3 more bytes.) ## Ungolfed <?php$data = gzinflate(base64_decode("TVJrEh4hCLsSIL5++rF6/yM1QdupM6ssQggRkbdMZTjOUW16eqr99jprupU9tp0xojlXtOZrjBVzNdWbrsp8Fedn/BO1tdfkncxjL05ehh75t4oOkUQW6d56jahhBXgAiu5uWGWDwDDBleqspOMnSig9xUz3gHEGYVAMOJYeGkfGHFmmlVePNJQ0yReuLYck+E/vLQo1aHzrS5yiJwAGA8idHvZcY/sCZPzGCdJwUL2GkYZ20EBCKwebdX19lcHTJ2Rdcyxfxn9h+tj7VxmMMqVOZrF6NpjVL3JRPkF6mN7DZINxnCkXikUz6xp4szTQjsPrXp0lmi0GX+lQvdulGFcOyNMv3gxDqPy3yr7y3REI7ezU8y165CNjJjazD0WA54OGulUdE4UyjIFAPV5WM0yLJPl7Qh/stppNa3+lQ+Fe14dgsW9mFpHZKa5Qwk0esr+Y50ErkEnBG9AgbuVl2dUnDeoDA71mu9xzaMvOOVTIO8CwZQPcMIQDE4jZ+q2rxR8="));
$input = trim(fgets(STDIN));$f = "str_repeat";

echo $f(0, 70).$f($m = "FFFFFFFFE0", 5); foreach(str_split($input) as $c) echo substr($data, ord($c) * 9 - 288, 9)."0"; echo$f($m, 7).$f("F", 50);

• +1 for idea of using only 9 hex digits in compressed data. – user2846289 May 17 '14 at 12:05
• 716 is the # to beat so far... though I'd imagine it can be done in less! I'll bounty it in a bit, but if no one beats the count, I'll wind up giving it to you... at least you have a Python version and not PHP. :-) – Dr. Rebmu May 17 '14 at 12:52
• All right, 663 and not in PHP--even better. :-) The bounty is yours! – Dr. Rebmu May 24 '14 at 12:48

C

This actually prints human readable hex (vs binary for readablity). Everything is represented in unsigned long long and only 9-10 hex digits are used due (depending whether you include the 00000 padding) the unfortunate 6x5 format (an 8x4 font would have just been too easy)

#include <stdio.h>
#define u unsigned long long
void p(int n, u x){while(n--) printf("%09llX",x);}
int main(void){
char *s="ABCDEFG";
p(7,0ULL);
p(5,0xFFFFFFFE0ULL);
while(*s)p(1,a[*s++]);
p(7,0xFFFFFFFE0ULL);
p(5,0xFFFFFFFFFULL);
return 0;
}


bash

#!/bin/sh
p(){
i=0
while ([ $i -lt$1 ]) do
printf $2 i=$((i+1))
done
}

t(){
#Given string input $1, go char by char & create "alien" representation C='~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ !"#$%&~)*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_abcdefghijklmnopqrstuvwxyz{|}~'

j=0
p 7 0000000000
p 5 FFFFFFFFE0
while ([ "${j}" -lt "${#1}" ]) do
i=${C%${1:$j:1}*} i=${#i}
printf ${A:$(( $i*10 )):10} j=$(($j+1)) done p 7 FFFFFFFFE0 p 5 FFFFFFFFFF } t "Dear aliens: Please get me off of this planet!!!"  • @Dr.Rebmu It took longer than I expected to get the "font" into a workable format. I had planned to post my other shortly, but it took more conversions and formatting than expected. – technosaurus May 12 '14 at 20:56 • +1 for meeting the spec! But there are lots of possibilities in golfing. Why introduce a new counter in the p routine when n is mutable? Why repeat the phrase "unsigned long long" twice when a #define could cut that down? Is paying for an 0x and ULL worth it in a hex encoded table where you're only getting 4 bits per character on the digits themselves? Etc. – Dr. Rebmu May 12 '14 at 22:22 • @Dr.Rebmu fixed the extra counter (oversight), and I planned to use a macro for the 0x##number##ULL (see bash version for the char alternative), but ideone.com thought it was a function call so I nixed it (gcc was fine) ... I could remove the (void) from main too, but some things just seem wrong. – technosaurus May 12 '14 at 23:01 • In code golf, the only "wrong" is if it doesn't run or meet the spec...being "right" is fewer characters! If you're going to use the approach here in your C program, I'd definitely say you can get the character count down by using hex strings instead of bothering with the unsigned long long (especially when you don't need the integer values except to convert them back into strings, which adds code...) – Dr. Rebmu May 13 '14 at 0:32 • Please add byte count to the title. Also, you should golf your code down more. – user12205 May 13 '14 at 23:48 # PHP, 874 bytes You will need to run this script from the command line (php uscii.php) because it takes input from stdin. <?php$f=base64_decode(str_replace('@','AA','@@@QhC@RSl@AClfV9SiPo4viYyIiJjZKiKyawi@@REIQQSCCEIiAEq6p@IT5C@@GEQ@Pg@@@xgAiIiAHRnXMXEYQhCOdEIiI/8RBBi4RlS+IX4eCGLjIh6MXfCIiEIdGLoxc6MXhEwDGAYwAGMAwiBERBBBAB8HwAQQQREQ6IREAR0QtrVzoxj+MfRj6MfOjCEIu5KMYy5+EPQh/8IehCDowvGL4xj+MYuIQhCOOIQhSZGVMUlGEIQhD8d1rGMYxzWcYujGMYu9GPoQg6MY1k30Y+pKL4QcEPvkIQhCRjGMYujGMYqJGMY1qqMVEVGMYxUQhPhEREPuQhCEOBBBBBA4QhCE4iog@@@AH0EE@@A4L4vhCHox8AD4Qg8IQvjF4AOj+DxFI4hC@+Lw+hCHoxiAgCEIQQBCFJiEJUxSWEIQhHAButaxAC2YxiADoxi4APR9C@Pi8IQAtmEI@+Dg+IT5CFEAEYxi4AIxiogARjWqgAioiogBFRCIAD4iI+MhEEIMhCAIQmCEEQmAug@@='));$a=trim(fgets(STDIN));$r='str_repeat';echo$r('0',70).$r($q='FFFFFFFFE0',5);foreach(str_split($a)as$c){$v=(ord($c)-32)*35;$x=$v>>3;$y=1<<(7-$v%8);$b=0;for($i=0;$i<40;$i++){$b+=$b+($i<35&&ord($f[$x])&$y);if(($i&7)==7)$b=!printf('%02X',$b);if(!($y>>=1)&&$x++)$y=128;}}echo$r($q,7).$r('F',50);


## Ungolfed:

<?php

// Font bitmap data rearranged as 95 consecutive 35-bit strings and padded
// out to 416 bytes with zeros:
$f=base64_decode(str_replace('@','AA', '@@@QhC@RSl@AClfV9SiPo4viYyIiJjZKiKyawi@@REIQQSCCEIiAEq6p@IT5C@@GEQ@Pg'. '@@@xgAiIiAHRnXMXEYQhCOdEIiI/8RBBi4RlS+IX4eCGLjIh6MXfCIiEIdGLoxc6MXhEw'. 'DGAYwAGMAwiBERBBBAB8HwAQQQREQ6IREAR0QtrVzoxj+MfRj6MfOjCEIu5KMYy5+EPQh'. '/8IehCDowvGL4xj+MYuIQhCOOIQhSZGVMUlGEIQhD8d1rGMYxzWcYujGMYu9GPoQg6MY1'. 'k30Y+pKL4QcEPvkIQhCRjGMYujGMYqJGMY1qqMVEVGMYxUQhPhEREPuQhCEOBBBBBA4Qh'. 'CE4iog@@@AH0EE@@A4L4vhCHox8AD4Qg8IQvjF4AOj+DxFI4hC@+Lw+hCHoxiAgCEIQQB'. 'CFJiEJUxSWEIQhHAButaxAC2YxiADoxi4APR9C@Pi8IQAtmEI@+Dg+IT5CFEAEYxi4AIx'. 'iogARjWqgAioiogBFRCIAD4iI+MhEEIMhCAIQmCEEQmAug@@=')); // Fetch input:$a=trim(fgets(STDIN));

$r='str_repeat'; echo$r('0',70).$r($q='FFFFFFFFE0',5);

// Convert each input character into its corresponding hex sequence:
foreach (str_split($a) as$c) {
$v=(ord($c)-32)*35;
$x=$v>>3;
$y=1<<(7-$v%8);
$b=0; for ($i=0;$i<40;$i++) {
// When $i>34, pad with zeros$b += $b + ($i<35 && ord($f[$x]) & $y); if (($i&7)==7) $b=!printf('%02X',$b); // print out $b and set$b=0
// Shift mask to next bit position
if (!($y>>=1)&&$x++) $y=128; } } // Tail: echo$r($q,7).$r('F',50);

• +1 (though I hope no one sends PHP to the aliens!!) – Dr. Rebmu May 13 '14 at 7:04

## JavaScript ES6 (838 Bytes)

_="z='00000';r=(n,m)=>m.repean);fop=prompi=o=';i<p.length;)o+=parseInatob('RSlClfV9SiPo4viYyIiJjZKiKyawiRQQSCCEq6pGEQPgxgAiIHRnXMXEYOdiI/8Ri4RlS+IX4eCGLjIhXfCIidGLcXhEwDGAYwAGMAwiBERBAB8HwAQQQREQ6IREAR0QtrVzj+MfRjfOjCu5KMYy5+EP/8IeDowvGL4xj+MYuIOOISZGVMUlGD8d1rxzWcYuju9GPoQgY1k30Y+pKL4QcEPvkIRjujqJ1qqMVEVxUPhEREPuEOBA4E4H0EEA4L4vH8AD4Qg8IQvjF4AOj+DxFI4+Lw+HgCQQBCFJiEJUxSWHAButaxAC2YxDi4APR9CPi8IQAtm+Dg+FEAEYxi4AIxARjWqgAioBFRCIAD4iI+MhEMAIQmCEEQmAugRV8nyE.split   p(e=>e028)).join    tch(/.{35}/g)[pi++)-32]+z,21610);14,z5,y='E0+o.toUpperCase(7,y5,10,'F)A.charCodeAQhGMY).toString(EI)).slice(-)+ ('.maC')hCoxiog(z+z+t(r(iABB6MFFAIT5C";for(Y in $=" ")with(_.split($[Y]))_=join(pop());eval(_)


Not crushed :

z='00000';r=(n,m)=>m.repeat(n);for(p=prompt(i=o='');i<p.length;)o+=(z+z+parseInt(atob('AAAAAAQhCAARSlAAAClfV9SiPo4viYyIiJjZKiKyawiAAAAREIQQSCCEIiAEq6pAAIT5CAAAAGEQAAPgAAAAAAxgAiIiAHRnXMXEYQhCOdEIiI/8RBBi4RlS+IX4eCGLjIh6MXfCIiEIdGLoxc6MXhEwDGAYwAGMAwiBERBBBAB8HwAQQQREQ6IREAR0QtrVzoxj+MfRj6MfOjCEIu5KMYy5+EPQh/8IehCDowvGL4xj+MYuIQhCOOIQhSZGVMUlGEIQhD8d1rGMYxzWcYujGMYu9GPoQg6MY1k30Y+pKL4QcEPvkIQhCRjGMYujGMYqJGMY1qqMVEVGMYxUQhPhEREPuQhCEOBBBBBA4QhCE4iogAAAAAAAH0EEAAAAA4L4vhCHox8AD4Qg8IQvjF4AOj+DxFI4hCAA+Lw+hCHoxiAgCEIQQBCFJiEJUxSWEIQhHAButaxAC2YxiADoxi4APR9CAAPi8IQAtmEIAA+Dg+IT5CFEAEYxi4AIxiogARjWqgAioiogBFRCIAD4iI+MhEEIMhCAIQmCEEQmAugAAARV8nyE').split('').map(e=>(z+z+e.charCodeAt(0).toString(2)).slice(-8)).join('').match(/.{35}/g)[p.charCodeAt(i++)-32]+z,2).toString(16)).slice(-10);r(14,z)+r(5,y='FFFFFFFFE0')+o.toUpperCase()+r(7,y)+r(5,r(10,'F'))
`
• +1 but beyond an un-crushed version, it's helpful to see it in a readable format without horizontal scrolling... – Dr. Rebmu May 17 '14 at 12:54