windows超级运行框-表达式计算(10)—2-8-16进制转换

1、环境准备

  1. windows 7,8,10,11操作系统
  2. ahk 2.x_64位

2、使用方式

可以在2进制“()”,8进制“o”,10进制 ,16进制(0x) 之间相互转换

(11111000011111)  ,以括号“()”包裹的数组0,1会被理解为二进制,运行框会直接识别并转换为10进制

o1234  ,以“o”开头的后面带数字会被理解为8进制,运行框可以直接识别并转换为10进制

256 ,单纯的数字会被识别为10进制,运行框会直接识别并转换为16进制

0x100 ,以“0x” 开头会被识别为16进制,运行框会直接识别并转换为10进制

tobase 参数1 参数2  ,可以实现任意进制转换

         参数1:必须确定其进制,比如 () , o ,10进制,0x
         参数2:输出进制  ,可以是2,8,10,16  

当然,上述操作不支持小数的进制转换!!!

  1.    输入win+R打开windows运行框 
  2.   在运行框中输入:(11111000011111)  ,输出 “15903 ”2进制转10进制
  3.  在运行框中输入:o1234  ,输出 “668”                         8进制 转10进制
  4.  在运行框中输入:256 ,输出 “0x100”                         10进制转换为16进制 
  5.  在运行框中输入:tobase 0x100 2    输出“ (100000000) ”   16进制转为2进制
  6.  在运行框中输入:tobase (100000000) 16    输出 “0x100”2进制转16进制

 

3、演示

windows超级运行框-表达式计算(10)—2-8-16进制转换

4、代码

#HotIf  winActive("运行") and winActive("ahk_class #32770")
#MaxThreadsPerHotkey 10
;在运行框中执行强大的计算功能,包括数学运算等
:*?:==::{
  try{
       rawText:=ControlGetText("Edit1","A") ;
       fullResult:=runbox.calculateExpression(rawText)
       if fullResult{
           ControlsetText(fullResult,"Edit1","A")
       }
       ControlSend("{END}","Edit1","A")
   }
}
#HotIf
;==========================================================================================================系统快捷键映射

;执行各种运算取值
class runbox
{

    ;执行比表达式计算,"==" 触发,callflag是其他函数调用该方法
    static calculateExpression(rawstr,callflag:=0)
    {
        ;2进制转为10进制 ,传入字符串(11111000011111)
        if (str:=RTrim(LTrim(Trim(rawStr),"("),")"))!=Trim(rawStr) and not RegExReplace(str,"[10]",""){
            result:=ak.otherToTen(str,2)
            fulltxt:=rawStr . "=" . result
            return callflag?result:fulltxt
        }
        ;8进制转为10进制,传入字符串o开头
        if  inStr(tr:=trim(rawStr),"o")==1 and not RegExReplace((str:=subStr(tr,2)),"\d+","") {
            result:=ak.otherToTen(str,8)
            fulltxt:=rawStr . "=" . result
            return callflag?result:fulltxt
        }
        ;16进制转为10进制 ,传入字符串0x开头
        if  inStr(str1:=trim(rawStr),"0x")==1 and not RegExReplace((str:=subStr(str1,3)),"[a-fA-F\d]+","") {
            result:= Format("{1:d}",str1)
            fulltxt:=rawStr . "=" . result
            return result
        }
        ;10进制转为16进制,传入纯数字,不包含任何其它字符
        if  not RegExReplace((str:=trim(rawStr)),"\d+",""){
            result:=ak.tenToOther(str,16)
            fulltxt:=rawStr . "=0x" . result
            return "0x" . result
        }
        ;任意进制转换,tobase 0x100 2 十六进制转为二进制,tobase 1000 10 16 十进制转为16进制
        if  instr(rawStr,"tobase ")==1 and (str:=Trim(LTrim(rawStr,"tobase "))){
            result:=this.tobaseExpression(str)
            fulltxt:=rawStr . "=" . result
            return fulltxt
        }
    }
    ;进制转换,str原字符串,二进制:111100011 十进制:1024 十六进制:0x100 八进制o100
    ;fromdecimal:需要转换的数据,todecimal:转换后的数据
    static tobaseExpression(str)
    {
        args:=strSplit( RegExReplace(trim(str),"\s+"," ") ," ")
        if(args.length==2){
            tmpMap:=Map("2","(","8","o","16","0x")
            return ak.mapget(tmpMap,args[2]) . ak.tenToOther(this.calculateExpression(args[1],1),args[2]) . (args[2]=="2"?")":"")
        }else if (args.length==3){
            return ak.tenToOther(ak.otherToTen(args[1],args[2]),args[3])
        }
    }

}
;==========================================================================================================系统快捷键映射


