找回密码
 加入
搜索
查看: 3842|回复: 7

[AU3基础] [已解决]---字符串后X位字符10进制/16进制加减运算

  [复制链接]
发表于 2011-2-28 14:47:44 | 显示全部楼层 |阅读模式
本帖最后由 kingfirekkk 于 2011-3-1 08:16 编辑

想实现一个比较简单的功能:
一串1~18位的数字和字母混编的字符串,进行加减运算?如:
AXC1D2A9B0001~AXC1D2A9B9999
又或者
NN23N45N111111111111~NN23N45N999999999999

要对这些字符串进行加减运算
想不通/不能理解:为什么:   "0001"+1 不等于0002,而是等于2
另外:ABCD001+1也不会等于ABCD002,也是等于2
感觉自己又在钻牛角尖了
发表于 2011-2-28 15:49:53 | 显示全部楼层
在C里,有一个强制性类型转换。。。。呵呵,,,
类型。。AUTOIT也有吧。。。Number ( 表达式 )
发表于 2011-2-28 15:57:38 | 显示全部楼层
语言参考 - 数据类型
In AutoIt 只有一个叫做 Variant(变量) 的数据类型.  
一个 Variant(变量) 可以是数字或字符串, 如何使用这些数据要视情况而定.  
例如,若试图让两个变量相乘, 则它们将被当作数字类型, 而试图连接(拼合)两个变量,则它们将被视为字符串.
示例:
    10 * 20 --- 等于数字 200 (符号 * 用于两个数相乘)
    10 * "20" --- 等于数字 200
    "10" * "20" --- 等于数字 200
    10 & 20 --- 相当于字符串 "1020" (符号 & 用于连接字符串)

Number ( 表达式 )
备 注
以字母开头的字符串将被视为数值零.
以数字开头的字符串将返回被(首个)非数字字符截断后的结果.





如果一个字符串被当作数字使用时,隐含调用 Number() 函数操作.

如果字符串不包含有效的数字, 则它会被假定为等于 0.  例如:

    10 * "fgh" --- 结果等于数字 0.

如果字符串被用于布尔运算, 并且字符串为 "" , 则字符串被视为 False (见下文).  例如:

    NOT "" --- 等于布尔值 true. ( NOT "" 意为非假,结果即真. )
发表于 2011-2-28 15:59:22 | 显示全部楼层
因为au3的变量定义
要了解下面几个原则:
1.字符串直接运算只能用&号连接.而无法直接进行加减
2.正整数运算,不会前面的0.

所以,你要先把原来的字符串拆成前缀字符和后缀序号.
接着把后缀序号当成int类型,使用数字运算.
最后,再使用字符串函数和字符串连接组合成新的字符串.
看一下下面的代码就明白了
$sLen = 18                                ;字符串为18位
$sKey = "FFFFFFFFFFFF"        ;假设前缀是12个F
$iVar  = 1

$NextVar = $iVar + 1
$NextKey = "000000000000000000" & $NextVar
$NextKey = StringRight($NextKey, 18 - StringLen($sKey))
$NextKey = $sKey & $NextKey

MsgBox(4096, "", $NextKey)
发表于 2011-2-28 16:01:42 | 显示全部楼层
另:你在一楼贴子里:最后一句:另外:ABCD001+1也不会等于ABCD002,也是等于2

应等于1啊,怎么会等于2?请教。。。。
发表于 2011-3-1 00:41:54 | 显示全部楼层
本帖最后由 netegg 于 2011-3-1 00:45 编辑

回复 5# boyhong
首先是你的第一个问题:数学里有0001+1=0002这个表达方式吗?
第二个问题abcd001+1根本就是错的,如果你要是问得'abcd001'+1,auto对加减等数学计算如果是字符串自动转换为0,出不来2这个值
 楼主| 发表于 2011-3-1 08:15:55 | 显示全部楼层
哈哈,想不到小弟的一贴子,整出这么多风波了。曲折一下,其它就可以实现此功能了。
中间还是有用到P版的一段代码.....未来得及整理了。

#NoTrayIcon
#Region ;**** 参数创建于 ACNWrapper_GUI ****
#AutoIt3Wrapper_icon=UDF\ICO.ico
#AutoIt3Wrapper_outfile=打印工具.exe
#AutoIt3Wrapper_UseUpx=n
#EndRegion ;**** 参数创建于 ACNWrapper_GUI ****
#include<array.au3>
#include <ButtonConstants.au3>
#include <ComboConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <String.au3>

#AutoIt3Wrapper_Run_Debug_Mode=Y
#Region ---Au3Recorder generated code Start ---
Opt("WinWaitDelay", 100)
Opt("WinDetectHiddenText", 1)
Opt("MouseCoordMode", 0)

