网上下的用C#写的code128的生成代码,一些问题请教。

beiguoyouzi 2009-02-20 10:07:37
private int[] StringToCode128(string AsciiData) //为什么要把从键盘输入的字符,如"1234ABCD"做一下转换,希望高人把这下面的代码大概说一下。
{
// turn the string into ascii byte data
byte[] asciiBytes = Encoding.ASCII.GetBytes(AsciiData);

// decide which codeset to start with,
Code128Code.CodeSetAllowed csa1 = asciiBytes.Length > 0 ? Code128Code.CodesetAllowedForChar(asciiBytes[0]) : Code128Code.CodeSetAllowed.CodeAorB;
Code128Code.CodeSetAllowed csa2 = asciiBytes.Length > 0 ? Code128Code.CodesetAllowedForChar(asciiBytes[1]) : Code128Code.CodeSetAllowed.CodeAorB;
CodeSet currcs = GetBestStartSet(csa1, csa2);

// set up the beginning of the barcode,
//使用大小可按需动态增加的数组实现 IList 接口。
//长度+3
System.Collections.ArrayList codes = new System.Collections.ArrayList(asciiBytes.Length + 3); // assume no codeset changes, account for start, checksum, and stop
codes.Add(Code128Code.StartCodeForCodeSet(currcs)); //设置起始点

// add the codes for each character in the string
for (int i = 0; i < asciiBytes.Length; i++)
{
int thischar = asciiBytes[i];
int nextchar = asciiBytes.Length > (i + 1) ? asciiBytes[i + 1] : -1;

codes.AddRange(Code128Code.CodesForChar(thischar, nextchar, ref currcs));
}

// calculate the check digit
int checksum = (int)(codes[0]);
for (int i = 1; i < codes.Count; i++)
{
checksum += i * (int)(codes[i]);
}
codes.Add(checksum % 103);

codes.Add(Code128Code.StopCode());

int[] result = codes.ToArray(typeof(int)) as int[];
return result;
}

还有一处:
width = ((codes.Length - 3) * 11 + 35) * BarWeight; //这个条码显示宽度为什么要-3然后*11,再+35,具体什么含义。

如果大侠们能留个qq号更好。
谢谢。
...全文
348 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
dalenyu 2009-03-06
  • 打赏
  • 举报
回复
看不懂!谁有Pb的
zgke 2009-02-20
  • 打赏
  • 举报
回复
for (int i = 0; i < asciiBytes.Length; i++)
{
int thischar = asciiBytes[i];
int nextchar = asciiBytes.Length > (i + 1) ? asciiBytes[i + 1] : -1;

codes.AddRange(Code128Code.CodesForChar(thischar, nextchar, ref currcs));
}

int checksum = (int)(codes[0]);
for (int i = 1; i < codes.Count; i++)
{
checksum += i * (int)(codes[i]);
}
codes.Add(checksum % 103);

//这些是计算验效码

关于128你可以参考下面的。
http://blog.csdn.net/zgke/archive/2008/12/10/3488866.aspx
lsj_zrp 2009-02-20
  • 打赏
  • 举报
回复
width = ((codes.Length - 3) * 11 + 35) * BarWeight
每个条码字符由6个单元11个模块组成,所以要乘11
-3是因为每个条码都有起始符,校验符,终止符,所以会-3
至于为什么要+35,这点也看不明白

你那些代码就是添加起始符,数据,校验符,终止符
//选择字符集
Code128Code.CodeSetAllowed csa1 = asciiBytes.Length > 0 ? Code128Code.CodesetAllowedForChar(asciiBytes[0]) : Code128Code.CodeSetAllowed.CodeAorB;
Code128Code.CodeSetAllowed csa2 = asciiBytes.Length > 0 ? Code128Code.CodesetAllowedForChar(asciiBytes[1]) : Code128Code.CodeSetAllowed.CodeAorB;
CodeSet currcs = GetBestStartSet(csa1, csa2);
//添加起始点
System.Collections.ArrayList codes = new System.Collections.ArrayList(asciiBytes.Length + 3); // assume no codeset changes, account for start, checksum, and stop
codes.Add(Code128Code.StartCodeForCodeSet(currcs)); //设置起始点

//添加数据
for (int i = 0; i < asciiBytes.Length; i++)
{
int thischar = asciiBytes[i];
int nextchar = asciiBytes.Length > (i + 1) ? asciiBytes[i + 1] : -1;

codes.AddRange(Code128Code.CodesForChar(thischar, nextchar, ref currcs));
}

//添加校验位
int checksum = (int)(codes[0]);
for (int i = 1; i < codes.Count; i++)
{
checksum += i * (int)(codes[i]);
}
codes.Add(checksum % 103);
//添加终止符
codes.Add(Code128Code.StopCode());
beiguoyouzi 2009-02-20
  • 打赏
  • 举报
回复
你给我的那个网页更不好懂。

110,535

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