ENA13条码转换函数
最近一个VB项目里需要做ENA-13的条码处理,一开始用了很多字体打,可就是不能被扫描,在网上查了相关资料后才知道ENA-13的编码规则,呼呼,
在网上想找相关的VB代码可找没找着,只能用这个规则自己写一个C#及VB函数了,以备日后再用,也为各位需要的同志服务
VB Code
C# Code
在网上想找相关的VB代码可找没找着,只能用这个规则自己写一个C#及VB函数了,以备日后再用,也为各位需要的同志服务
VB Code
1'ENA-13条码转换函数
2'调用此函数打印时推荐使用EanBwrP36Tt,EanBwrP36xTt等同类字体
3'ENA-13条码格式可参考:http://www.enpot.com.cn/zhishi/barcode302.htm
4'Copyright(c) 2001-2006 by S.B.Z. Studio
5'Pvistely 2006-07-17
6Public Function ENA13Encoder(pSCode As String) As String
7 Dim tmpRuleStr As String, tmpHandleStr As String
8 Dim tmpRule, tmpHandle
9 If Len(pSCode) <> 13 Then Exit Function
10 '左资料码格式
11 tmpRuleStr = "AAAAAA,AAAAAA,AABABB,AABBAB,AABBBA,ABBAAB,ABBBAA,ABABAB,ABABBA,ABBABA"
12 '导入值符号
13 tmpHandleStr = "# $ % & ' ( ) * + ,"
14 tmpRule = Split(tmpRuleStr, ",")
15 tmpHandle = Split(tmpHandleStr, " ")
16 Dim tmpStr As String
17 Dim tmpStr2 As Integer
18 Dim i, j
19 If Not IsNumeric(pSCode) Then ENA13Encoder = "": Exit Function
20 tmpRuleStr = tmpRule(Val(Left(pSCode, 1)))
21 tmpHandleStr = tmpHandle(Val(Left(pSCode, 1)))
22 '第一位(导入值)加左护线
23 tmpStr = tmpHandleStr & "!"
24 '第二至七位(左资料码)
25 For i = 1 To 6
26 tmpStr2 = Mid(pSCode, i + 1, 1)
27 If Mid(tmpRuleStr, i, 1) = "A" Then
28 tmpStr = tmpStr & Mid(pSCode, i + 1, 1)
29 Else
30 tmpStr = tmpStr & Chr(Val(Mid(pSCode, i + 1, 1)) + 65)
31 End If
32 Next
33 '中分符
34 tmpStr = tmpStr & "-"
35 '第八至十三位(右资料码,包括校验位)
36 For i = 7 To 12
37 tmpStr = tmpStr & Chr(Val(Mid(pSCode, i + 1, 1)) + 97)
38 Next
39 '函数返回时加右护线
40 ENA13Encoder = tmpStr & "!"
41End Function
2'调用此函数打印时推荐使用EanBwrP36Tt,EanBwrP36xTt等同类字体
3'ENA-13条码格式可参考:http://www.enpot.com.cn/zhishi/barcode302.htm
4'Copyright(c) 2001-2006 by S.B.Z. Studio
5'Pvistely 2006-07-17
6Public Function ENA13Encoder(pSCode As String) As String
7 Dim tmpRuleStr As String, tmpHandleStr As String
8 Dim tmpRule, tmpHandle
9 If Len(pSCode) <> 13 Then Exit Function
10 '左资料码格式
11 tmpRuleStr = "AAAAAA,AAAAAA,AABABB,AABBAB,AABBBA,ABBAAB,ABBBAA,ABABAB,ABABBA,ABBABA"
12 '导入值符号
13 tmpHandleStr = "# $ % & ' ( ) * + ,"
14 tmpRule = Split(tmpRuleStr, ",")
15 tmpHandle = Split(tmpHandleStr, " ")
16 Dim tmpStr As String
17 Dim tmpStr2 As Integer
18 Dim i, j
19 If Not IsNumeric(pSCode) Then ENA13Encoder = "": Exit Function
20 tmpRuleStr = tmpRule(Val(Left(pSCode, 1)))
21 tmpHandleStr = tmpHandle(Val(Left(pSCode, 1)))
22 '第一位(导入值)加左护线
23 tmpStr = tmpHandleStr & "!"
24 '第二至七位(左资料码)
25 For i = 1 To 6
26 tmpStr2 = Mid(pSCode, i + 1, 1)
27 If Mid(tmpRuleStr, i, 1) = "A" Then
28 tmpStr = tmpStr & Mid(pSCode, i + 1, 1)
29 Else
30 tmpStr = tmpStr & Chr(Val(Mid(pSCode, i + 1, 1)) + 65)
31 End If
32 Next
33 '中分符
34 tmpStr = tmpStr & "-"
35 '第八至十三位(右资料码,包括校验位)
36 For i = 7 To 12
37 tmpStr = tmpStr & Chr(Val(Mid(pSCode, i + 1, 1)) + 97)
38 Next
39 '函数返回时加右护线
40 ENA13Encoder = tmpStr & "!"
41End Function
C# Code
1 /// <summary>
2 /// Ena-13编码转换函数
3 /// 调用此函数打印时推荐使用EanBwrP36Tt,EanBwrP36xTt等同类字体
4 /// ENA-13条码格式可参考:http://www.enpot.com.cn/zhishi/barcode302.htm
5 /// Copyright(c) 2001-2006 by S.B.Z. Studio
6 /// Pvistely 2006-07-17
7 /// </summary>
8 /// <param name="pCode">带效验位13位数字串</param>
9 /// <returns> ENA-13编码</returns>
10 public static string ENA13Encoder(string pCode)
11 {
12 string tmpRuleStr = "AAAAAA,AAAAAA,AABABB,AABBAB,AABBBA,ABBAAB,ABBBAA,ABABAB,ABABBA,ABBABA";
13 string tmpHandleStr = "# $ % & ' ( ) * + ,";
14 string[] tmpRule = tmpRuleStr.Split(new char[] { ',' });
15 string[] tmpHandle = tmpHandleStr.Split(new char[] { ' ' });
16 if (!Microsoft.VisualBasic.Information.IsNumeric(pCode)) return "";
17 string tmpStr = "";
18 string tmpStr2 = "";
19 tmpRuleStr = tmpRule[Convert.ToInt32(pCode.Substring(0, 1))];
20 tmpHandleStr = tmpHandle[Convert.ToInt32(pCode.Substring(0, 1))];
21 //第一位
22 tmpStr = tmpHandleStr + "!";
23 //第二至七位
24 for (int i = 1; i <= 6; i++)
25 {
26 tmpStr2 = pCode.Substring(i, 1);
27 if (tmpRuleStr.Substring(i - 1, 1) == "A")
28 tmpStr += pCode.Substring(i, 1);
29 else
30 tmpStr += (char)(Convert.ToInt32(pCode.Substring(i, 1)) + 65);
31 }
32 //中分符
33 tmpStr += "-";
34 //第八至十三位
35 for (int i = 7; i <= 12; i++)
36 tmpStr += (char)(Convert.ToInt32(pCode.Substring(i, 1)) + 97);
37 //加右护线
38 return tmpStr + "!";
39 }
40
2 /// Ena-13编码转换函数
3 /// 调用此函数打印时推荐使用EanBwrP36Tt,EanBwrP36xTt等同类字体
4 /// ENA-13条码格式可参考:http://www.enpot.com.cn/zhishi/barcode302.htm
5 /// Copyright(c) 2001-2006 by S.B.Z. Studio
6 /// Pvistely 2006-07-17
7 /// </summary>
8 /// <param name="pCode">带效验位13位数字串</param>
9 /// <returns> ENA-13编码</returns>
10 public static string ENA13Encoder(string pCode)
11 {
12 string tmpRuleStr = "AAAAAA,AAAAAA,AABABB,AABBAB,AABBBA,ABBAAB,ABBBAA,ABABAB,ABABBA,ABBABA";
13 string tmpHandleStr = "# $ % & ' ( ) * + ,";
14 string[] tmpRule = tmpRuleStr.Split(new char[] { ',' });
15 string[] tmpHandle = tmpHandleStr.Split(new char[] { ' ' });
16 if (!Microsoft.VisualBasic.Information.IsNumeric(pCode)) return "";
17 string tmpStr = "";
18 string tmpStr2 = "";
19 tmpRuleStr = tmpRule[Convert.ToInt32(pCode.Substring(0, 1))];
20 tmpHandleStr = tmpHandle[Convert.ToInt32(pCode.Substring(0, 1))];
21 //第一位
22 tmpStr = tmpHandleStr + "!";
23 //第二至七位
24 for (int i = 1; i <= 6; i++)
25 {
26 tmpStr2 = pCode.Substring(i, 1);
27 if (tmpRuleStr.Substring(i - 1, 1) == "A")
28 tmpStr += pCode.Substring(i, 1);
29 else
30 tmpStr += (char)(Convert.ToInt32(pCode.Substring(i, 1)) + 65);
31 }
32 //中分符
33 tmpStr += "-";
34 //第八至十三位
35 for (int i = 7; i <= 12; i++)
36 tmpStr += (char)(Convert.ToInt32(pCode.Substring(i, 1)) + 97);
37 //加右护线
38 return tmpStr + "!";
39 }
40