欢迎您访问 最编程 本站为您分享编程语言代码,编程技术文章!
您现在的位置是: 首页

C语言实现的Code128编码

最编程 2024-08-14 16:45:27
...
//code128编码值数组,,每一个字符占11个单位表示,代码中用两个byte表示(取后11位bit)
static
uint16_t s_code128_encode_set[] ={ 0x6cc,0x66c,0x666,0x498,0x48c,0x44c,0x4c8,0x4c4,0x464,0x648, 0x644,0x624,0x59c,0x4dc,0x4ce,0x5cc,0x4ec,0x4e6,0x672,0x65c, 0x64e,0x6e4,0x674,0x76e,0x74c,0x72c,0x726,0x764,0x734,0x732, 0x6d8,0x6c6,0x636,0x518,0x458,0x446,0x588,0x468,0x462,0x688, 0x628,0x622,0x5b8,0x58e,0x46e,0x5d8,0x5c6,0x476,0x776,0x68e, 0x62e,0x6e8,0x6e2,0x6ee,0x758,0x746,0x716,0x768,0x762,0x71a, 0x77a,0x642,0x78a,0x530,0x50c,0x4b0,0x486,0x42c,0x426,0x590, 0x584,0x4d0,0x4c2,0x434,0x432,0x612,0x650,0x7ba,0x614,0x47a, 0x53c,0x4bc,0x49e,0x5e4,0x4f4,0x4f2,0x7a4,0x794,0x792,0x6de, 0x6f6,0x7b6,0x578,0x51e,0x45e,0x5e8,0x5e2,0x7a8,0x7a2,0x5de, 0x5ee,0x75e,0x7ae,0x684,0x690,0x69c,0x18EB };

#define CODE128_MEX_LEN 30
static uint16_t code128_temp[CODE128_MEX_LEN] = {0};
#define CODE128_ONE_ELEMENT_SIZE 11

STATUS_TYPE code128C_encode(const uint8_t *src,uint8_t length,uint8_t *out)
{
int i = 0;
int j = 0;
uint8_t chr_temp = 0;
uint32_t check_digit = 0;
uint16_t *p_out = code128_temp;
memset(code128_temp,0,CODE128_MEX_LEN);

p_out[0] = 0x69c;
p_out++;
check_digit += 105;

for(i = 0,j = 0;i < length;i += 2,j++ )
{
if(src[i] < '0' || src[i] > '9' || src[i+1] < '0' || src[i+1] > '9')
{
return STATUS_INVALID_CHAR;
}

chr_temp = (src[i] - '0')*10 + src[i + 1] - '0';
check_digit += (j + 1)*chr_temp;

p_out[j] = s_code128_encode_set[chr_temp];

}

check_digit %=103;

p_out[j] = s_code128_encode_set[check_digit];
p_out[j+1] = 0x18EB;

return STATUS_OK;
}

 

 

推荐阅读