Skip to content

Instantly share code, notes, and snippets.

@aemkei
Forked from 140bytes/LICENSE.txt
Created October 8, 2011 15:11
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save aemkei/1272408 to your computer and use it in GitHub Desktop.
Save aemkei/1272408 to your computer and use it in GitHub Desktop.
Digital Segment Display - 140byt.es

Digital Segment Display - 140byt.es

Converts single digits into old-school ASCII string.

See my digital clock example.

Digit List

 _      _  _       _   _  _   _   _
| |  |  _| _| |_| |_  |_   | |_| |_|
|_|  | |_  _|   |  _| |_|  | |_|  _|

Sample Clock

         _  _     _  _  
| |_| .  _| _| . |_   | 
|   | . |_  _| .  _|  | 

For more information

See the 140byt.es site for a showcase of entries (built itself using 140-byte entries!), and follow @140bytes on Twitter.

To learn about byte-saving hacks for your own code, or to contribute what you've learned, head to the wiki.

140byt.es is brought to you by Jed Schmidt, with help from Alex Kloss. It was inspired by work from Thomas Fuchs and Dustin Diaz.

function f(
a, // number to convert
b // placeholer: index in string to replace
){
return "\n_ |"[ // character map
b % 4 && // add line break for position 4 and 8
b % 2 + -~a // draw space, "|" or "_" based on position
] || "ცᕦဨဢᒦႂႊᅦႪႦ" // data is stored in unicode character
.charCodeAt(a) // convert character to integer
.toString(2) // convert to binary representaion
.replace( // parse binary data
/./g, f // analyse every char
)
}
<!DOCTYPE html>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<title>Digital Clock - 140bytes</title>
<style type="text/css">
body { font-weight: bold; color: lime; background: black; }
pre { display: inline-block; width: auto; }
</style>
<div id="time"></div>
<script>
var digits =
function f(a,b){return"\n_ |"[b%4&&b%2+-~a]||"ცᕦဨဢᒦႂႊᅦႪႦ".charCodeAt(a).toString(2).replace(/./g,f)}
function pad(number){
return (number < 10 ? "0" : "") + number;
}
setInterval(function(){
var display = "",
date = new Date(),
time = pad(date.getHours()) +
pad(date.getMinutes()) +
pad(date.getSeconds()),
i;
for(i in time){
display += "<pre>" + digits(time[i]) + "</pre>";
if (i==1 || i==3){
display += "<pre>\n.\n.</pre>"
}
}
document.getElementById( "time" ).innerHTML = display
}, 1000);
</script>
function f(a,b){return"\n_ |"[b%4&&b%2+-~a]||"ცᕦဨဢᒦႂႊᅦႪႦ".charCodeAt(a).toString(2).replace(/./g,f)}
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2011 YOUR_NAME_HERE <YOUR_URL_HERE>
Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. You just DO WHAT THE FUCK YOU WANT TO.
{
"name": "toDigital",
"description": "Converts digits into an old-school digital ASCII string.",
"keywords": [
"number",
"digital",
"ascii",
"string"
],
"contributors": [
{
"name" : "Martin Kleppe",
"url" : "https://github.com/aemkei"
},
{
"name" : "@tsaniel",
"url" : "https://github.com/tsaniel"
}
]
}
<!DOCTYPE html>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<title>Digits - 140bytes</title>
<style type="text/css"> pre { display: inline-block; width: auto;}</style>
<div id="output"></div>
<script>
var digits =
function f(a,b){return"\n_ |"[b%4&&b%2+-~a]||"ცᕦဨဢᒦႂႊᅦႪႦ".charCodeAt(a).toString(2).replace(/./g,f)}
for (var i=0; i<10; i++){
document.getElementById( "output" ).innerHTML += "<pre>" + digits(i) + "</pre>"
}
</script>
@neizod
Copy link

neizod commented Oct 11, 2011

amazing!

@tsaniel
Copy link

tsaniel commented Oct 12, 2011

Save 7 bytes.

function(a,b,c){for(a in c="ĨǮʼnŌƎĜĘŮĈČ".charCodeAt(a).toString(2))b=[b]+(+c[a]?" ":"|_"[a%2])+(~-a%4?"":"\n");return b}

@aemkei
Copy link
Author

aemkei commented Oct 12, 2011

@tsaniel: Nice improvements!

So I have to remember these two tricks:

b="";b+=x
b=[b]+x
(a-1)%x
~-a%x

@tsaniel
Copy link

tsaniel commented Oct 13, 2011

Let's save another byte.

function(a,b,c){for(a in c="ĨǮʼnŌƎĜĘŮĈČ".charCodeAt(a).toString(2))b=[b]+" |_"[1^c[a]&&a%2+1]+(~-a%4?"":"\n");return b}

@aemkei
Copy link
Author

aemkei commented Oct 13, 2011

Hot stuff! But now it's getting harder for me to update the annotated source.
@tsaniel: What is the crazy^ operator doing in this case?

@tsaniel
Copy link

tsaniel commented Oct 13, 2011

Before we discuss, let's save 2 more bytes first.

function(a,b,c){for(a in c="ĨǮʼnŌƎĜĘŮĈČ".charCodeAt(a).toString(2))b=[b]+" |_"[1^c[a]&&a%2+1]+["\n"[~-a%4]];return b}

Now discuss.
Notice that the c[a] is either 1 or 0.
When c[a] is 1, put space.
When c[a] is 0, draw "|" or "_".
It's sad that the a%2 is either 1 or 0 too, that means we have to separate them.
So first 1^ toggles the c[a] in this case, the space uses the position 0.
And for a%2, plus 1 and use the positions 1 and 2.

By the way, I am finding another way to golf this code.

@aemkei
Copy link
Author

aemkei commented Oct 13, 2011

Down to 126 bytes. Nice work @tsaniel! I'm curious.

@tsaniel
Copy link

tsaniel commented Oct 13, 2011

I've got an idea of using ascii characters codes rather than the extended. It may save some bytes.
(down 20bytes to 10bytes)

@tsaniel
Copy link

tsaniel commented Oct 15, 2011

It's hard to use just 7 bits to do this.
Anyway, the other 2 bytes with replace

function f(a,b){return b+1?" |_"[1^a&&b%2+1]+["\n"[~-b%4]]:"ĨǮʼnŌƎĜĘŮĈČ".charCodeAt(a).toString(2).replace(/./g,f)}

@tsaniel
Copy link

tsaniel commented Oct 15, 2011

Okay, finally i use the ternary.

function f(a,b){return++b?"| _"[a]+["\n"[b%3]]:" "+"؉Щݎݖ·پٵ܂ٚ٠".charCodeAt(a).toString(3).replace(/./g,f)}

And the compatible version

function f(a,b){return++b?"| _".charAt(a)+[["\n"][b%3]]:" "+"؉Щݎݖ·پٵ܂ٚ٠".charCodeAt(a).toString(3).replace(/./g,f)}

@aemkei
Copy link
Author

aemkei commented Oct 16, 2011

Hey @tsaniel,

I can't get this to work.

This is how my test output looks like:

 _       _  _   _   _   _  _   _   _
| |   |  _| _| |_  |_  |_   | |_| |_|
|_|   | |_  _| |    _| |_|  | |_|  _|

So, everything is fine except the number 4.

      =>    1
|_|   =>   020   => 903 => ·
  |   =>   110

... but as soon as I copy and paste the resulting character, it will be somehow converted:

 _    <=    2
|_    <=   021   <= 183 <= ·
|     <=   0

This might be a stupid Mac OS issue or something else when working with UTF8.

And what about the "compatible" version? Is the other one not working in all browsers?

@tsaniel
Copy link

tsaniel commented Oct 17, 2011

That problem is also with my Windows XP, so it might be the encoding problem?

The "compatible" version is for IE6, 7 actually.

@tsaniel
Copy link

tsaniel commented Oct 18, 2011

Let's try another version.

function f(a,b){return"\n_ |"[b%4&&b%2+-~a]||"ცᕦဨဢᒦႂႊᅦႪႦ".charCodeAt(a).toString(2).replace(/./g,f)}

@aemkei
Copy link
Author

aemkei commented Oct 18, 2011

So, no for in loops anymore and down to 120 bytes.

@tsaniel
Copy link

tsaniel commented Oct 19, 2011

Isn't it 119 bytes?

@aemkei
Copy link
Author

aemkei commented Oct 19, 2011

Hmm, my browser (https://raw.github.com/gist/1272408/index.js) and ls -l both tell me: 120B.

@tsaniel
Copy link

tsaniel commented Oct 19, 2011

Whatever - encoding is really funny.

@subzey
Copy link

subzey commented Dec 2, 2011

Maybe I've missed something, but why b%4&&b%2+-~a, not b%4&&b%2-~a?

@subzey
Copy link

subzey commented Dec 2, 2011

function f(a,b){return"\n_ |"[b%4&&b%2-~a]||("uʳ��ɓAE³UQ".charCodeAt(a)*2+4096).toString(2).replace(/./g,f)}

Not tweetable due to non-printable chars corruption. Sorry.

@tsaniel
Copy link

tsaniel commented Dec 4, 2011

@subzey: Seems you miss 2 and 3 ?

@subzey
Copy link

subzey commented Dec 4, 2011

Sorry. Some weird thing is happened with non-printable chars.

Thanks for your reply, @tsaniel! I suppose, my entry cannot be valid as even if it consists of 111 bytes, it cannot be tweeted without corruption.

@tsaniel
Copy link

tsaniel commented Dec 5, 2011

Anyway, good work @subzey!
By the way, could you use the similar trick on the early version?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment