如何在方案中将字符串转换为二进制

发布于 2025-01-22 11:58:07 字数 1057 浏览 3 评论 0原文

因此,我遇到了这个问题,我一直在尝试编码一种将二进制号码转换为方案(R5RS)字符串的方法,但是我一直在遇到一些问题。我和我的导师试图在一个小时的时间内解决它,但我们无法解决它。

实际上,截至我完成最后一场会议时,他抛弃了以下内容:

    (define binary->string (lambda (lst)
                         (define str (make-string 0)) 
                         (letrec ((recurse (lambda (l)
                                             ; turn into not null
                                             (if (null? l)
                                                 (lambda ()
                                                   (set! str (string-append str (number->string (car l))))
                                                   (recurse (cdr l)))
                                                 )
                                             )
                                           )
                                  )
                           (recurse lst)
                          )
                         str)
  )

(binary->string '(0 1 0 1 0 0 1 1))

看看它说“变成无效”的地方?好吧,我们遇到了一些问题,试图将数字转换为无效。我肯定希望这会有所帮助。谢谢!

So, I've ran into this problem where I've been trying to code a means to convert a binary number to a string in Scheme (R5RS), but I've been having some....problems. My tutor and I tried to solve it over the course of an hour, but we haven't been able to solve it.

In fact, as of the time I finished the last session, he left behind the following:

    (define binary->string (lambda (lst)
                         (define str (make-string 0)) 
                         (letrec ((recurse (lambda (l)
                                             ; turn into not null
                                             (if (null? l)
                                                 (lambda ()
                                                   (set! str (string-append str (number->string (car l))))
                                                   (recurse (cdr l)))
                                                 )
                                             )
                                           )
                                  )
                           (recurse lst)
                          )
                         str)
  )

(binary->string '(0 1 0 1 0 0 1 1))

See where it says "turn into not null"? Well, we've had some problems trying to convert the number into not null. I sure hope this helps. Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

窝囊感情。 2025-01-29 11:58:07

所以你想要这个结果吗?

> (binary->string '(0 1 0 1 0 0 1 1))
"01010011"

我看到您已经使用过 string-append number-> string ,因此您还应使用 apply map

(define (binary->string arg)
  (apply string-append
         (map number->string arg)))

> (binary->string '(0 1 0 1 0 0 1 1))
"01010011"

如果您也想将其转换为小数号,请使用 string- string- string- string- string-&gt;数字 < /a>带有 radix 2:

> (string->number "01010011" 2)
83

So you want this result?

> (binary->string '(0 1 0 1 0 0 1 1))
"01010011"

I see you've already used string-append and number->string, so you should also use apply and map:

(define (binary->string arg)
  (apply string-append
         (map number->string arg)))

> (binary->string '(0 1 0 1 0 0 1 1))
"01010011"

If you also want to convert it into decimal number, use string->number with radix 2:

> (string->number "01010011" 2)
83
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文