将大数转换为十六进制

发布于 2024-10-25 04:31:44 字数 1148 浏览 3 评论 0原文

我想将一个大数字(实际上是从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夜清冷一曲。 2024-11-01 04:31:44

使用 64 位 Emacs(可以像您正在使用的那样处理整数),进行以下更改:

(defun seconds-since-origin ()
  (string-to-number (format-time-string "%s")))

(defun hexatridecimal (decimal)
  (let ((quotient (/ decimal 36))
        (remainder (% decimal 36)))
    (concat 
     (if (> quotient 0)
         (hexatridecimal quotient)
       "")
     (hexatridecimal-digit remainder))))

注意:这确实假设 64 位 Emacs 具有更多位 比 32 位版本大的整数

如果您想在 32 位 Emacs 上工作,可以使用 calc' s big-int 功能可以为您进行数学计算,并适当调整代码。

编辑添加:
这是使用 calc 的 bignum 功能的版本:

(require 'calc)
(require 'calc-ext)
(defun seconds-since-origin ()
  (math-read-number-simple (format-time-string "%s")))
(defun hexatridecimal (decimal-bignum)
  (let* ((q-r (math-idivmod decimal-bignum 36))
     (quotient (car q-r))
     (remainder (cdr q-r)))
(concat 
 (if (math-lessp 0 quotient)
     (hexatridecimal quotient)
   "")
 (hexatridecimal-digit remainder))))

Using a 64-bit Emacs (which can handle integers like you're using), make the following changes:

(defun seconds-since-origin ()
  (string-to-number (format-time-string "%s")))

(defun hexatridecimal (decimal)
  (let ((quotient (/ decimal 36))
        (remainder (% decimal 36)))
    (concat 
     (if (> quotient 0)
         (hexatridecimal quotient)
       "")
     (hexatridecimal-digit remainder))))

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:

(require 'calc)
(require 'calc-ext)
(defun seconds-since-origin ()
  (math-read-number-simple (format-time-string "%s")))
(defun hexatridecimal (decimal-bignum)
  (let* ((q-r (math-idivmod decimal-bignum 36))
     (quotient (car q-r))
     (remainder (cdr q-r)))
(concat 
 (if (math-lessp 0 quotient)
     (hexatridecimal quotient)
   "")
 (hexatridecimal-digit remainder))))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文