As @jxh proposed, a static "look up table" is the solution.
Based on 7-bit ASCII, this is 128 entries with A-Z, a-z, 0-9, SP and ',' filled in. A good start... You can carefully add more punctuation to the table as you need. (Notice that '&' still translates to '00'.)
SP comes back as 20... More code might just output a blank instead of translating it to the ASCII hex value.
Some characters will need more complexity, translating to multiple Brail characters, but this is a start. Also "shifting" in/out of digits is left for you to add.
(The hex values filling this table have been copied from your question. I've not checked each & every value as being correct.)
unsigned int brail_print( char c ) {
unsigned char brail[] = {
// CTRL codes
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
// CTRL codes
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
// punctuation
0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
// punctuation & digits
0x00, 0x01, 0x03, 0x09, 0x19, 0x11, 0x0B, 0x1B, 0x13, 0x0A, 0x1A, 0x00, 0x00, 0x00, 0x00, 0x00,
// @ + A-O
0x00, 0x01, 0x03, 0x09, 0x19, 0x11, 0x0B, 0x1B, 0x13, 0x0A, 0x1A, 0x05, 0x07, 0x0D, 0x1D, 0x15,
// P-Z + braces
0x0F, 0x1F, 0x17, 0x0E, 0x1E, 0x25, 0x27, 0x3A, 0x2D, 0x3D, 0x35, 0x00, 0x00, 0x00, 0x00, 0x00,
// ` + a-o
0x00, 0x01, 0x03, 0x09, 0x19, 0x11, 0x0B, 0x1B, 0x13, 0x0A, 0x1A, 0x05, 0x07, 0x0D, 0x1D, 0x15,
// p-z + braces
0x0F, 0x1F, 0x17, 0x0E, 0x1E, 0x25, 0x27, 0x3A, 0x2D, 0x3D, 0x35, 0x00, 0x00, 0x00, 0x00, 0x00,
};
return brail[ c ];
}
int main() {
char str[] = "The Cat sat, & purred, on the Mat. 123";
int i;
for( i = 0; str[ i ]; i++ )
printf( "%c ", str[ i ] );
putchar( '\n' );
for( i = 0; str[ i ]; i++ )
printf( "%02X ", brail_print( str[ i ] ) );
putchar( '\n' );
return (0);
}
T h e C a t s a t , & p u r r e d , o n t h e M a t . 1 2 3
1E 13 11 20 09 01 1E 20 0E 01 1E 02 20 00 20 0F 25 17 17 11 19 02 20 15 1D 20 1E 13 11 20 0D 01 1E 00 20 01 03 09
EDIT:
Since the Brail character set is composed of two columns of 3 pins, you may make your life easier dealing with octal
representations, rather than hexadecimal
. For your consideration:
#include <stdio.h>
unsigned int brail_printOCT( char c ) {
unsigned char oct[] = {
// CTRL codes
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
// CTRL codes
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
// punctuation
040, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 002, 0, 0, 0,
// punctuation & digits
0, 001, 003, 011, 031, 021, 013, 033, 023, 012, 032, 0, 0, 0, 0, 0,
// @ + A-O
0, 001, 003, 011, 031, 021, 013, 033, 023, 012, 032, 005, 007, 015, 035, 025,
// P-Z + braces
017, 037, 027, 016, 036, 045, 047, 072, 055, 075, 065, 0, 0, 0, 0, 0,
// ` + a-o
0, 001, 003, 011, 031, 021, 013, 033, 023, 012, 032, 005, 007, 015, 035, 025,
// p-z + braces
017, 037, 027, 016, 036, 045, 047, 072, 055, 075, 065, 0, 0, 0, 0, 0,
};
return oct[ c ];
}
int main() {
char str[] = "AaBbCc xyz , & 123";
int i;
for( i = 0; str[ i ]; i++ )
printf( "%4c ", str[ i ] );
putchar( '\n' );
for( i = 0; str[ i ]; i++ )
printf( " %03o ", brail_printOCT( str[ i ] ) );
putchar( '\n' );
return (0);
}
Output
A a B b C c x y z , & 1 2 3
001 001 003 003 011 011 040 055 075 065 040 002 040 000 040 001 003 011
EDIT: In fact, knowing these are octal values, and that "left/right" are swapped in this representation, two octal digits map beautifully to the Brail alphabet.
S t u f f h a p p e n s
16 36 45 13 13 40 23 01 17 17 21 35 16