Global $var, $win, $hwd, $main, $quit, $num, $config, $gi, $close = 0, $cp, $box, $start, $leirong, $_bit, $bit, $a, $b, $c
Global $end
Global $nb = ''
Global $Paused = 0

$config = @ScriptDir & "\config.ini"
$tite = IniRead($config, "Config", "标题", "")
$var = WinList($tite)
For $i = 1 To $var[0][0]
        $win &= $var[$i][0]
Next
#EndRegion ### END Koda GUI section ###

#include <ButtonConstants.au3>
#include <ComboConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>

$main = GUICreate("打印辅助工具", 430, 210, -1, -1)
GUICtrlCreateGroup("条码打印项", 4, 4, 420, 80)
GUICtrlCreateLabel("窗口标题:", 16, 23, 60, 17)
GUICtrlSetFont(-1, 8, 800, 0, "MS Sans Serif")
GUICtrlCreateLabel("重复次数:", 16, 55, 60, 17)
GUICtrlSetFont(-1, 8, 800, 0, "MS Sans Serif")
$num = GUICtrlCreateInput("100", 80, 52, 249, 21)
$print = GUICtrlCreateButton("打 印", 340, 38, 75, 25)
GUICtrlCreateGroup("", -99, -99, 1, 1)
GUICtrlCreateGroup("自动增量", 4, 89, 420, 80)
GUICtrlCreateLabel("条码起始:", 16,110, 60, 17)
GUICtrlSetFont(-1, 8, 800, 0, "MS Sans Serif")
$start = GUICtrlCreateInput("自动捕捉剪切板的内容", 80, 110, 249, 21)
$leirong = ClipGet()
If $leirong <> '' And StringLen($leirong) < 20 Then GUICtrlSetData($start, $leirong)
GUICtrlCreateLabel("后几位:", 220, 143, 60, 25)
GUICtrlSetFont(-1, 8, 800, 0, "MS Sans Serif")
$bit = GUICtrlCreateCombo("", 290, 140, 40, 25, BitOR($CBS_DROPDOWN, $CBS_AUTOHSCROLL))
GUICtrlSetData(-1, "2|3|4|5|6|7|8", "4")
$ten = GUICtrlCreateRadio("十进制", 16, 140, 80, 21)
GUICtrlSetState(-1, $GUI_CHECKED)
$hx = GUICtrlCreateRadio("十六进制", 130, 140, 80, 21)
$cp = GUICtrlCreateButton("复 制", 340, 123, 73, 25)
GUICtrlCreateGroup("", -99, -99, 1, 1)

$box = GUICtrlCreateCombo($win, 80, 19, 249, 25, BitOR($CBS_DROPDOWN, $CBS_AUTOHSCROLL))
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###
HotKeySet("{PAUSE}", "TogglePause")
HotKeySet("{ESC}", "Terminate")
HotKeySet("{NUMPADDIV}", "addnb")
HotKeySet("{NUMPADMULT}", "subnb")

While 1
        $nMsg = GUIGetMsg()
        Switch $nMsg
                Case $GUI_EVENT_CLOSE
                        $close = 1
                        Exit
                Case $ten
                        GUICtrlSetState($hx, $GUI_UNCHECKED)
                Case $hx
                        GUICtrlSetState($ten, $GUI_UNCHECKED)
                Case $print
                        $gi = GUICtrlRead($num)
                        If $gi >= 1 And $gi <> "" And GUICtrlRead($box) <> "" Then
                                Do
                                        print($win)
                                        $gi -= 1
                                        ToolTip("当前打印数量为" & $gi & @CRLF & "如需暂停打印,请按Pause键" & @CRLF & "如需退出打印,请按ESC键!")
                                Until $gi = 0
                                _WinWaitActivate("辅助工具", "")
                        Else
                                MsgBox(16 + 262144, "警告", "重复次数不能为空或者零,或者没有找到窗口!")
                        EndIf
                Case $cp
                        $nb = GUICtrlRead($start) ;要加减的字符(全部)
                        $_bit = GUICtrlRead($bit) ;位数
                        $a = StringRight($nb, $_bit) ;要加减运算的字符
                        $b = StringTrimRight($nb, $_bit) ;左边的字符
        EndSwitch
WEnd

