convert.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. package main
  2. // I used iconv in door++, but it was horrible. It doesn't translate
  3. // control codes to Unicode.
  4. // This converts CP437 to Unicode for rendering in Unicode terminals
  5. // like the linux telnet client.
  6. // This does everything but: \x07, \x08, \x0a and \x0d.
  7. // 0x07, 0x08, 0x09, 0x0a, 0x0c, 0x0d, 0x1b (ANSI spec)
  8. func CP437_to_Unicode(cp437 string) string {
  9. var result string
  10. for _, char := range []byte(cp437) {
  11. // for _, char := range []rune(cp437) {
  12. switch int(char) {
  13. case 0x01:
  14. result += "\u263A"
  15. case 0x02:
  16. result += "\u263B"
  17. case 0x03:
  18. result += "\u2665"
  19. case 0x04:
  20. result += "\u2666"
  21. case 0x05:
  22. result += "\u2663"
  23. case 0x06:
  24. result += "\u2660"
  25. case 0x09:
  26. result += "\u25CB"
  27. case 0x0b:
  28. result += "\u2642"
  29. case 0x0c:
  30. result += "\u2640"
  31. case 0x0e:
  32. result += "\u266B"
  33. case 0x0f:
  34. result += "\u263C"
  35. case 0x10:
  36. result += "\u25BA"
  37. case 0x11:
  38. result += "\u25C4"
  39. case 0x12:
  40. result += "\u2195"
  41. case 0x13:
  42. result += "\u203C"
  43. case 0x14:
  44. result += "\xc2\xb6"
  45. case 0x15:
  46. result += "\xc2\xa7"
  47. case 0x16:
  48. result += "\u25AC"
  49. case 0x17:
  50. result += "\u21A8"
  51. case 0x18:
  52. result += "\u2191"
  53. case 0x19:
  54. result += "\u2193"
  55. case 0x1a:
  56. result += "\u2192"
  57. case 0x1c:
  58. result += "\u221F"
  59. case 0x1d:
  60. result += "\u2194"
  61. case 0x1e:
  62. result += "\u25B2"
  63. case 0x1f:
  64. result += "\u25BC"
  65. case 0x7f:
  66. result += "\u2302"
  67. case 0x80:
  68. result += "\xc3\x87"
  69. case 0x81:
  70. result += "\xc3\xbc"
  71. case 0x82:
  72. result += "\xc3\xa9"
  73. case 0x83:
  74. result += "\xc3\xa2"
  75. case 0x84:
  76. result += "\xc3\xa4"
  77. case 0x85:
  78. result += "\xc3\xa0"
  79. case 0x86:
  80. result += "\xc3\xa5"
  81. case 0x87:
  82. result += "\xc3\xa7"
  83. case 0x88:
  84. result += "\xc3\xaa"
  85. case 0x89:
  86. result += "\xc3\xab"
  87. case 0x8a:
  88. result += "\xc3\xa8"
  89. case 0x8b:
  90. result += "\xc3\xaf"
  91. case 0x8c:
  92. result += "\xc3\xae"
  93. case 0x8d:
  94. result += "\xc3\xac"
  95. case 0x8e:
  96. result += "\xc3\x84"
  97. case 0x8f:
  98. result += "\xc3\x85"
  99. case 0x90:
  100. result += "\xc3\x89"
  101. case 0x91:
  102. result += "\xc3\xa6"
  103. case 0x92:
  104. result += "\xc3\x86"
  105. case 0x93:
  106. result += "\xc3\xb4"
  107. case 0x94:
  108. result += "\xc3\xb6"
  109. case 0x95:
  110. result += "\xc3\xb2"
  111. case 0x96:
  112. result += "\xc3\xbb"
  113. case 0x97:
  114. result += "\xc3\xb9"
  115. case 0x98:
  116. result += "\xc3\xbf"
  117. case 0x99:
  118. result += "\xc3\x96"
  119. case 0x9a:
  120. result += "\xc3\x9c"
  121. case 0x9b:
  122. result += "\xc2\xa2"
  123. case 0x9c:
  124. result += "\xc2\xa3"
  125. case 0x9d:
  126. result += "\xc2\xa5"
  127. case 0x9e:
  128. result += "\u20A7"
  129. case 0x9f:
  130. result += "\u0192"
  131. case 0xa0:
  132. result += "\xc3\xa1"
  133. case 0xa1:
  134. result += "\xc3\xad"
  135. case 0xa2:
  136. result += "\xc3\xb3"
  137. case 0xa3:
  138. result += "\xc3\xba"
  139. case 0xa4:
  140. result += "\xc3\xb1"
  141. case 0xa5:
  142. result += "\xc3\x91"
  143. case 0xa6:
  144. result += "\xc2\xaa"
  145. case 0xa7:
  146. result += "\xc2\xba"
  147. case 0xa8:
  148. result += "\xc2\xbf"
  149. case 0xa9:
  150. result += "\u2310"
  151. case 0xaa:
  152. result += "\xc2\xac"
  153. case 0xab:
  154. result += "\xc2\xbd"
  155. case 0xac:
  156. result += "\xc2\xbc"
  157. case 0xad:
  158. result += "\xc2\xa1"
  159. case 0xae:
  160. result += "\xc2\xab"
  161. case 0xaf:
  162. result += "\xc2\xbb"
  163. case 0xb0:
  164. result += "\u2591"
  165. case 0xb1:
  166. result += "\u2592"
  167. case 0xb2:
  168. result += "\u2593"
  169. case 0xb3:
  170. result += "\u2502"
  171. case 0xb4:
  172. result += "\u2524"
  173. case 0xb5:
  174. result += "\u2561"
  175. case 0xb6:
  176. result += "\u2562"
  177. case 0xb7:
  178. result += "\u2556"
  179. case 0xb8:
  180. result += "\u2555"
  181. case 0xb9:
  182. result += "\u2563"
  183. case 0xba:
  184. result += "\u2551"
  185. case 0xbb:
  186. result += "\u2557"
  187. case 0xbc:
  188. result += "\u255D"
  189. case 0xbd:
  190. result += "\u255C"
  191. case 0xbe:
  192. result += "\u255B"
  193. case 0xbf:
  194. result += "\u2510"
  195. case 0xc0:
  196. result += "\u2514"
  197. case 0xc1:
  198. result += "\u2534"
  199. case 0xc2:
  200. result += "\u252C"
  201. case 0xc3:
  202. result += "\u251C"
  203. case 0xc4:
  204. result += "\u2500"
  205. case 0xc5:
  206. result += "\u253C"
  207. case 0xc6:
  208. result += "\u255E"
  209. case 0xc7:
  210. result += "\u255F"
  211. case 0xc8:
  212. result += "\u255A"
  213. case 0xc9:
  214. result += "\u2554"
  215. case 0xca:
  216. result += "\u2569"
  217. case 0xcb:
  218. result += "\u2566"
  219. case 0xcc:
  220. result += "\u2560"
  221. case 0xcd:
  222. result += "\u2550"
  223. case 0xce:
  224. result += "\u256C"
  225. case 0xcf:
  226. result += "\u2567"
  227. case 0xd0:
  228. result += "\u2568"
  229. case 0xd1:
  230. result += "\u2564"
  231. case 0xd2:
  232. result += "\u2565"
  233. case 0xd3:
  234. result += "\u2559"
  235. case 0xd4:
  236. result += "\u2558"
  237. case 0xd5:
  238. result += "\u2552"
  239. case 0xd6:
  240. result += "\u2553"
  241. case 0xd7:
  242. result += "\u256B"
  243. case 0xd8:
  244. result += "\u256A"
  245. case 0xd9:
  246. result += "\u2518"
  247. case 0xda:
  248. result += "\u250C"
  249. case 0xdb:
  250. result += "\u2588"
  251. case 0xdc:
  252. result += "\u2584"
  253. case 0xdd:
  254. result += "\u258C"
  255. case 0xde:
  256. result += "\u2590"
  257. case 0xdf:
  258. result += "\u2580"
  259. case 0xe0:
  260. result += "\u03B1"
  261. case 0xe1:
  262. result += "\xc3\x9f"
  263. case 0xe2:
  264. result += "\u0393"
  265. case 0xe3:
  266. result += "\u03C0"
  267. case 0xe4:
  268. result += "\u03A3"
  269. case 0xe5:
  270. result += "\u03C3"
  271. case 0xe6:
  272. result += "\xc2\xb5"
  273. case 0xe7:
  274. result += "\u03C4"
  275. case 0xe8:
  276. result += "\u03A6"
  277. case 0xe9:
  278. result += "\u0398"
  279. case 0xea:
  280. result += "\u03A9"
  281. case 0xeb:
  282. result += "\u03B4"
  283. case 0xec:
  284. result += "\u221E"
  285. case 0xed:
  286. result += "\u03C6"
  287. case 0xee:
  288. result += "\u03B5"
  289. case 0xef:
  290. result += "\u2229"
  291. case 0xf0:
  292. result += "\u2261"
  293. case 0xf1:
  294. result += "\xc2\xb1"
  295. case 0xf2:
  296. result += "\u2265"
  297. case 0xf3:
  298. result += "\u2264"
  299. case 0xf4:
  300. result += "\u2320"
  301. case 0xf5:
  302. result += "\u2321"
  303. case 0xf6:
  304. result += "\xc3\xb7"
  305. case 0xf7:
  306. result += "\u2248"
  307. case 0xf8:
  308. result += "\xc2\xb0"
  309. case 0xf9:
  310. result += "\u2219"
  311. case 0xfa:
  312. result += "\xc2\xb7"
  313. case 0xfb:
  314. result += "\u221A"
  315. case 0xfc:
  316. result += "\u207F"
  317. case 0xfd:
  318. result += "\xc2\xb2"
  319. case 0xfe:
  320. result += "\u25A0"
  321. case 0xff:
  322. result += "\xc2\xa0"
  323. default:
  324. result += string(char)
  325. }
  326. }
  327. return result
  328. }