import { Buffer } from "buffer"; import _, {floor} from "lodash"; const statics: any = { 'ESC': '1B', 'FF': '0C' } const mmToInch = 0.0393701; // 26x51 label, or 1" x 2" const pageSize = getPageLengthParameters(2); const command = ` ESC i a 00h ESC @ ESC i L 01h ESC ( C 02h 00h ${pageSize.mL} ${pageSize.mH} ESC $ CBh 00h ESC ( V 02h 00h CBh 00h ESC k 0Bh ESC X 00h 64h 00h At your side FF `; console.warn('output: ', parseEscToHex(command)) function inchToDots(value: number, dpi = 203) { const dots = value * dpi; const margin = 6 * mmToInch * dpi; return Math.round(dots - margin); } function toHex(value: number) { return value.toString(16).padStart(2, '0') + 'h'; } function getPageLengthParameters(widthInInches: number) { const dots = inchToDots(widthInInches); const mH = Math.floor(dots / 265); const mL = dots - mH * 265; return { mH: toHex(mH), mL: toHex(mL) } } function parseEscToHex(text: string): string { return text.trim().split(/\s+/) .map(chunk => { if (_.has(statics, chunk)) { // Found static mapping return statics[chunk] } if(chunk.length == 3 && chunk.endsWith('h')) { // parse as hex return chunk.slice(0, -1) } return Buffer.from(chunk).toString('hex') }) .join(' ') }