Skip to content

Instantly share code, notes, and snippets.

@chriskillpack
Last active March 9, 2023 23:07
Show Gist options
  • Save chriskillpack/27c770a7ef036ed37500ce53f714d947 to your computer and use it in GitHub Desktop.
Save chriskillpack/27c770a7ef036ed37500ce53f714d947 to your computer and use it in GitHub Desktop.
Convert the box drawing characters of PC codepage 437 to Unicode
/*
Convert the box drawing characters of PC codepage 437 to Unicode
and encode as UTF-8.
If no argument is provided then program reads from stdin. These are equivalent:
437toutf8 437in.txt > utf8out.txt
437toutf8 < 437in.txt > utf8out.txt
*/
package main
import (
"bufio"
"log"
"os"
)
// Just the box drawing characters for now
var cp437 = map[byte]rune{
169: '⌐',
170: '¬',
174: '«',
175: '»',
176: '░',
177: '▒',
178: '▓',
179: '│',
180: '┤',
181: '╡',
182: '╢',
183: '╖',
184: '╕',
185: '╣',
186: '║',
187: '╗',
188: '╝',
189: '╜',
190: '╛',
191: '┐',
192: '└',
193: '┴',
194: '┬',
195: '├',
196: '─',
197: '┼',
198: '╞',
199: '╟',
200: '╚',
201: '╔',
202: '╩',
203: '╦',
204: '╠',
205: '═',
206: '╬',
207: '╧',
208: '╨',
209: '╤',
210: '╥',
211: '╙',
212: '╘',
213: '╒',
214: '╓',
215: '╫',
216: '╪',
217: '┘',
218: '┌',
219: '█',
220: '▄',
221: '▌',
222: '▐',
223: '▀',
254: '■',
}
func main() {
r := bufio.NewReader(os.Stdin)
if len(os.Args) > 1 {
rf, err := os.Open(os.Args[1])
if err != nil {
log.Fatal(err)
}
r = bufio.NewReader(rf)
}
w := bufio.NewWriter(os.Stdout)
for {
if b, err := r.ReadByte(); err == nil {
if r, ok := cp437[b]; ok {
w.WriteRune(r)
} else {
w.WriteByte(b)
}
} else {
break
}
}
w.Flush()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment