找回密码
 加入
搜索
查看: 3552|回复: 5

[效率算法] 请大家修改这个BASE32编码函数以支持中文,谢谢

  [复制链接]
发表于 2011-9-18 14:30:11 | 显示全部楼层 |阅读模式
本帖最后由 asdasdasd 于 2011-9-18 15:55 编辑

能支持英文,数字,不支持中文,请大家修改一下,
比如说:中华人民共和国,它的正确编码应该是:23ilxkwizpb7donsxlg3t6q,但是此程序算出来的不对,英语和数字的是对的
; ===============================================================================================================================
;Base32 encoding decoding adapted from:
;http://www.docjar.com/src/api/xnap/plugin/gnutella/util/Base32.java
;ported to AutoIt by Stephen Podhajecki {gehossafats at netmdc dot com}
;this port falls under GPL
;; This program is free software; you can redistribute it and/or
;; modify it under the terms of the GNU General Public License
;; as published by the Free Software Foundation; either version 2
;; of the License, or any later version.
;;
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with this program; if not, write to the Free Software
;; Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
;;
; ===============================================================================================================================
Const $BASE32CHARS ="ABCDEFGHIJKLMNOPQRSTUVWXYZ234567";
Local $_BASE32_DECODE_TABLE[128]
Local $BASE32_INIT = 0
; #FUNCTION# =====================================================================================================================
; Description ...: Encode a string as base32
; Parameters ....: $vData - IN - Data to decode.
; Return values .: On Success - Encoded data
;                  On Failure - @error set to 1, 0 returned
; Author ........: Ported by Stephen Podhajecki {gehossafats at netmdc. com}
; Remarks .......: Works with text strings and files.
; Related .......: _Base32_Decode
; ================================================================================================================================

$data="中国"
MsgBox(0,"",_Base32_Encode($a))
Func _Base32_Encode($vData)
        If $BASE32_INIT = 0 Then
                If Not _Base32_BuildTable() Then Return SetError(1,0,0)
        EndIf
        Local $data = __Split_Key($vData)
        Local $dataLength = UBound($data)
        If Not $dataLength > 0 Then Return SetError(1,0,0)
        Local $chars[$dataLength*8/5 +(1*(Mod($dataLength,5)>0))]
        Local $charsLength = UBound($chars)
        Local $i = 0, $j =0, $index =0, $b =0
        Local $ALPHABET = __Split_Key($BASE32CHARS)
        For $i =0 to $charsLength -1
                If $index > 3 Then
                        $b = BitAnd(ASC($data[$j]) ,BitShift(0xFF,$index))
                        $index = Mod($index+5,8)
                        $b = BitShift($b,-$index)
                        if $j < $dataLength -1 Then
                                $b =BitOR($b,BitShift(BitAND(ASC($data[$j+1]),0xFF), (8-$index)))
                        EndIf
                        $chars[$i] = $ALPHABET[$b]
                        $j += 1
                Else
                        $chars[$i] = $ALPHABET[BitAnd(BitShift(ASC($data[$j]),(8-($index+5))),0x1F)]
                        $index = Mod($index +5,8)
                        if $index =0 Then
                                $j +=1
                        EndIf
                EndIf
        Next
        Local $sEncoded = ""
        For $x = 0 to UBound($chars)-1
                $sEncoded &= $chars[$x]
        Next
        Return $sEncoded
EndFunc

; #FUNCTION# =====================================================================================================================
; Description ...: Decode a base32 encoded string
; Parameters ....: $vData - IN - Data to decode.
; Return values .: On Success - Decoded data
;                  On Failure - @error set to 1, 0 returned
; Author ........: Ported by Stephen Podhajecki {gehossafats at netmdc. com}
; Remarks .......: Works with text strings and files.
; Related .......: _Base32_Encode
; ================================================================================================================================
Func _Base32_Decode($vData)
        If $BASE32_INIT = 0 Then
                If Not _Base32_BuildTable() Then Return SetError(1,0,0)
        EndIf
        Local $stringData  = __Split_Key($vData)
        Local $stringDataLength = UBound($stringData)
        If Not $stringDataLength > 0 Then Return SetError(1,0,0)
        Local $data[(($stringDataLength*5)/8)]
        Local $dataLength = UBound($data)
        Local $i, $j =0, $index = 0, $val =0, $decoded = ""
        For $i = 0 to $stringDataLength -1
                $val =0
                $val = $_BASE32_DECODE_TABLE[ASC($stringData[$i])]
                If $val = 0xFF Then
                        ;;rem illegal character
                        Return SetError(1,0,0)
                EndIf
                If ($index <=3) Then
                        $index = Mod($index+5,8)
                        If $index = 0 Then
                                $data[$j] = BitOr($data[$j],$val)
                                $j += 1
                        Else
                                $data[$j] = BitOR($data[$j],BitShift($val, -(8-$index)))
                        EndIf
                Else
                        $index = Mod($index+5,8)
                        $data[$j] = BitOr($data[$j],BitShift($val,$index))
                        $j += 1
                        if $j < $dataLength Then 
                                $data[$j] = BitOr($data[$j],BitShift($val, -(8 - $index)))
                                $data[$j] = BitAnd($data[$j],0xFF)
                        EndIf
                EndIf
        Next
        For $x = 0 to UBound($data) -1
                $decoded &= Chr($data[$x])
        Next
        Return $decoded
EndFunc
; ===============================================================================================================================
; _Base32_BuildTable(): Builds a conversion table 
; ===============================================================================================================================
Func _Base32_BuildTable()
        For $i = 0 to UBound($_BASE32_DECODE_TABLE)-1
                $_BASE32_DECODE_TABLE[$i] = 0xFF
        Next
        For $i = 0 to StringLen($BASE32CHARS)-1
                $_BASE32_DECODE_TABLE[ASC(StringMid($BASE32CHARS,$i+1,1))]= $i
                if $i < 24 Then
                        $_BASE32_DECODE_TABLE[ASC(StringLower(StringMid($BASE32CHARS,$i+1,1)))]= $i
                EndIf
        Next
        $BASE32_INIT = 1
        Return 1
EndFunc

; ===============================================================================================================================
; __Split_Key:  Internal function
; splits a string into an array of characters and strip the count value from the first element.
; ===============================================================================================================================
Func __Split_Key($szKey, $szDelim = "")
        If $szKey = "" Then Return SetError(1, 0, 0)
        If IsArray($szKey) Then Return $szKey
        Local $iCount, $szTemp = ""
        Local $aTemp = StringSplit($szKey, $szDelim)
        If Not @error Then
                Local $iCount = $aTemp[0], $iTotal = 0
                For $x = 1 To $iCount
                        $iTotal += 1
                        $aTemp[$x - 1] = $aTemp[$x]
                Next
                ReDim $aTemp[$iTotal]
                Return $aTemp
        EndIf
        Return SetError(1, 0, 0)
EndFunc   ;==>__Split_Key
发表于 2011-9-18 14:45:35 | 显示全部楼层
干脆直接说下想要的函数是个啥功能好了
 楼主| 发表于 2011-9-18 14:52:12 | 显示全部楼层
本帖最后由 asdasdasd 于 2011-9-18 14:54 编辑

BASE32编码,官网论坛上有,但是不支持中文啊,数字和字母都能通过

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?加入

×
发表于 2011-9-18 16:04:13 | 显示全部楼层
你把中文改成unicode编码就行了,解码的时候unicode返回来
 楼主| 发表于 2011-9-18 16:10:23 | 显示全部楼层
中文在AU3环境不是默认UNICODE吗?
 楼主| 发表于 2011-9-18 16:11:07 | 显示全部楼层
尝试过把中文改成ANSI编码,结果不正常
您需要登录后才可以回帖 登录 | 加入

本版积分规则

QQ|手机版|小黑屋|AUTOIT CN ( 鲁ICP备19019924号-1 )谷歌 百度

GMT+8, 2024-5-2 09:28 , Processed in 0.081132 second(s), 24 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表