#x1234
is just a hex number and not a byte-encoding. You can easily define a byte encoding yourself.
For an instance with the following my-byte-decode function:
(defun my-byte-decode (list)
"Decode hex number big-endian encoded as sequence of bytes in a list."
(cl-reduce (lambda (hi lo) (+ (* #x100 hi) lo)) list))
You can decode a little endian encoded number (#x34 #x12)
by (my-byte-decode (nreverse '(#x34 #x12)))
.
For completeness the encoding function:
(defun my-byte-encode (number)
"Big-endian encode hex NUMBER.
See `my-byte-decode' for details on the encoding."
(unless (integerp number)
(user-error "Expected an integer"))
(let (ret)
(while (/= number 0)
(setq ret (cons (mod number #x100) ret))
(setq number (/ number #x100)))
(or ret (list 0))))
Test: (equal (my-byte-encode #x1234) '(#x12 #x34))
gives t
.
You get little-ending encoding by wrapping the my-byte-encode
into a nreverse
:
(equal (nreverse (my-byte-encode #x1234)) '(#x34 #x12))
gives t
.
Now I address the new version of your question:
How might I convert hex to decimal as little endian, so that #1234 is interpreted as 13330?
EDIT: We already discussed in the comments that the number #x0001
is the same as 1
. so the word size encoded in the string #x0001
through the leading zeros is inevitably lost when the Elisp interpreter reads the string and interprets it as a number.
If you know the word size in advance you can pad the read byte sequence with zeros:
(defun my-fill-front (list size val)
"If LIST is shorter than SIZE fill it at front.
Use a list with VAL for fitting the size."
(let ((len (length list)))
(if (< len size)
(append (make-list (- size len) val)
list)
list)))
Now you have all the tools above. Just use the encoding function, pad it with zeros such that it has the right size, transform to little endian, and decode:
(my-byte-decode (nreverse (my-fill-front (my-byte-encode #x1234) 2 0)))
The answer is 13330
as you wish.