将大数转换为十六进制
我想将一个大数字(实际上是从 unix 纪元时间开始计算的当前时间(以秒为单位))转换为十六进制字符串。
我很久以前就破解了一些代码,(我认为)它可以在早期版本的 emacs 上运行,但它在我现在使用的 emacs 23 上不再运行(可能是我搞乱了代码)。
我写这篇文章时遇到的困难是,这个数字很大,无法作为某些函数的一个参数来处理,所以我必须将其划分,做一些事情,然后将它们放在一起。例如,如果您向 string-to-number
提供一个大数字,即使我想要一个整数,它也会返回一个浮点数。
有人可以建议重写它吗?
以下是我编写的损坏的代码。它不起作用,但可能会让您了解我正在尝试做什么。首先,这些都是下面使用的子例程:
(defun seconds-since-origin ()
(string-to-number (concat
(number-to-string (- (string-to-number
(substring (format-time-string "%s") 0 5)
) 10000))
(substring (format-time-string "%s") 5)
)))
(defun hexatridecimal (decimal)
(concat
(let ((quotient (/ decimal 36))) (if (> quotient 35)
(hexatridecimal quotient)
(hexatridecimal-digit quotient)
))
(hexatridecimal-digit (% decimal 36))
))
(defun hexatridecimal-digit (number)
(nth number (list
"0" "1" "2" "3" "4" "5" "6" "7" "8" "9" "a" "b" "c" "d" "e" "f" "g" "h" "i" "j"
"k" "l" "m" "n" "o" "p" "q" "r" "s" "t" "u" "v" "w" "x" "y" "z"))
)
以下是将被调用的主函数:
(hexatridecimal (seconds-since-origin))
I want to turn a large number, actually the present time in seconds counted from the unix epoch time, into a hexatridecimal string.
I hacked some code a long time ago, which (I think) worked on an earlier version of emacs, but it does not work any more on emacs 23 that I use now (It might be possible that I messed up the code).
The difficulty at the time I wrote it was that the number was large to be handled as one argument for certain functions, so I had to divide it, do something, and put them together. For example, if you give a large number to string-to-number
, it gives back a float even though I want an integer.
Can anyone suggest a rewrite of it?
Following is the broken code that I wrote. It does not work, but may give you an idea of what I am trying to do. First, these are all subroutines that are used below:
(defun seconds-since-origin ()
(string-to-number (concat
(number-to-string (- (string-to-number
(substring (format-time-string "%s") 0 5)
) 10000))
(substring (format-time-string "%s") 5)
)))
(defun hexatridecimal (decimal)
(concat
(let ((quotient (/ decimal 36))) (if (> quotient 35)
(hexatridecimal quotient)
(hexatridecimal-digit quotient)
))
(hexatridecimal-digit (% decimal 36))
))
(defun hexatridecimal-digit (number)
(nth number (list
"0" "1" "2" "3" "4" "5" "6" "7" "8" "9" "a" "b" "c" "d" "e" "f" "g" "h" "i" "j"
"k" "l" "m" "n" "o" "p" "q" "r" "s" "t" "u" "v" "w" "x" "y" "z"))
)
The following will be the main function that will be called:
(hexatridecimal (seconds-since-origin))
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用 64 位 Emacs(可以像您正在使用的那样处理整数),进行以下更改:
注意:这确实假设 64 位 Emacs 具有更多位 比 32 位版本大的整数。
如果您想在 32 位 Emacs 上工作,可以使用 calc' s big-int 功能可以为您进行数学计算,并适当调整代码。
编辑添加:
这是使用
calc
的 bignum 功能的版本:Using a 64-bit Emacs (which can handle integers like you're using), make the following changes:
Note: this does assume a 64-bit Emacs which has more bits for large integers than the 32-bit builds.
If you want to work on a 32-bit Emacs, you can use calc's big-int capability to do the maths for you, and adjust the code appropriately.
Edited to add:
Here's the version using
calc
's bignum capabilities: