Braille-ify a string

22

0

And no, This is not a dupe of Translate ASCII text to braille.

There are 28=256 Braille patterns in Unicode. (By 'Braille' I mean 8-cell ones)

W, wait. How many ASCII characters were there?

27 = 128?

Well then, Let's turn ASCII into Braille, 'cause there is absolutely no reason not to!


The way from ASCII, to Braille

We can see each cells represent a bit, which each cell is 'punched' or not.

Now we can allocate each cells to represent the bits of the ASCII character as binary.

(1  )(16 )
(2  )(32 )
(4  )(64 )
(8  )( - )

* ( - ) is blank

Now we can convert ASCII to Braille. For example, A(65=01000001) equals to .

Examples

Input -> Output
Braille! -> ⠢⠺⠱⡱⡴⡴⠵⠑
(Upscaled)
.. .o o. o. .. .. o. o.
o. oo .o .o .o .o .o .o
.o .o .o .o oo oo oo ..
.. .. .. o. o. o. .. ..

Matthew Roh

Posted 2017-04-21T08:13:42.940

Reputation: 5 043

Surely a is , not (which I think is q)? – Neil – 2017-04-21T09:19:51.577

@Neil The challenge isn't just "convert char codes + 10240 to chars." And yes, a is . – Erik the Outgolfer – 2017-04-21T09:41:35.597

@EriktheOutgolfer I didn't suggest it was, but would have been wrong either way, since it has the wrong number of punched cells. – Neil – 2017-04-21T10:05:39.977

@Neil Oh nice. I just recalculated and found out you're right. – Matthew Roh – 2017-04-21T12:56:58.703

Does it feel weird to anyone else that the LSB (bottom right) is left unused, rather than the MSB (top left)? – Julian Wolf – 2017-04-21T22:51:42.067

Not really. The ordering is column-major little-endian. The missing bit, which would go at the bottom right, is the MSB (bit 7, the 128s bit). – Buster – 2017-04-21T23:49:39.807

Of course for real Braille the difficulties in discerning different one-dot values should be obvious. As for "reasons not to." :) – Wildcard – 2017-04-22T01:30:38.840

@SIGSEGV : Where can I find all Unicode Braille signs ? – LMD – 2017-04-22T16:25:27.020

@Buster, you're right. I guess my brain wanted the numbers to be descending, despite the clear diagram showing otherwise in the question ¯\_(ツ)_/¯ – Julian Wolf – 2017-04-22T22:46:45.953

@user7185318 The characters used in the question are <U+2822><U+283A><U+2831><U+2871><U+2874><U+2874><U+2835><U+2811>. Found using $ echo ⠢⠺⠱⡱⡴⡴⠵⠑ | iconv -t ascii --unicode-subst="<U+%04X>". – luser droog – 2017-04-22T22:59:21.403

Answers

14

CJam, 27 26 bytes

80qf{i2b7Te[4/~\)\@+++2bc}

Try it online!

Explanation

The Braille code points are neatly ordered so that the individual dots do count up in binary. However, the ordering of the bits in the code points is different. We want the following order:

04
15
26
37

Whereas the characters are laid out in Unicode in this order:

03
14
25
67

(Which kinda makes sense, because historically, Braille only used the first six dots.) Note that we don't need the 7 dot, since the input is guaranteed to be in the ASCII range. So given a list of bits [6 5 4 3 2 1 0] of an input character, we want to reorder them into [3 6 5 4 2 1 0], to pull the bit representing the bottom-left dot to the most significant position.

80     e# Push 80... we'll need this later.
q      e# Read all input.
f{     e# Map this block onto each character, putting a copy of the 80
       e# below each character.
  i    e#   Convert the character to its code point.
  2b   e#   Get its binary representation.
  7Te[ e#   Pad it to 7 bits with zeros. We've now got some bit list
       e#   [6 5 4 3 2 1 0].
  4/   e#   Split into chunks of 4: [[6 5 4 3] [2 1 0]]
  ~    e#   Dump them onto the stack: [6 5 4 3] [2 1 0]
  \    e#   Swap them: [2 1 0] [6 5 4 3]
  )    e#   Pull off the last element: [2 1 0] [6 5 4] 3
  \    e#   Swap: [2 1 0] 3 [6 5 4]
  @    e#   Rotate: 3 [6 5 4] [2 1 0]
  ++   e#   Concatenate twice: [3 6 5 4 2 1 0]
       e#   That's the reordering done.
  +    e#   Prepend the 80. That puts it in the 2^7 position of the
       e#   binary digit list, which gives it a value of 10240, which
       e#   is where the Braille characters start.
  2b   e#   Convert the bits back to an integer.
  c    e#   Convert the code point to the corresponding integer.
}%

Martin Ender

Posted 2017-04-21T08:13:42.940

Reputation: 184 808

1Clever trick with the 80. – Erik the Outgolfer – 2017-04-21T08:58:37.370

11

JavaScript (ES6), 83 bytes

f=
s=>s.replace(/./g,c=>String.fromCharCode((c=c.charCodeAt())&7|c*8&64|c/2&56|10240))
<input oninput=o.textContent=f(this.value)><pre id=o>

Neil

Posted 2017-04-21T08:13:42.940

Reputation: 95 035

Oh yeah, I should probably divide by 2 before taking that AND to save a byte as well. – Martin Ender – 2017-04-21T10:55:01.260