Func Addnb()
        If GUICtrlRead($ten) = $GUI_CHECKED Then
                _WinWaitActivate("BarTender Enterprise", "")
                MouseClick("left")
                MouseClick("left")
                _WinWaitActivate("修改所选条形码对象", "")
                Send(GUICtrlRead($start))
                Send("{ENTER}")
                $a += 1
                Switch $_bit - StringLen($a)
                        Case 0
                                $c = $a
                        Case 1
                                $c = "0" & $a
                        Case 2
                                $c = "00" & $a
                        Case 3
                                $c = "000" & $a
                        Case 4
                                $c = "0000" & $a
                        Case 5
                                $c = "00000" & $a
                        Case 6
                                $c = "000000" & $a
                        Case 7
                                $c = "0000000" & $a
                        Case 8
                                $c = "00000000" & $a
                EndSwitch
                GUICtrlSetData($start, $b & $c)
        Else ;十六进制加
                _WinWaitActivate("BarTender Enterprise", "")
                MouseClick("left")
                MouseClick("left")
                _WinWaitActivate("修改所选条形码对象", "")
                Send(GUICtrlRead($start))
                Send("{ENTER}")
                $a = Dec($a)
                $a = Hex($a + 0x01, $_bit)
                GUICtrlSetData($start, $b & $a)
        EndIf
EndFunc   ;==>Addnb

Func subnb()
        If GUICtrlRead($ten) = $GUI_CHECKED Then
                $a -= 1
                Switch $_bit - StringLen($a)
                        Case 0
                                $c = $a
                        Case 1
                                $c = "0" & $a
                        Case 2
                                $c = "00" & $a
                        Case 3
                                $c = "000" & $a
                        Case 4
                                $c = "0000" & $a
                        Case 5
                                $c = "00000" & $a
                        Case 6
                                $c = "000000" & $a
                        Case 7
                                $c = "0000000" & $a
                        Case 8
                                $c = "00000000" & $a
                EndSwitch
                GUICtrlSetData($start, $b & $c)
                _WinWaitActivate("BarTender Enterprise", "")
                MouseClick("left")
                MouseClick("left")
                _WinWaitActivate("修改所选条形码对象", "")
                Send(GUICtrlRead($start))
                Send("{ENTER}")
        Else ;十六进制减
                $a = Dec($a)
                $a = Hex($a - 0x01, $_bit)
                GUICtrlSetData($start, $b & $a)
                _WinWaitActivate("BarTender Enterprise", "")
                MouseClick("left")
                MouseClick("left")
                _WinWaitActivate("修改所选条形码对象", "")
                Send(GUICtrlRead($start))
                Send("{ENTER}")
        EndIf
EndFunc   ;==>subnb

Func _tentohex($iNumber)
        Dim $sBits = "0123456789ABCDEF"
        Dim $iNumBits = 16, $sResult = ""
        ; 10进制1024转8进制。
        While $iNumber > 0
                $sResult = StringMid($sBits, Mod($iNumber, $iNumBits) + 1, 1) & $sResult
                $iNumber = Int($iNumber / $iNumBits)
        WEnd
        Return $sResult
EndFunc   ;==>_tentohex

Func print($windows)
        _WinWaitActivate($windows, "")
        Send("{F6}")
        _WinWaitActivate("Print", "")
        Send("{ENTER}")
        _WinWaitActivate($windows, "")
        Send("{ALT}E")
        Send("{ENTER}")
        Send("{F6}")
        _WinWaitActivate("Print", "")
        Send("{ENTER}")
        _WinWaitActivate($windows, "")
        If $close = 1 Then Exit
EndFunc   ;==>print

Func _WinWaitActivate($title, $text, $timeout = 0)
        WinWait($title, $text, $timeout)
        If Not WinActive($title, $text) Then WinActivate($title, $text)
        WinWaitActive($title, $text, $timeout)
EndFunc   ;==>_WinWaitActivate

Func IsVisible($handle)
        If BitAND(WinGetState($handle), 2) Then
                Return 1
        Else
                Return 0
        EndIf
EndFunc   ;==>IsVisible

Func Terminate()
        Exit 0
EndFunc   ;==>Terminate

Func TogglePause()
        $Paused = Not $Paused
        While $Paused
                Sleep(100)
        WEnd
EndFunc   ;==>TogglePause
发表于 2011-3-1 09:19:30 | 显示全部楼层
回复  boyhong
首先是你的第一个问题:数学里有0001+1=0002这个表达方式吗?
第二个问题abcd001+1根本就是 ...
netegg 发表于 2011-3-1 00:41



    呵呵,感谢netegg的关注   ....所以正如我二楼所说,非要运算的话,就:Number ( 表达式 ) 先来一下,再算。也是可以的啊。。。
您需要登录后才可以回帖 登录 | 加入

本版积分规则

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

GMT+8, 2024-10-2 12:31 , Processed in 0.076926 second(s), 22 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

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