0

can anyone help me to decode data matrix using C40 encoding technique? One thing i found from net about c40 is :

Sample, sequence to encode with C40 method : Ab

The 3 characters are : 14, 02, 02
14 * 1600 + 2 * 40 + 2 + 1 = 22 483
CW1 = 22 483 \ 256 = 87
CW2 = 22 483 MOD 256 = 211
The sequence is consequently : 87, 211

this is encoding but i want to decode using c40 in visual c++ . have anyone idea about that? Thank you in advance.

Krishi
  • 1
  • 1
  • 5

2 Answers2

1

Reversing it like this might work.

87 * 256 = 22 272
22 272 + 211 = 22 483

CW1 * 1600 + CW2 * 40 + CW3 + 1 = 22 483
CW1 * 1600 + CW2 * 40 + CW3 = 22 482
40 * (CW1 * 40 + CW2) + CW3 = 22 482

=> CW3 = 22 482 mod 40 = 2

40 * (CW1 * 40 + CW2) = 22 480

CW1 * 40 + CW2 = 562

=> CW2 = 562 mod 40 = 2

CW1 * 40 = 560

=> CW1 = 14

Or,

code = CW1 * 256 + CW2
c1 = code - 1
CW3 = c1 mod 40
c2 = c1 - CW3
CW2 = (c2 / 40) mod 40
c3 = c2 - CW2
CW1 = c3 / 40
molbdnilo
  • 64,751
  • 3
  • 43
  • 82
1

This is pseudo-code to convert two C40 encoded bytes (i1,i2) back to their ASCII code numbers. Even though pseudo-code, it should be easy to implement it in any language.

INPUT:  Two bytes (i1,i2) resulting from C40 encoding
OUTPUT: Array [] of decoded ASCII characters

if i1 == 0xFE then // if i1 is unlatch 0xFE, then i2 is char encoded as ASCII + 1
     return [char(i2-1)]
else  // below '/' denotes integer division
     val16 = (i1*256) + i2
     u1    = (val16-1) / 1600
     u2    = (val16 - (u1*1600)) / 40
     u3    = v16 - (u1*1600) - (u2*40) - 1
     // u1,u2,u3 are the chars with their C40 code numbers
     // now convert them back to ASCII code numbers
     ascii_chars = []
     for u in [u1,u2,u3] do
         if u == 3 then // C40 3 is space, add 32 to get ascii value
             ascii_chars.append(chr(32))
         else if u >=4 and u <= 13 then // C40 number range, add 40 to get ascii value
             ascii_chars.append(chr(u+44))
         else if u >= 14 and u <= 39 then // C40 range A-Z, add 51 to get ascii value
             ascii_chars.append(chr(u+51))
     return ascii_chars 
ndbd
  • 2,417
  • 3
  • 23
  • 32