Maybe jQuery abuse can be used? – Matthew Roh – 2017-04-21T12:50:50.063

6

Python 3, 98 71 66 bytes

lambda s:''.join(chr(i*8&64|i//2&56|i&7|10240)for i in map(ord,s))

Try it online!

ovs

Posted 2017-04-21T08:13:42.940

Reputation: 21 408

5

CJam, 27 bytes

1 byte stolen from Neil.

q{i__8&8*@7&@2/56&++'⠀+}%

Try it online!

Explanation

This uses the same basic idea as my other CJam answer but uses bitwise arithmetic instead of base conversion and list manipulation to reorder the bits.

q        e# Read all input.
{        e# Map this block over each character...
  i__    e#   Convert the character to its code point and make two copies.
  8&     e#   AND 8. Gives the 4th bit, which we need to move to the 7th place.
  8*     e#   Multiply by 8 to move it up three places.
  @7&    e#   Pull up another copy and take it AND 7. This extracts the three
         e#   least significant bits which shouldn't be moved at all.
  @2/    e#   Pull up the last copy and divide by 2 to shift all bits down
         e#   by one place.
  56&    e#   AND 56. Extracts the three most-significant bits.
  ++     e#   Add all three components back together.
  '⠀+    e#   Add to the empty Braille character which is the offset for all
         e#   the code points and which converts the value to a character.
}%

Martin Ender

Posted 2017-04-21T08:13:42.940

Reputation: 184 808

3

PHP, 109 Bytes

foreach(str_split($argn)as$c)echo json_decode('"\u'.dechex(10240+(($d=ord($c))&7)+($d/2&56)+(($d&8)*8)).'"');

Online Version

Jörg Hülsermann

Posted 2017-04-21T08:13:42.940

Reputation: 13 026

2

Mathematica 100 Bytes

FromCharacterCode[10240+#~Drop~{4}~Prepend~#[[4]]~FromDigits~2&/@ToCharacterCode@#~IntegerDigits~2]&

Ungolfed:

ToCharacterCode["Braille!0"]
PadLeft@IntegerDigits[%,2]
Prepend[Drop[#,{4}],#[[4]]]&/@%
FromDigits[#,2]&/@%
FromCharacterCode[%+10240]

+60 bytes of this tied up in long function names.

Kelly Lowder

Posted 2017-04-21T08:13:42.940

Reputation: 3 225

1

Jelly, 21 bytes

O&€“¬®p‘æ.1,8,.+“'ṁ’Ọ

Try it online!

How it works

O&€“¬®p‘æ.1,8,.+“'ṁ’Ọ  Main link. Argument: s (string)

O                      Ordinal; map all characters to their Unicode code points.
   “¬®p‘               Yield the code points of the enclosed characters in Jelly's
                       code page, i.e., [1, 8, 112].
 &€                    Take the bitwise AND of each code point to the left and the
                       three code points to the right.
          1,8,.        Yield [1, 8, 0.5].
        æ.             Take the dot product of the array to the right and each flat
                       array in the array to the left.
                “'ṁ’   Yield 10240 = 250 × 39 + 239, where 39 and 239 are the
                       indices of ' and ṁ in Jelly's code page.
               +       Add 10240 to all integers to the left.
                    Ọ  Unordinal; convert all code points to their respective 
                       Unicode charcters.

Dennis

Posted 2017-04-21T08:13:42.940

Reputation: 196 637

0

Retina, 59 bytes

T`-- -'0-7@-GP-W\`-gp-w--(-/8-?H-OX-_h-ox-`⠀-⡿

Try it online! Hex dump:

0000  54 60 00 2a 07 10 2a 17  20 2a 17 30 2a 17 40 2a  T`-- -'0-7@-
0010  47 50 2a 57 5c 60 2a 67  70 2a 77 08 2a 0f 18 2a  GP-W\`-gp-w--
0020  1f 28 2a 2f 38 2a 3f 48  2a 4f 58 2a 5f 68 2a 6f  (-/8-?H-OX-_h-o
0030  78 2a 7f 60 e2 a0 80 2a  e2 a1 bf                 x-`⠀-⡿

Neil

Posted 2017-04-21T08:13:42.940

Reputation: 95 035

0

Chip, 62 59 bytes

h*
 Z~.
z.g+b
>xv<
||sf
Zx^<
Z< |
A/a/D
B/b
C/c
E/d
F/e
G/f

Try it online!

I suspect I can golf it more, just gotta figure out how...

Chip reads in each byte of input as a collection of bits, referred to by the first eight letters of the alphabet (uppercase is input, lower is output):

HGFEDCBA

We simply need to map those bits of the input to the following three bytes of output:

11100010 101000hd 10gfecba

The top half of the code is doing all the sequencing, and generates the first two bytes, the bottom half generates the third byte.

Since the specification only requires handling 7 bits for ASCII, we don't examine H. To include the eighth bit, change line B/b to B/b/H.

Phlarx

Posted 2017-04-21T08:13:42.940

Reputation: 1 366

0

C#, 63 bytes

s=>string.Concat(s.Select(c=>(char)(c*8&64|c/2&56|c&7|10240)));

Try it online!

Inspiration from @ovs and @Neil

aloisdg moving to codidact.com

Posted 2017-04-21T08:13:42.940

Reputation: 1 767