【lisp源码】进制转换

分类:lisp函数 | 标签: lisp   源码   进制   转换  
2021-11-11 21:53 阅读(?)评论(0)

 lisp源码,功能为进制转换。


;;;=================================================================*
;;;                                                                 *
;;;      以下为进制转换通用函数                                     *
;;;      zml84 于 2012-06-05                                         *
;;;=================================================================*
;;;功能:十进制数值  转换为  十六进制字符                           *
;;;参数:int -----整数,十进制数值。                                *
;;;例如:(ZL-JZ10->JZ16 11500)  返回 "2CEC"                         *
;;;      (ZL-JZ10->JZ16  8400)  返回 "20D0"                         *
(defun ZL-JZ10->JZ16 (int)
  (cond	((< int 10)
	 (itoa int)
	)
	((<= 10 int 15)
	 (chr (+ int 55))
	)
	(t
	 (strcat
	   (ZL-JZ10->JZ16 (/ int 16))
	   (ZL-JZ10->JZ16 (rem int 16))
	 )
	)
  )
)
;;;=================================================================*
;;;功能:十六进制字符  转换为  十进制数值                           *
;;;参数:str -----十六进制字符,十六进制字符。                      *
;;;例如:(ZL-JZ16->JZ10 "2CEC")  返回 11500                         *
;;;      (ZL-JZ16->JZ10 "20D0")  返回  8400                         *
(defun ZL-JZ16->JZ10 (str / num len)
  (setq str (strcase str)) ;_转换为大写
  (cond
    ((<= (strlen str) 1)
     (setq num (ascii str))
     (cond ((<= 48 num 57) ;_ 0-9
	    (- num 48)
	   )
	   ((<= 65 num 70) ;_ A-F
	    (- num 55)
	   )
	   (t 0)
     )
    )
    ;;
    (t
     (setq len (strlen str))
     (+	(* (ZL-JZ16->JZ10 (substr str 1 (1- len))) 16)
	(ZL-JZ16->JZ10 (substr str len 1))
     )
    )
  )
)
;;;=================================================================*
;;;======================================================================================
;;;十进制转化为二进制
;;;例如:(deg->bin 3) 返回"11"
;;;      (deg->bin 5) 返回"101"
(defun ZL-JZ10->JZ2	(int / a b)
    (if	(< int 1)
	"0"
	(if (= int 1)
	    "1"
	    (progn
		(setq a	(/ int 2)
		      b	(- int (* a 2))
		)
		(strcat	(ZL-JZ10->JZ2 a)
			(itoa b)
		)
	    )
	)
    )
)
;;;======================================================================================
;;;二进制转化为十进制
;;;例如:(bin->deg "11")  返回 3
;;;      (deg->bin "101") 返回 5
(defun ZL-JZ2->JZ10	(str)
    (if	(<= (strlen str) 1)
	(if (= str "1")
	    1
	    0
	)
	(if (= (substr str 1 1) "1")
	    (+ (expt 2 (- (strlen str) 1))
	       (ZL-JZ2->JZ10 (substr str 2))
	    )
	    (ZL-JZ2->JZ10 (substr str 2))
	)
    )
)
  最后修改于 2021-11-11 22:18    阅读(?)评论(0)
 
表  情:
加载中...
 

请各位遵纪守法并注意语言文明