<commit_before>;;;; -*- Mode: LISP; Syntax: COMMON-LISP; Package: BIT-SMASHER; Base: 10 -*-
;;;; conversion.lisp

;;;; Copyright (c) 2014--2015, "the Phoeron" <NAME> <//thephoeron.com>
;;;; See LICENSE for additional information.

(in-package :bit-smasher)

(defun hex->bits (x)
  "Return the bit-vector for hexadecimal string X."
  (let ((binlist (loop for c across x collect (hex-to-bit-lookup c))))
    (apply #'concatenate 'bit-vector binlist)))

(defun hex->octets (x)
  "Return the octet-vector for hexadecimal string X."
  (ironclad:hex-string-to-byte-array x))

(defun hex->int (x)
  "Return the integer value for hexadecimal string X."
  (octets->int (hex->octets x)))

(defun octets->hex (o)
  "Return the hexadecimal string for octet-vector O."
  (ironclad:byte-array-to-hex-string o))

(defun octets->int (o)
  "Return the integer value for octet-vector O."
  (ironclad:octets-to-integer o))

(defun octets->bits (o)
  "Return the bit-vector for octet-vector O."
  (hex->bits (octets->hex o)))

(defun int->octets (n)
  "Return the octet-vector for integer N."
  (ironclad:integer-to-octets n))

(defun int->hex (n)
  "Return the hexadecimal string for integer N."
  (octets->hex (int->octets n)))

(defun int->bits (n)
  "Return the bit-vector for integer N."
  (hex->bits (int->hex n)))

(defun bits->hex (data)
  "Return the hexadecimal string for bit-vector DATA."
  (format nil "~X" (bit-vector-integer-value-and-place data)))

(defun bits->int (data)
  "Return the integer value for bit-vector DATA."
  (bit-vector-integer-value-and-place data))

(defun bits->octets (data order)
  "Return the octet-vector for bit-vector DATA, significant to ORDER."
  (ironclad:integer-to-octets (bits->int data) :n-bits order))

(defun hex<- (data)
  (cond ((and (typep data 'integer)
              (= data 0))
         "00")
        ((typep data 'integer)
         (int->hex data))
        ((typep data 'bit-vector)
         (bits->hex data))
        ((or (typep data '(vector (unsigned-byte 8)))
             (typep data '(simple-array (unsigned-byte 8) (*))))
         (octets->hex data))
        ((typep data 'string)
         data)
        (t (error "Type of value N not recognized."))))

(defun octets<- (data)
  (cond ((and (typep data 'integer)
              (= data 0))
         (hex->octets "00"))
        ((typep data 'integer)
         (int->octets data))
        ((typep data 'bit-vector)<commit_msg>bugfix: bits->octets<commit_after>;;;; -*- Mode: LISP; Syntax: COMMON-LISP; Package: BIT-SMASHER; Base: 10 -*-
;;;; conversion.lisp

;;;; Copyright (c) 2014--2015, "the Phoeron" <NAME> <//thephoeron.com>
;;;; See LICENSE for additional information.

(in-package :bit-smasher)

(defun hex->bits (x)
  "Return the bit-vector for hexadecimal string X."
  (let ((binlist (loop for c across x collect (hex-to-bit-lookup c))))
    (apply #'concatenate 'bit-vector binlist)))

(defun hex->octets (x)
  "Return the octet-vector for hexadecimal string X."
  (ironclad:hex-string-to-byte-array x))

(defun hex->int (x)
  "Return the integer value for hexadecimal string X."
  (octets->int (hex->octets x)))

(defun octets->hex (o)
  "Return the hexadecimal string for octet-vector O."
  (ironclad:byte-array-to-hex-string o))

(defun octets->int (o)
  "Return the integer value for octet-vector O."
  (ironclad:octets-to-integer o))

(defun octets->bits (o)
  "Return the bit-vector for octet-vector O."
  (hex->bits (octets->hex o)))

(defun int->octets (n)
  "Return the octet-vector for integer N."
  (ironclad:integer-to-octets n))

(defun int->hex (n)
  "Return the hexadecimal string for integer N."
  (octets->hex (int->octets n)))

(defun int->bits (n)
  "Return the bit-vector for integer N."
  (hex->bits (int->hex n)))

(defun bits->hex (data)
  "Return the hexadecimal string for bit-vector DATA."
  (format nil "~X" (bit-vector-integer-value-and-place data)))

(defun bits->int (data)
  "Return the integer value for bit-vector DATA."
  (bit-vector-integer-value-and-place data))

(defun bits->octets (data)
  "Return the octet-vector for bit-vector DATA, significant to ORDER."
  (ironclad:integer-to-octets (bits->int data)))

(defun hex<- (data)
  (cond ((and (typep data 'integer)
              (= data 0))
         "00")
        ((typep data 'integer)
         (int->hex data))
        ((typep data 'bit-vector)
         (bits->hex data))
        ((or (typep data '(vector (unsigned-byte 8)))
             (typep data '(simple-array (unsigned-byte 8) (*))))
         (octets->hex data))
        ((typep data 'string)
         data)
        (t (error "Type of value N not recognized."))))

(defun octets<- (data)
  (cond ((and (typep data 'integer)
              (= data 0))
         (hex->octets "00"))
        ((typep data 'integer)
         (int->octets data))
        ((typep data 'bit-vector)
{'fraction_non_alphanumeric': 0.1644886952914333, 'fraction_numerical': 0.0074673304293715, 'mean_word_length': 3.0351464435146442, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 60, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3986374', 'n_tokens_mistral': 1748, 'n_tokens_neox': 1692, 'n_words': 511}