中国程序员联盟 正在重新改版中ing 不便之处还请见谅 改版后将内容涉及java delphi .net php
 
  首页 | 数据库开发 | 网络通讯 | 多线程 | 多媒体开发 | 图像处理 | 程序人生 | 系统函数 | 控件开发 | Web服务
 
  当前位置:笨鱼delphi技术网>数据库开发>开发应用>文章内容

delphi 条形码处理

来源:国外 关于:Mike Shkolnik 发布时间:2007-08-02   [收藏] [推荐]
I want to publish a code for checksum calculation by modulus 10 which is used in the barcodes. I must say that this "mod10" is specifical so readf an article if you're interested.
This algorithm is very popular for UPC barcodes (Universal Product Code), hash code or serial number generation for applications etc...
The basic algorithm:
add the values of the digits in the odd positions (1, 3, 5...)
multiply this result by 3
add the values of the digits in the even positions (2, 4, 6...)
sum the results of steps 2 and 3
the check digit is the smallest number which, when added to the result in step 4, produces a multiple of 10.
Small example. Assume the source data is 08137919805
0+1+7+1+8+5=22
22*3=66
8+3+9+9+0=29
66+29=95
95+??=100 where ?? is a 5 (our checksum)
My implementation in the Pascal:
function Mod10(const Value: string): Integer;
var
  i, intOdd, intEven: Integer;
begin
  {add all odd seq numbers}
  intOdd := 0;
  i := 1;
  while (i <= Length(Value)) do
  begin
    Inc(intOdd, StrToIntDef(Value[i], 0));
    Inc(i, 2);
  end;
  {add all even seq numbers}
  intEven := 0;
  i := 2;
  while (i <= Length(Value)) do
  begin
    Inc(intEven, StrToIntDef(Value[i], 0));
    Inc(i, 2);
  end;
  Result := 3 * intOdd + intEven;
  {modulus by 10 to get}
  Result := Result mod 10;
  if Result <> 0 then
    Result := 10 - Result
end;
You can expand or optimize this algorithm for own needs.
For example, I modified it and now I use it for any characters (not only digits) in source value.
The original algorithm I used for UPC-barcode validation in the SMReport Designer and the my extended algorithm I use in the serial number generation as part of the protection schema (in the shareware projects).

Solve 2:
function BarCodeValid(ACode: string): boolean;
var
  I: integer;
  SumOdd, SumEven: integer;
  ADigit, AChecksumDigit: integer;
begin
  SumOdd := 0;
  SumEven := 0;
  for I := 1 to (Length(ACode) - 1) do
  begin
    ADigit := StrToIntDef(ACode[I], 0);
    if (I mod 2 = 0) then
    begin
      SumEven := SumEven + ADigit;
    end
    else
    begin
      SumOdd := SumOdd + ADigit;
    end; {if}
  end; {for}
  AChecksumDigit := StrToIntDef(ACode[Length(ACode)], 0);
  Result := ((SumOdd * 3 + SumEven + AChecksumDigit) mod 10 = 0);
end; {--BarCodeValid--}

[浏览: 次]   
上一篇:delphi Olevariant   下一篇:delphi 程序人生前人箴言
[收藏] [推荐] [返回顶部] [打印本页] [关闭窗口]  
    评论加载中…
google adsense热点文章
·delphi Delphi_三谈多态——善用virtua
·delphi Delphi_三层开发基本概念介绍
·delphi 汉字转拼音码(上)
·delphi CS构架下的客户端自动更新程序
·delphi Olevariant
·delphi 在Dephi中使用TStream读写数据
·delphi delphi处理流
·delphi 汉字转拼音码(下)
·delphi 关于使用COM对象的方法
·delphi MTS组件——从理论到实践
·delphi 汉字转拼音码(中3)
·delphi 创建纯资源的DLL
     delphi技术网 | firefox 下载 | Avant Browser下载 | dedecms 技术网 | drupal 爱好者 | php 技术网
  Copyright@www.delphichm.com,2006-2009.All Rights Reserved.
 
程序员联盟 | delphi Java .net|QQ:707102932