extern void Code128Auto(char *src, char *res); extern void UCCEAN128(char *src, char *res); extern void Code128A(char *src, char *res); extern void Code128B(char *src, char *res); extern void Code128C(char *src, char *res); void GeneralEncode(char *src, char *res, int StartIndex, int StartWeight); void GeneralEncode(char *src, char *res, int StartIndex, int StartWeight, int StartSum, int StartSet) { int tmp; int i; bool CurrDone; int StrLen; char CurrChar; char NextChar; int index = StartIndex; int Weight = StartWeight; int Sum = StartSum; int CurrSet = StartSet; StrLen = strlen(src); i = 0; while (i < StrLen) { CurrChar = src[i]; CurrDone = false; if ((i + 1) < StrLen) { NextChar = src[i + 1]; if (CurrChar >= '0' && CurrChar <= '9' && NextChar >= '0' && NextChar <= '9') { tmp = (CurrChar - '0') * 10 + (NextChar - '0'); // 2 digits if (CurrSet != 3) { // the previous set is not Set C res[index++] = (char)(99 + 98); Sum = Sum + Weight * 99; Weight = Weight + 1; CurrSet = 3; } if (tmp == 0) res[index++] = (char)192; else if (tmp > 0 && tmp < 95) res[index++] = (char)(tmp + 32); else res[index++] = (char)(tmp + 98); Sum = Sum + Weight * tmp; i = i + 2; CurrDone = true; } } if (!CurrDone) { if (CurrChar >= 0 && CurrChar <= 31) { // choose Set A if (CurrSet != 1) { // the previous set is not Set A res[index++] = (char)(101 + 98); Sum = Sum + Weight * 101; Weight = Weight + 1; CurrSet = 1; } if (CurrChar == 31) { res[index++] = (char)193; Sum = Sum + Weight * 95; } else { res[index++] = (char)(CurrChar + 96); Sum = Sum + Weight * (CurrChar + 64); } } else { // choose Set B if (CurrSet != 2) { // the previous set is not Set B res[index++] = (char)(100 + 98); Sum = Sum + Weight * 100; Weight = Weight + 1; CurrSet = 2; } if (CurrChar == 32) { res[index++] = (char)192; } else if (CurrChar == 127) { res[index++] = (char)193; Sum = Sum + Weight * 95; } else if (CurrChar < 127 && CurrChar > 32) { res[index++] = (char)CurrChar; Sum = Sum + Weight * (CurrChar - 32); } } i = i + 1; } Weight = Weight + 1; } // add CheckDigit Sum = Sum % 103; if (Sum == 0) res[index++] = (char)192; else if (Sum <= 94) res[index++] = (char)(Sum + 32); else res[index++] = (char)(Sum + 98); // add stop character (204) res[index++] = (char)204; res[index++] = '\0'; } extern void Code128Auto(char *src, char *res) { int Sum; int CurrSet; int Weight; char Message[256]; Sum = 104; // 2 indicates Set B CurrSet = 2; // start character with value 202 for Set B Message[0] = (char)202; char CurrChar = src[0]; if (CurrChar <= 31 && CurrChar >= 0) { // switch to Set A // 1 indicates Set A CurrSet = 1; // start character with value 201 for Set A Message[0] = (char)201; Sum = 103; } Weight = 1; GeneralEncode(src, Message, 1, Weight, Sum, CurrSet); strcpy(res, Message); } extern void UCCEAN128(char *src, char *res) { int Sum; int CurrSet; int Weight; char Message[256]; Sum = 105; // 3 indicates Set C CurrSet = 3; // start character (203) + FNC1 (200) Message[0] = (char)203; Message[1] = (char)200; Sum = Sum + 102; Weight = 2; GeneralEncode(src, Message, 2, Weight, Sum, CurrSet); strcpy(res, Message); } extern void Code128A(char *src, char *res) { int StrLen; char CurrChar; char Message[256]; int index; int Weight; int Sum; StrLen = strlen(src); Sum = 103; // start character (201) for Set A index = 0; res[index++] = (char)201; Weight = 1; for (int i = 0 ; i < StrLen; i++) { CurrChar = src[i]; if (CurrChar == 32) res[index++] = (char)192; else if (CurrChar == 31) { res[index++] = (char)193; Sum = Sum + Weight * 95; } else if (CurrChar <= 95 && CurrChar > 32) { res[index++] = (char)CurrChar; Sum = Sum + Weight * (CurrChar - 32); } else if (CurrChar >= 0 && CurrChar <= 31) { res[index++] = (char)(CurrChar + 96); Sum = Sum + Weight * (CurrChar + 64); } else { Code128Auto(src, Message); strcpy(res, Message); return; } Weight = Weight + 1; } // add CheckDigit Sum = Sum % 103; if (Sum == 0) res[index++] = (char)192; else if (Sum <= 94) res[index++] = (char)(Sum + 32); else res[index++] = (char)(Sum + 98); // add stop character (204) res[index++] = (char)204; res[index++] = '\0'; } extern void Code128B(char *src, char *res) { int StrLen; char CurrChar; char Message[256]; int index; int Weight; int Sum; StrLen = strlen(src); Sum = 104; // start character (202) for Set B index = 0; res[index++] = (char)202; Weight = 1; for (int i = 0; i < StrLen; i++) { CurrChar = src[i]; if (CurrChar == 32) res[index++] = (char)192; else if (CurrChar == 127) { res[index++] = (char)193; Sum = Sum + Weight * 95; } else if (CurrChar < 127 && CurrChar > 32) { res[index++] = (char)CurrChar; Sum = Sum + Weight * (CurrChar - 32); } else { Code128Auto(src, Message); strcpy(res, Message); return; } Weight = Weight + 1; } // add CheckDigit Sum = Sum % 103; if (Sum == 0) res[index++] = (char)192; else if (Sum <= 94) res[index++] = (char)(Sum + 32); else res[index++] = (char)(Sum + 98); // add stop character (204) res[index++] = (char)204; res[index++] = '\0'; } extern void Code128C(char *src, char *res) { int tmp; int i; int StrLen; char CurrChar; char NextChar; char Message[256]; int index; int Weight; int Sum; StrLen = strlen(src); Sum = 105; // start character (203) for Set C index = 0; res[index++] = (char)203; Weight = 1; i = 0; while (i < StrLen) { CurrChar = src[i]; if ((i + 1) < StrLen) { NextChar = src[i + 1]; if (CurrChar >= '0' && CurrChar <= '9' && NextChar >= '0' && NextChar <= '9') { //2 digits tmp = (CurrChar - '0') * 10 + (NextChar - '0'); if (tmp == 0) res[index++] = (char)192; else if (tmp > 0 && tmp < 95) res[index++] = (char)(tmp + 32); else res[index++] = (char)(tmp + 98); Sum = Sum + Weight * tmp; i = i + 2; } else { Code128Auto(src, Message); strcpy(res, Message); return; } } else { res[index++] = (char)198; Sum = Sum + Weight * 100; Weight = Weight + 1; if (CurrChar == 32) res[index++] = (char)192; else if (CurrChar == 127) { res[index++] = (char)193; Sum = Sum + Weight * 95; } else if (CurrChar < 127 && CurrChar > 32) { res[index++] = (char)CurrChar; Sum = Sum + Weight * (CurrChar - 32); } else { Code128Auto(src, Message); strcpy(res, Message); return; } i = i + 1; } Weight = Weight + 1; } // add CheckDigit Sum = Sum % 103; if (Sum == 0) res[index++] = (char)192; else if (Sum <= 94) res[index++] = (char)(Sum + 32); else res[index++] = (char)(Sum + 98); // add stop character (204) res[index++] = (char)204; res[index++] = '\0'; }