;++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ak工具类class
class ak
{
    ;Func 帮助map获取值,优化原生map报错问题,ignoreCase是否忽略大小写
    static mapget(map1,key,ignoreCase:=0)
    {
        try{
          if ignoreCase{
            for k ,v in map1 {
                if k=key
                    return v
            }
          }
          return map1.get(key)
        }catch as e{
           return ""
        }
    }
      ;Func 计算任意进制的十进制,str:101010(二进制) 或者其他进制,不能带o或者0x前缀
      static otherToTen(n,b)
      {
           MI:=strLen(n) ;幂
           Loop  parse, n
               result .= A_Loopfield  "*" b "^" MI-A_Index "+"
           return this.polish_notation(rtrim(result,"+"))
      }

      ;Func  十进制转换为任意进制,n:10000 ,也可传入16进制0x
       static tenToOther(n,b)
       {
             return (n < b ? "" : this.tenToOther(n//b,b)) . ((d:=Mod(n,b)) < 10 ? d : Chr(d+55))
       }
   ;Fucn 逆波兰表达式计算 + - x ÷ 幂(**/^) 模(%)  expression:数学表达式可以带括号
   ;参考:https://blog.csdn.net/assiduous_me/article/details/101981332
   static polish_notation(expression)
   {
       operator_list:=Map("+",0,"-",0,"*",0,"`/",0,"%",0,"^",0) ;注意list的haskey操作只是检测索引
       operatorlevel_map:=Map("(",0,"+","1","-",1,"*",2,"/","2","%",2,"^",3,")",4)
       operator_map:=Map("+","add","-","sub" ,"*","multi","/","divi","%","mod2","^","pow")
       expression:=strReplace(strReplace(RegExReplace(trim(expression),"\s+",""),"**","^") ,"(-","(0-")
       expression:=inStr(expression,"-(")==1?strReplace(this.insertStrAt(expression,this.mirrorSymbolIndex(expression,"(",")"),")"),"-(","(0-("):expression
       ;①.获取一个中缀表达式集合类似 100+2 -> ["100","+","2"]
       middlefix_list:=[],fix:=""
       Loop parse,expression{
           current_value:=A_LoopField
           if(operatorlevel_map.has(current_value))
           {
             tmp:=""!=fix?middlefix_list.push(fix):""
             middlefix_list.push(current_value)
             fix:=""
           }else fix:=fix . current_value
       }
      tmp2:=fix!=""?middlefix_list.push(fix):""
      if(middlefix_list[1]="-"){ ;处理开头为负数
          middlefix_list.insertAt(1,"(")
          middlefix_list.insertAt(2,"0")
          middlefix_list.insertAt(5,")")
      }
      ;②.转换为后缀表达式(逆波兰表达式)
       operator_stack:=[] ,suffix_list:=[],number_stack:=[]
       for index ,currentElmt in middlefix_list
       {
         if(operator_list.has(currentElmt))
         {
             while(operator_stack.length>0 && operatorlevel_map.get(operator_stack.get(operator_stack.Length))>=operatorlevel_map.get(currentElmt))
                suffix_list.push(operator_stack.pop())
             operator_stack.push(currentElmt)
         }else if(currentElmt=="(")
            operator_stack.push("(")
         else if(currentElmt==")"){
            while(operator_stack.length>0 && operatorlevel_map.get(operator_stack.get(operator_stack.length))>operatorlevel_map.get("("))
               suffix_list.push(operator_stack.pop())
            if(operator_stack.length>0)
                operator_stack.pop()
         }else
             suffix_list.push(currentElmt)
       }
       while(operator_stack.length>0)
           suffix_list.push(operator_stack.pop())
       ;③.计算表达式最终的值,规则数字入栈,操作符就出栈两个元素计算值并把结果入栈
       for key,opertor_or_number in suffix_list{
          if(operator_list.has(opertor_or_number)){
               number2:=number_stack.pop(),number1:=number_stack.pop()
               tmpObj:={add:number1+number2,sub:number1-number2,multi:number1*number2,pow:number1**number2}
               T1:=opertor_or_number=="/"?(tmpObj.divi:=number1/number2):""       ;除法容易引发除0异常
               T2:=opertor_or_number=="%"?(tmpObj.mod2:=mod(number1,number2)):""  ;取模容易引发除0异常
               number_stack.push(tmpObj.%operator_map.get(opertor_or_number)%)
          }else
               number_stack.push(opertor_or_number)
       }
       return number_stack.pop()
   }


}
;++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ak工具类class

5、总结

由于很多操作代码可以复用,单个功能代码臃肿,进制转换不支持小数,之能满足日常的需求,如果有特别精确的需求请使用专业工具、

完整功能参考:windows超级运行框-表达式计算(12)汇总

各位大佬觉得还可以怎么操作? 欢迎评论区留言讨论!

给TA捐赠
共{{data.count}}人
人已捐赠
其他

windows超级运行框-表达式计算(5)—uuid/cpuid获取

2023-12-1 10:03:21

其他

AutoHotkey_H简单介绍(引用)

2023-12-7 21:40:42

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
有新私信 私信列表
搜索