Convert Byte String to Int in Scheme

孤街浪徒 提交于 2019-12-12 18:18:10

问题


I have code like this to convert hex into byte string

(define (word->bin s)
  (let ((n (string->number s)))
    (bytes (bitwise-and (arithmetic-shift n -24) #xFF)
    (bitwise-and (arithmetic-shift n -16) #xFF)
    (bitwise-and (arithmetic-shift n -8) #xFF)
    (bitwise-and n #xFF))))
(word->bin "#x10000002")

I'm thinking of a similar function to convert binary into integers, then print it. The end result is the binary translated to hex. Some helpful links: http://download.plt-scheme.org/doc/372/html/mzscheme/mzscheme-Z-H-11.html#node_sec_11.2.1

http://docs.plt-scheme.org/reference/bytestrings.html#(def.((quote.~23~25kernel)._bytes-~3estring/utf-8))


回答1:


I'm not sure that this is what you're looking for, or even if you're using PLT, but if you do, then you should look at the integer-bytes->integer and integer->integer-bytes functions that are included in PLT. Note that these create byte strings with binary content -- so it might be different than what you're trying to do here.

(And if you're using version 372, then you should really upgrade.)



来源:https://stackoverflow.com/questions/1597543/convert-byte-string-to-int-in-scheme

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!