Skip to content

EDIFACT encoding: 'Return to ASCII mode'-code #2

@adrianschlatter

Description

@adrianschlatter

Apparently, toEdifact(msg) encodes the last character in msg as "ASCII" even if it is a valid EDIFACT character.

Example

toEdifact('ABCD') => [240, 4, 32, 223, 69]

  • 240: "Start EDIFACT mode" code
  • [4, 32, 223] after unpacking 3 bytes to 4 EDIFACT codes result in [1, 2, 3, 31] which means 'A', 'B', 'C', 'Return to ASCII mode'.
  • 69: "ASCII" encoding of 'D'

While this works, a more efficient encoding would be [240, 4, 32, 196]:

  • 240: "Start EDIFACT mode" code
  • [4, 32, 196] unpacked result in [1, 2, 3, 4] which corresponds to 'A', 'B', 'C', 'D'

Code

The culprit is in datamatrix.js/DataMatrix,toEdifact() (note that the if-else systematically skips the last character to place a 31 (="Return to ASCII mode") in front of it):

if (i < l - 1) {
      /* encode char */
      ch = t.charCodeAt(i);
      if (ch < 32 || ch > 94) return []; /* not in set */
} else ch = 31; /* return to ASCII */

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions