Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Type conversion would be beneficial #80

Closed
skx opened this issue Nov 9, 2022 · 1 comment · Fixed by #89
Closed

Type conversion would be beneficial #80

skx opened this issue Nov 9, 2022 · 1 comment · Fixed by #89
Assignees

Comments

@skx
Copy link
Owner

skx commented Nov 9, 2022

I've got a bunch of binary operations working for my (embedded) use-case, but they're fiddlier than they need to be.

Converting a number to binary is easy:

(set! dec_base (fn* ( n b )
                    "Convert the given number to a string representation of the number in the given base.

Example: (dec_base 254 2)  ; int -> binary
Example: (dec_base 201 16) ; int -> hex"
    (if (< n b)
        (chr (+ n (if (< n 10) 48 55)))
      (join (list (dec_base (/ n b) b) (dec_base (% n b) b)))
    )))

But to convert back I've fallen back to using eval:

(set! bin2dec (fn* (n)
                   "Convert the given binary string to an integer.

Example: (print (bin2dec \"1101\"))
See-also:  dec_base, hex2dec"
                   (eval (join (list "0b" n)))))

(set! hex2dec (fn* (n)
                   "Convert the given hex string to an integer.

Example: (print (hex2dec \"ff\"))
See-also:  dec_base, bin2dec"
                   (eval (join (list "0x" n)))))

Using eval like this is probably safe, but it feels like a code-smell.

Adding a conversion/parsing routine as native would help. Consider moving the rest to the stdlib once I've validated they're sane:

;;; bits.lisp - Binary functions

;; This file contains binary functions "and", "or", and "xor" for bits
;; and integers.
;;
;; We have some numerical conversion routines for converting between
;; integers and hex/binary numbers.
;;
;; Finally we also have some routines for testing whether a given bit
;; in a number is set, or unset.  As well as forcibly clearing or setting
;; them.
;;
;; NOTE: Most of these routines will fail for "large" values.  64-bit is
;; fine, after that good luck.
;;
@skx
Copy link
Owner Author

skx commented Nov 9, 2022

Two choices. We can add:

  • (number "33")
    • Convert a string to a number.
  • (binary 32)
    • Convert a number to a binary-string
  • (hex 255)
    • Convert a number to a hexadecimal string

Or we could add:

  • (number "33")
    • Convert a string to a number.
  • (base 32 2)
    • Convert a number to a binary-string
  • (base 255 16)
    • Convert a number to a hexadecimal string.

We could add wrappers for (binary) and (hex) using (base).

@skx skx self-assigned this Nov 12, 2022
skx added a commit that referenced this issue Nov 12, 2022
This pull-request closes #80, by implementing support for numeric
conversion - decimal to binary/hex, and the other way round.
@skx skx closed this as completed in #89 Nov 12, 2022
skx added a commit that referenced this issue Nov 12, 2022
These should have been part of #80, oops.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant