all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* write strings with bits
@ 2023-04-18 17:08 Emanuel Berg
  0 siblings, 0 replies; only message in thread
From: Emanuel Berg @ 2023-04-18 17:08 UTC (permalink / raw)
  To: help-gnu-emacs

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




^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2023-04-18 17:08 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-04-18 17:08 write strings with bits Emanuel Berg

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.