zsx's Old Blog

黑历史逐步隐藏中_(:з」∠)_仅保留部分可能有些价值的文章,并不保证将来的可访问性。

ASP VBScript CRC32算法

原VB6代码:http://tieba.baidu.com/p/327458071

参考VB6版的CRC32算法所写:

ASP的Byte()不像VB6的Byte()那样,可以直接用Byte(位置)得到其二进制值。所以,这里用了ado.stream,既读取文件的二进制,又顺便用AscB(objAdo.Read(1))来代替Buffer(i)。


<%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>
<%
Response.Write CRC32("D:\aaa.txt")
Public Function CRC32(Path)
      
    Dim objAdo
    Set objAdo = CreateObject("adodb.stream")
    objAdo.Open
    objAdo.Type = 1
    objAdo.LoadFromFile Path
          
    Dim crc32Result
    Dim i
    Dim j
    Dim dwCrc
    Dim iLookup
    Dim Lb
    Dim Ub
           
     '常数
    Const Num0 = &H0
    Const Num1 = &H1
    Const Num2 = &H2
    Const Num8 = &H8
    Const Num255 = &HFF
    Const Num256 = &H100
    Const Num16777215 = &HFFFFFF
    Const dwPolynomial = &HEDB88320
    Const Num2147483647 = &H7FFFFFFF
    Const NumNegative1 = &HFFFFFFFF
    Const NumNegative2 = &HFFFFFFFE
    Const NumNegative256 = &HFFFFFF00
           
    'CRC32表
    Dim crc32Table(&HFF)
           
    '初始化CRC32表
    For i = Num0 To Num255
        dwCrc = i
        For j = Num8 To Num1 Step NumNegative1
            If (dwCrc And Num1) Then
                dwCrc = ((dwCrc And NumNegative2) \ Num2) And Num2147483647
                dwCrc = dwCrc Xor dwPolynomial
            Else
                dwCrc = ((dwCrc And NumNegative2) \ Num2) And Num2147483647
            End If
        Next
        crc32Table(i) = dwCrc
    Next
    crc32Result = NumNegative1
    '计算CRC32码
    For i = 0 To objAdo.Size-1
        objAdo.Position=i
        iLookup = (crc32Result And Num255) Xor AscB(objAdo.Read(1))
        crc32Result = ((crc32Result And NumNegative256) \ Num256) And Num16777215
        crc32Result = crc32Result Xor crc32Table(iLookup)
    Next
            
    CRC32 = Hex(Not (crc32Result))
End Function
      
%>


控制面板
您好,欢迎到访网站!
  查看权限
Google Adsense
文章归档
站点信息
  • 文章总数:259
  • 页面总数:10
  • 分类总数:17
  • 标签总数:136
  • 评论总数:1644
  • 浏览总数:216659

Powered By Z-BlogPHP 1.5.1 Zero

闽ICP备15006942号   闽公网安备 35010302000147号