最新消息:重新回归WordPress,我要比较认真的开始更新我的博客了。

条码和图书ISBN相互转换的方法

程序问题 hanlei 1918浏览

条码 转 ISBN:
条码示例:9787508027104 前三位978(中国)
1. 除去前三位及最后一位(4),剩下7 5 0 8 0 2 7 1 0(A)
2. 高位 -> 低位(分别乘以10,9,8..3,2),即7*10,5*9,0*8…1*3,0*2。
3. 求和:70 45 0 56 0 10 28 3 0 = 212;
4. 和除以11: 212 % 11 余3
5. 余数3取11的补数:11 – 3 = 8;(8即为校验位)
注:当余数为0时,校验位为 0,余数为1时,补数为10,此时用符号X代替。
6. 结果ISBN的书号为:A值加检验位8,即得:7 5 0 8 0 2 7 1 0 8
ISBN 转 条码:
示范数据:7-5080-2710-8
1. 去掉末尾校验码8,前面统一加上978(中国),为:978750802710
2. 从代码位置序号2开始,所有偶数位的数字代码求和为a, 7 + 7 + 0 + 0 + 7 + 0 = 21
3. 将a乘以3为a, 21 * 3 = 63
4. 从代码位置序号1开始,所有奇数位的数字代码求和为b,9 + 8 + 5 + 8 + 2 + 1 = 33
5. 将a和b相加为c, 63 + 33 = 96
6. 取c的个位数d。 个位:6
7. 用10减去d即为校验位数值,将它置于最后一位。10 – 6 = 4
8. 条形码为:9 7 8 7 5 0 8 0 2 7 1 0 4
[code]using System;
using System.Text.RegularExpressions;
namespace BarCode
{
///

/// Summary description for BarCode2ISBN.
///

public class BarCode2ISBN
{
public BarCode2ISBN()
{
//
// TODO: Add constructor logic here
//
}
#region ISBN_BarCode
public string BarCodeToISBN(string barCode)
{
if(barCode.Length != 13) return string.Empty;
if(!Regex.IsMatch(barCode, @”d{13}”)) return string.Empty;
string coreCode = barCode.Substring(3, 9);
int sum = 0;
for(int i = 10; i >= 2; i–)
{
sum += i * Convert.ToInt32(coreCode.Substring(10 – i,1));
}
string checkCode = null;
if(sum % 11 == 0)
{
checkCode = “0”;
}
else if(sum % 11 == 1)
{
checkCode = “X”;
}
else
{
checkCode = (11 – (sum % 11)).ToString();
}
return coreCode + checkCode;
}
public string ISBNToBarCode(string isbnCode)
{
isbnCode = isbnCode.Replace(“-“, string.Empty);
if(isbnCode.Length != 10) return string.Empty;
if(!Regex.IsMatch(isbnCode, @”d{10}”)) return string.Empty;
string coreCode = isbnCode.Substring(0, 9);
string chinaIsbnCode = “978”;
string tmpCode = chinaIsbnCode + coreCode;
int sumOddNumber = 0;//奇数和
int sumEvenNumber = 0;//偶数和
for(int i = 0; i < 11; i++) { if( (i + 1) % 2 == 1) { sumOddNumber += Convert.ToInt32(tmpCode.Substring(i, 1)); } else { sumEvenNumber += Convert.ToInt32(tmpCode.Substring(i, 1)); } } int sum = sumOddNumber + sumEvenNumber * 3; string checkCode = null; if(sum % 10 == 0) { checkCode = "0"; } else { checkCode = (10 - (sum % 10)).ToString(); } return tmpCode + checkCode; } #endregion } }[/code]
参考资源:
标准:
http://www.aimglobal.org/standards/aimpubs.asp
128码:
http://www.aimglobal.org/standards/symbinfo/code_128_overview.asp
http://www.aimglobal.org/aimstore/stackedsymbologies.asp
http://www.aspose.com/Products/Aspose.BarCode/Demos/OnlineDemo.aspx?MainPage1DemosIndex=2
http://www.bokai.com/Barcode.Net/LiveWebDemo.aspx

转载请注明:HANLEI'BLOG » 条码和图书ISBN相互转换的方法