help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

write strings with bits


From: Emanuel Berg
Subject: write strings with bits
Date: Tue, 18 Apr 2023 19:08:44 +0200
User-agent: Gnus/5.13 (Gnus v5.13)

Some fun code to write strings in binary ...

Cred to whoever wrote that at codereview.

There are more logical binary operands BTW, but don't look for
"logor", it is called `logior' ...

;;; -*- lexical-binding: t -*-
;;
;; cred:
;;   https://codereview.stackexchange.com/q/186840
;;
;; this file:
;;   https://dataswamp.org/~incal/emacs-init/str-to-bits.el

(require 'esh-util)

(defun number-to-bitstring (num)
  (let ((s ""))
    (while (> num 0)
      (setq s (concat (number-to-string (logand num 1)) s))
      (setq num (ash num -1)) )
    (if (string= "" s)
        "0"
      s) ))

(defalias 'dec2bits #'number-to-bitstring)

;; (dec2bits ?a) ; 1100001

(defun str-to-bits (str &optional delim)
  (interactive "sString: ")
  (or delim (setq delim " "))
  (let ((bits))
    (dolist (c (string-to-list str) (mapconcat #'eshell-stringify bits delim))
      (push (number-to-bitstring c) bits) )))

;; (str-to-bits "What's up Emacs community?") ; 111111 1110000 1110101 [...]

(provide 'str-to-bits)

-- 
underground experts united
https://dataswamp.org/~incal




reply via email to

[Prev in Thread] Current Thread [Next in Thread]