Skip to content

Instantly share code, notes, and snippets.

@lukealbao
Created October 9, 2015 20:38
Show Gist options
  • Save lukealbao/41c514238962290b4759 to your computer and use it in GitHub Desktop.
Save lukealbao/41c514238962290b4759 to your computer and use it in GitHub Desktop.
/*
* A conversion of the hex2ebcdic.js conversion utility. The original
* exported a function that used a big switch statement. Now it is
* a hash table that will be used by a single `encode` function.
*
* Author: Luke Albao <[email protected]>
* Copyright: First Performance Corporation
*/
// To run this from the command line:
// $ node hexToEbcdic.js <paste hex string here>
function hexToEbcdic (data) {
var str = '';
for (var b = 0, l = data.length; b < l; b++) {
str += hexToEbcdicLookup[data[b]]
|| String.fromCharCode(data[b]);
}
return str;
}
var hexToEbcdicLookup = {
0: "<NUL>",
1: "<SOH>",
2: "<STX>",
3: "<ETX>",
4: "<SEL>",
5: "<HT>",
6: "<RNL>",
7: "<DEL>",
8: "<GE>",
9: "<SPS>",
10: "<RPT>",
11: "<VT>",
12: "<FF>",
13: "chr(13)",
14: "<SO>",
15: "<SI>",
16: "<DLE>",
17: "<DC1>",
18: "<DC2>",
19: "<DC3>",
20: "<RES/ENP>",
21: "<NL>",
22: "<BS>",
23: "<POC>",
24: "<CAN>",
25: "<EM>",
26: "<UBS>",
27: "<CU1>",
28: "<IFS>",
29: "<IGS>",
30: "<IRS>",
31: "<ITB/IUS>",
32: "<DS>",
33: "<SOS>",
34: "<FS>",
35: "<WUS>",
36: "<BYP/INP>",
37: "<LF>",
38: "<ETB>",
39: "<ESC>",
40: "<SA>",
41: "<SFE>",
42: "<SM/SW>",
43: "<CSP>",
44: "<MFA>",
45: "<ENQ>",
46: "<ACK>",
47: "<BEL>",
48: null,
49: null,
50: "<SYN>",
51: "<IR>",
52: "<PP>",
53: "<TRN>",
54: "<NBS>",
55: "<EOT>",
56: "<SBS>",
57: "<IT>",
58: "<RFF>",
59: "<CU3>",
60: "<DC4>",
61: "<NAK>",
62: null,
63: "<SUB>",
64: " ",
65: "<RSP>",
66: null,
67: null,
68: null,
69: null,
70: null,
71: null,
72: null,
73: null,
74: "[",
75: ".",
76: "<",
77: "(",
78: "+",
79: "!",
80: "&",
81: null,
82: null,
83: null,
84: null,
85: null,
86: null,
87: null,
88: null,
89: null,
90: "]",
91: "$",
92: "*",
93: ")",
94: ";",
95: "^",
96: "_",
97: "/",
98: null,
99: null,
100: null,
101: null,
102: null,
103: null,
104: null,
105: null,
106: "|",
107: ",",
108: "%",
109: "_",
110: ">",
111: "?",
112: null,
113: null,
114: null,
115: null,
116: null,
117: null,
118: null,
119: null,
120: null,
121: "`",
122: ":",
123: "#",
124: "@",
125: "'",
126: "=",
127: "\"",
128: null,
129: "a",
130: "b",
131: "c",
132: "d",
133: "e",
134: "f",
135: "g",
136: "h",
137: "i",
138: null,
139: "{",
140: null,
141: null,
142: null,
143: "+",
144: null,
145: "j",
146: "k",
147: "l",
148: "m",
149: "n",
150: "o",
151: "p",
152: "q",
153: "r",
154: null,
155: "}",
156: null,
157: null,
158: null,
159: null,
160: null,
161: "~",
162: "s",
163: "t",
164: "u",
165: "v",
166: "w",
167: "x",
168: "y",
169: "z",
170: null,
171: null,
172: null,
173: "[",
174: null,
175: null,
176: null,
177: null,
178: null,
179: null,
180: null,
181: null,
182: null,
183: null,
184: null,
185: null,
186: null,
187: null,
188: null,
189: null,
190: null,
191: null,
192: "{",
193: "A",
194: "B",
195: "C",
196: "D",
197: "E",
198: "F",
199: "G",
200: "H",
201: "I",
202: null,
203: null,
204: null,
205: null,
206: null,
207: null,
208: "}",
209: "J",
210: "K",
211: "L",
212: "M",
213: "N",
214: "O",
215: "P",
216: "Q",
217: "R",
218: null,
219: null,
220: null,
221: null,
222: null,
223: null,
224: "\\",
225: null,
226: "S",
227: "T",
228: "U",
229: "V",
230: "W",
231: "X",
232: "Y",
233: "Z",
234: null,
235: null,
236: null,
237: null,
238: null,
239: null,
240: "0",
241: "1",
242: "2",
243: "3",
244: "4",
245: "5",
246: "6",
247: "7",
248: "8",
249: "9",
250: null,
251: null,
252: null,
253: null,
254: null,
255: null
}
if (!module.parent) {
var inputString = process.argv[2];
var inputBuffer = new Buffer(inputString, 'hex');
console.log(hexToEbcdic(inputBuffer));
}
module.exports = hexToEbcdic;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment