明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 2106|回复: 2

[求助]请帮忙给个十六进制转十进制的程序

[复制链接]
发表于 2009-3-16 15:26:00 | 显示全部楼层 |阅读模式
请帮忙给个十六进制转十进制的程序,谢谢啦
发表于 2009-3-17 16:24:00 | 显示全部楼层
(defun hex2ocx(hex)
  (setq hex_len (strlen hex)); hex_len=len(hex)
  (setq i 0);i=0
  (setq ocx 0);ocx=0
  (while (> hex_len 0);while hex_len >=0
    (setq m_hex (substr hex hex_len 1));m_hex=mid(hex,hex_len,1)
    (if (and (>= m_hex "a") (<= m_hex "f")) (setq tmp (- (ascii m_hex) 87)));if m_hex>="a" and m_hex <="f"   tmp=asc(m_hex)-87
    (if (and (>= m_hex "A") (<= m_hex "F")) (setq tmp (- (ascii m_hex) 55)));if m_hex>="A" and m_hex <="F"   tmp=asc(m_hex)-55
    (if (and (>= m_hex "0") (<= m_hex "9")) (setq tmp (atoi m_hex)));if m_hex>="0" and m_hex <="9"   TMP=VAL(m_hex)
    (SETQ OCX (+ ocx (* tmp (expt 16 i))));计算N进制转10进制为:ocx=m*n^i-1+ocx  //i为m所处N进制的位数,如个位为1
    (setq i (1+ i))
    (setq hex_len (1- hex_len))
    )
  ocx
  )
发表于 2009-3-17 16:31:00 | 显示全部楼层

(SETQ OCX (+ ocx (* tmp (expt 16 i))))

将这句里的"16"改成其它数可算出你要的任意进制的十进制数.

如改为"8",可计算八进制,如改为"2"可计算二进制.当然,上面的判断代码要进行相应的修改,

还有,程序的参数应该是字符串类型.

您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|CAD论坛|CAD教程|CAD下载|联系我们|关于明经|明经通道 ( 粤ICP备05003914号 )  
©2000-2023 明经通道 版权所有 本站代码,在未取得本站及作者授权的情况下,不得用于商业用途

GMT+8, 2025-2-27 19:57 , Processed in 0.152468 second(s), 25 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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