From: keyboard
Subject: binary to hex format
Date: 
Message-ID: <1154586246.150811.16060@s13g2000cwa.googlegroups.com>
Suppose I have a list,
(0 0 0 0 0 0 0 0 1 0 0 0 1 0 0 0 1 1 1 1 1 1 1 1...)
I want to format it as a new list
(00 88 FF...)
which takes 8 elements of the first list and makes a hex number.

How can I do it?

keyboard

From: ··············@hotmail.com
Subject: Re: binary to hex format
Date: 
Message-ID: <1154613220.604593.86500@i3g2000cwc.googlegroups.com>
keyboard wrote:
> Suppose I have a list,
> (0 0 0 0 0 0 0 0 1 0 0 0 1 0 0 0 1 1 1 1 1 1 1 1...)
> I want to format it as a new list
> (00 88 FF...)
> which takes 8 elements of the first list and makes a hex number.

The concept of "hex number" is a false one. Integers are integers. You
can read and print them in various bases, but the number is the same.

It makes sense to combine one-bit integers into eight-bit integers; in
the default print base of 10 (decimal), this will print your result as
(0 136 255 ...) unless you mean (0 -20 -1 ...)

Others have helpfully provided code to help you in your conversion. I
hope my pedantry is not overly obnoxious.
From: Stefan Mandl
Subject: Re: binary to hex format
Date: 
Message-ID: <4jfcl2F7h689U1@news.dfncis.de>
If you want the hex-numbers as strings, you can do something like:

(defun hex (list)
   (if (null list) '()
       (destructuring-bind (d1 c1 b1 a1 d2 c2 b2 a2 &rest rest) list
         (cons
          (apply #'concatenate 'string
                 (mapcar #'string
                         (list (char "0123456789ABCDEF" (+ (* (+ (* (+ (* d1 2) c1) 2) b1) 2) a1))
                               (char "0123456789ABCDEF" (+ (* (+ (* (+ (* d2 2) c2) 2) b2) 2) a2)))))
        (hex rest)))))
From: ··············@hotmail.com
Subject: Re: binary to hex format
Date: 
Message-ID: <1154649559.154037.220420@h48g2000cwc.googlegroups.com>
Stefan Mandl wrote:
> If you want the hex-numbers as strings, you can do something like:
>
> (defun hex (list)
>    (if (null list) '()
>        (destructuring-bind (d1 c1 b1 a1 d2 c2 b2 a2 &rest rest) list
>          (cons
>           (apply #'concatenate 'string
>                  (mapcar #'string
>                          (list (char "0123456789ABCDEF" (+ (* (+ (* (+ (* d1 2) c1) 2) b1) 2) a1))
>                                (char "0123456789ABCDEF" (+ (* (+ (* (+ (* d2 2) c2) 2) b2) 2) a2)))))
>         (hex rest)))))

This is a bit of a pet peeve of mine. People seem to always miss the
functions

DIGIT-CHAR and DIGIT-CHAR-P

http://www.lispworks.com/documentation/HyperSpec/Body/f_digit_.htm#digit-char
http://www.lispworks.com/documentation/HyperSpec/Body/f_digi_1.htm#digit-char-p

(char "0123456789ABCDEF" num) --> (digit-char num 16)
From: Stefan Mandl
Subject: Re: binary to hex format
Date: 
Message-ID: <4jfhq9F7mlvkU1@news.dfncis.de>
> This is a bit of a pet peeve of mine. People seem to always miss the
> functions
> 
> DIGIT-CHAR and DIGIT-CHAR-P

No offense intended ;)

We all know CL is *BIG* and this is one of the corners I'm not
familiar with.

So, thanks for the hint!

regards,
Stefan
From: Petter Gustad
Subject: Re: binary to hex format
Date: 
Message-ID: <874pwt76be.fsf@gustad.com>
···············@hotmail.com" <············@gmail.com> writes:

> keyboard wrote:
> > Suppose I have a list,
> > (0 0 0 0 0 0 0 0 1 0 0 0 1 0 0 0 1 1 1 1 1 1 1 1...)
> > I want to format it as a new list
> > (00 88 FF...)
> > which takes 8 elements of the first list and makes a hex number.
> 
> The concept of "hex number" is a false one. Integers are integers. You
> can read and print them in various bases, but the number is the same.

True. You can even use a bit-vector instead of a list. The least
significant bit is element zero. Vectors are displayed in the REPL
with the least significant bit to the left. The above could be
represented as the following bit vector:

#*111111110001000100000000

If you written the most least significant bit to the left.

A function to convert a bit-vector to an integer could be defined as:

(defun bvi (bv)
  "bit-vector to integer conversion"
  (loop for bit across bv and
        i from 0
        sum (ash bit i))) 

Then you could display it out as you like:

CL-USER> (bvi #*111111110001000100000000)
35071
CL-USER> (format nil "~X" (bvi #*111111110001000100000000))
"88FF"
CL-USER> (format nil "~B" (bvi #*111111110001000100000000))
"1000100011111111"

Numbers when displayed as strings are shown with the most significant
digit to the left.

Petter
-- 
A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
From: Petter Gustad
Subject: Re: binary to hex format
Date: 
Message-ID: <871wrx76b5.fsf@gustad.com>
···············@hotmail.com" <············@gmail.com> writes:

> keyboard wrote:
> > Suppose I have a list,
> > (0 0 0 0 0 0 0 0 1 0 0 0 1 0 0 0 1 1 1 1 1 1 1 1...)
> > I want to format it as a new list
> > (00 88 FF...)
> > which takes 8 elements of the first list and makes a hex number.
> 
> The concept of "hex number" is a false one. Integers are integers. You
> can read and print them in various bases, but the number is the same.

True. You can even use a bit-vector instead of a list. The least
significant bit is element zero. Vectors are displayed in the REPL
with the least significant bit to the left. The above could be
represented as the following bit vector:

#*111111110001000100000000

If you written the most least significant bit to the left.

A function to convert a bit-vector to an integer could be defined as:

(defun bvi (bv)
  "bit-vector to integer conversion"
  (loop for bit across bv and
        i from 0
        sum (ash bit i))) 

Then you could display it out as you like:

CL-USER> (bvi #*111111110001000100000000)
35071
CL-USER> (format nil "~X" (bvi #*111111110001000100000000))
"88FF"
CL-USER> (format nil "~B" (bvi #*111111110001000100000000))
"1000100011111111"

Numbers when displayed as strings are shown with the most significant
digit to the left.

Petter
-- 
A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
From: Nathan Baum
Subject: Re: binary to hex format
Date: 
Message-ID: <Pine.LNX.4.64.0608030823510.24883@localhost>
On Thu, 2 Aug 2006, keyboard wrote:

> Suppose I have a list,
> (0 0 0 0 0 0 0 0 1 0 0 0 1 0 0 0 1 1 1 1 1 1 1 1...)
> I want to format it as a new list
> (00 88 FF...)
> which takes 8 elements of the first list and makes a hex number.
>
> How can I do it?
>
> keyboard

(defun partition (list count)
   (loop :for s = list :then (subseq s count) :while s
      :collect (subseq s 0 count)))

(defun formatify (list)
   (loop :for b :in (partition list 8)
      :collect (reduce #'+ (mapcar #'* b '(128 64 32 16 8 4 2 1)))))

(let ((*print-base* 16))
   (print (formatify '(0 0 0 0 0 0 0 0 1 0 0 0 1 0 0 0 1 1 1 1 1 1 1 1))))
From: Frank Buss
Subject: Re: binary to hex format
Date: 
Message-ID: <17zkeg181a3b3.1ht1dpld4pgcc.dlg@40tude.net>
Nathan Baum wrote:

> (defun partition (list count)
>    (loop :for s = list :then (subseq s count) :while s
>       :collect (subseq s 0 count)))
> 
> (defun formatify (list)
>    (loop :for b :in (partition list 8)
>       :collect (reduce #'+ (mapcar #'* b '(128 64 32 16 8 4 2 1)))))
> 
> (let ((*print-base* 16))
>    (print (formatify '(0 0 0 0 0 0 0 0 1 0 0 0 1 0 0 0 1 1 1 1 1 1 1 1))))

and a solution which needs linear time and with 2 digits per hex number:

(defun hex (bin)
  (let ((result '())
        (factor #x80)
        (accu 0))
    (dolist (digit bin)
      (incf accu (* factor digit))
      (setf factor (ash factor -1))
      (when (zerop factor)
        (setf factor #x80)
        (push accu result )
        (setf accu 0)))
    (unless (= #x80 factor)
      (push accu result))
    (nreverse result)))

(format nil "(~{~16,2,'0,,R~^ ~})"
        (hex '(0 0 0 0 0 0 0 0 1 0 0 0 1 0 0 0 1 1 1 1 1 1 1 1)))

-- 
Frank Buss, ··@frank-buss.de
http://www.frank-buss.de, http://www.it4-systems.de
From: Frode Vatvedt Fjeld
Subject: Re: binary to hex format
Date: 
Message-ID: <2h7j1qi85q.fsf@vserver.cs.uit.no>
"keyboard" <···········@gmail.com> writes:

> Suppose I have a list,
> (0 0 0 0 0 0 0 0 1 0 0 0 1 0 0 0 1 1 1 1 1 1 1 1...)
> I want to format it as a new list
> (00 88 FF...)
> which takes 8 elements of the first list and makes a hex number.

There's a function in my "picl" program (an assembler for the PIC
microcontroller) that does resizing of bytes:

(picl::byte-list-resize '(0 0 0 0 0 0 0 0 1 0 0 0 1 0 0 0 1 1 1 1 1 11 1)
                         1 8 :big-endian)

=> (#x0 #x88 #xff)

The 1 says that the original byte-size is 1 bit, the desired size is
8, and byte ordering is "big-endian"
(i.e. most-significant-byte-first, or in this particular case,
most-significant-bit-first, since the byte-size is 1).

<URL:http://www.cs.uit.no/~frodef/sw/picl.lisp>

-- 
Frode Vatvedt Fjeld
From: keyboard
Subject: Re: binary to hex format
Date: 
Message-ID: <1154658966.934069.293560@i3g2000cwc.googlegroups.com>
keyboard schrieb:

> Suppose I have a list,
> (0 0 0 0 0 0 0 0 1 0 0 0 1 0 0 0 1 1 1 1 1 1 1 1...)
> I want to format it as a new list
> (00 88 FF...)
> which takes 8 elements of the first list and makes a hex number.
>
> How can I do it?
>
> keyboard

Thank you all for the wise advise.

My problem is that before I saw your reply/answers, I found my question
was hard - loop or pattern matching. After I read the posts, it's so
easy and direct.

What should I do more to improve my programming skill in Clisp ? - I
have been using it for almost 2 years, not for real projects but for
fun and routine trials.

keyboard
From: GP lisper
Subject: Re: binary to hex format
Date: 
Message-ID: <slrned6fb7.7nc.spambait@phoenix.clouddancer.com>
On 2 Aug 2006 23:24:06 -0700, <···········@gmail.com> wrote:
> Suppose I have a list,
> (0 0 0 0 0 0 0 0 1 0 0 0 1 0 0 0 1 1 1 1 1 1 1 1...)
> I want to format it as a new list
> (00 88 FF...)
> which takes 8 elements of the first list and makes a hex number.

hex numbers are made from 4 bits

-- 
Reply-To email is ignored.

-- 
Posted via a free Usenet account from http://www.teranews.com
From: Pascal Bourguignon
Subject: Re: binary to hex format
Date: 
Message-ID: <873bccmmj1.fsf@thalassa.informatimago.com>
GP lisper <········@CloudDancer.com> writes:

> On 2 Aug 2006 23:24:06 -0700, <···········@gmail.com> wrote:
>> Suppose I have a list,
>> (0 0 0 0 0 0 0 0 1 0 0 0 1 0 0 0 1 1 1 1 1 1 1 1...)
>> I want to format it as a new list
>> (00 88 FF...)
>> which takes 8 elements of the first list and makes a hex number.
>
> hex numbers are made from 4 bits

hex digits. hex numbers are made of any number of quadbits.

-- 
__Pascal Bourguignon__                     http://www.informatimago.com/
I need a new toy.
Tail of black dog keeps good time.
Pounce! Good dog! Good dog!