From: Bob Felts
Subject: Integer to Character conversion
Date: 
Message-ID: <1hslw8p.x4kuq71t0cwqoN%wrf3@stablecross.com>
Many months ago a co-worker and I were having a contest to see whose
programming language was bigger^H^H^H^H^H^H better.  Someone had an
e-mail sig of binary numbers, e.g.:
   
   01000001 01110101 01110100 00100000 01110110 ...

The task was to convert this to ASCII to see what it said.  He chose C
while I used Lisp.  My solution was:

(defun dcb-to-ascii (dcb)
 "Convert a list of decimal-coded-binary to ASCII"
  (map 'string 'character
       (mapcar #'(lambda (n) (parse-integer (write-to-string n) :radix
2)) dcb)))

This worked on my PowerBook using OpenMCL and my friend conceeded the
contest.

But now that I have a MacBook and have switched to SBCL, this no longer
runs.  I don't remember how I ever got the idea that (character ...)
would accept an integer as an argument, since the Hyperspec says that it
doesn't.

Kent Pittman describes a way to do this conversion at
http://www.nhplace.com/kent/PS/EQUAL.html, but I'm lazy.  I don't want
to have to type in an ASCII table.

Is there an easy solution or am I going to have to do some actual work?

From: Kaz Kylheku
Subject: Re: Integer to Character conversion
Date: 
Message-ID: <1169934049.257480.8470@j27g2000cwj.googlegroups.com>
On Jan 27, 1:08 pm, ····@stablecross.com (Bob Felts) wrote:
> Is there an easy solution or am I going to have to do some actual work?

Are you looking for CODE-CHAR?
From: Bob Felts
Subject: Re: Integer to Character conversion
Date: 
Message-ID: <1hslyqe.188ewmp1wjd3gqN%wrf3@stablecross.com>
Kaz Kylheku <········@gmail.com> wrote:

> On Jan 27, 1:08 pm, ····@stablecross.com (Bob Felts) wrote:
> > Is there an easy solution or am I going to have to do some actual work?
> 
> Are you looking for CODE-CHAR?

Yes.  And now I feel stupid.  I did a Google search on "lisp integer
character conversion" and didn't see anything.  I did try to find it
before asking.

Thanks, everyone.
From: Ken Tilton
Subject: Re: Integer to Character conversion
Date: 
Message-ID: <wNQuh.6724$Qc7.6038@newsfe08.lga>
Bob Felts wrote:
> Kaz Kylheku <········@gmail.com> wrote:
> 
> 
>>On Jan 27, 1:08 pm, ····@stablecross.com (Bob Felts) wrote:
>>
>>>Is there an easy solution or am I going to have to do some actual work?
>>
>>Are you looking for CODE-CHAR?
> 
> 
> Yes.  And now I feel stupid.  I did a Google search on "lisp integer
> character conversion" and didn't see anything.  I did try to find it
> before asking.

Did you try (apropos "CHAR")? Granted, you have to know enough not to do 
it on "CHARACTER"), and the output would be voluminous. The groovy ACL 
apropos dialogue lets me specify exported symbols for functions of just 
CL, and that knocks it down to a couple dozen. If you are not blessed 
with access to ACL on win32 or Wilson rackets or Nike tennis outfits*, 
there is always the "character dictionary" of the hyperspec:

   http://www.lisp.org/HyperSpec/Body/sec_the_chara_s_dictionary.html

ken

* Serena Williams used her championship speech to shill both. After the 
way she looked destroying Sharapova, no one was going to try to stop her. k

-- 
Well, I've wrestled with reality for 35 years, Doctor, and
I'm happy to state I finally won out over it.
                                   -- Elwood P. Dowd

In this world, you must be oh so smart or oh so pleasant.
                                   -- Elwood's Mom
From: Bob Felts
Subject: Re: Integer to Character conversion
Date: 
Message-ID: <1hsm24f.1d9woa1fmc6vuN%wrf3@stablecross.com>
Ken Tilton <·········@gmail.com> wrote:

> Bob Felts wrote:
> > Kaz Kylheku <········@gmail.com> wrote:
> > 
> > 
> >>On Jan 27, 1:08 pm, ····@stablecross.com (Bob Felts) wrote:
> >>
> >>>Is there an easy solution or am I going to have to do some actual work?
> >>
> >>Are you looking for CODE-CHAR?
> > 
> > 
> > Yes.  And now I feel stupid.  I did a Google search on "lisp integer
> > character conversion" and didn't see anything.  I did try to find it
> > before asking.
> 
> Did you try (apropos "CHAR")? Granted, you have to know enough not to do
> it on "CHARACTER"), 

That's the problem.  I tried  (apropos <various guesses>), but obviously
I didn't try the right things.

> and the output would be voluminous. The groovy ACL 
> apropos dialogue lets me specify exported symbols for functions of just
> CL, and that knocks it down to a couple dozen. If you are not blessed
> with access to ACL on win32 

"win32"?  What is that?  Some kind of cabbage?  ;-)

> or Wilson rackets or Nike tennis outfits*, 
> there is always the "character dictionary" of the hyperspec:
> 
>    http://www.lisp.org/HyperSpec/Body/sec_the_chara_s_dictionary.html
> 

That was helpful.  I'll try to remember that.

> ken
> 
> * Serena Williams used her championship speech to shill both. After the
> way she looked destroying Sharapova, no one was going to try to stop her. k

I'd buy tickets to a Tilton v. Williams matchup on c.l.l.  ;-)
From: Ken Tilton
Subject: Re: Integer to Character conversion
Date: 
Message-ID: <mfRuh.38$Ra5.16@newsfe09.lga>
Bob Felts wrote:
> Ken Tilton <·········@gmail.com> wrote:
> 

>>CL, and that knocks it down to a couple dozen. If you are not blessed
>>with access to ACL on win32 
> 
> 
> "win32"?  What is that?  Some kind of cabbage?  ;-)

Oh, sorry, in a sense, yes. win32 (or os x) is a luxury OS for us rich / 
famous lifestyle folks who can throw away a couple hundred bucks (aka 
"cabbage") instead of a hundred hours (of "free" time) to get an OS up 
and running, or blow eight benjamins on a proper Lisp environment.

> I'd buy tickets to a Tilton v. Williams matchup on c.l.l.  ;-)

That is just about the only place I would escape with my life.

kzo

-- 
Well, I've wrestled with reality for 35 years, Doctor, and
I'm happy to state I finally won out over it.
                                   -- Elwood P. Dowd

In this world, you must be oh so smart or oh so pleasant.
                                   -- Elwood's Mom
From: robert maas, see http://tinyurl.com/uh3t
Subject: Re: Integer to Character conversion
Date: 
Message-ID: <rem-2007jan29-006@yahoo.com>
> From: ····@stablecross.com (Bob Felts)
> > Are you looking for CODE-CHAR?
> Yes.  And now I feel stupid.  I did a Google search on "lisp integer
> character conversion" and didn't see anything.  I did try to find it
> before asking.

IMO there should be a nice organization of lisp functions that
relate various data types, such as integer related to character
here, plus a search engine to directly go to the appropriate
atype-rel-btype section. I'm working on the first part now (but
it'll be a long while before it's ready for your purpose), and if
anybody pays me I'd work on the search engine too.

For an example of a two-level browsing version, it might look
something like this on-screen (Web page):
[Numbers]
  [Integers]
    and [Rationals]  [Characters]  [Symbols]  [Floats]  [Complexes]
  [Rationals]
    and [Integers]  [Floats]  [Complexes]
  [Floats]
    and [Rationals]  [Integers]  [Complexes]
  [Complexes]
    and [Real/Rational/Integer]
[Sequences]
  [Strings]
    and [Characters]  [Lists]  [Symbols]  [Sequences]
  [Lists]
    and [Strings]  [DottedPairs]  [Sequences]  [LambdaExpressions]
[Characters]
  and [Strings]  [Integers]  [Symbols]
[Symbols]
  and [Strings]  [Packages]  [PropertyLists]  [Functions]  [Values]
[Packages]
  and [Symbols]
etc. (things inside [brackets] are buttons you can click)
So in the sub-menu for Characters you can click Integers, or vice
versa, to arrive at the same WebPage describing conversions between
those two types in either direction or coordinating them in
parallel.

One caveat: I'm doing this "cookbook" for several different
languages together, currently six (php/perl/lisp/c/c++/java), so
you can not only see mappings from English descriptions to the
appropriate library call or operator primitive in each language,
but also mapping from what you already know how to do in one
language to how to do "the same thing" (or nearly equivalent) in
another language. See my posting in comp.programming for link to
link to this project, and general discussion. Proofreaders (for
technical content and also typos) would be appreciated if anyone
thinks the project is worthwhile.
From: Michael
Subject: Re: Integer to Character conversion
Date: 
Message-ID: <2007012716362016807-spamtrap@ectosphenocom>
On 2007-01-27 16:08:36 -0500, ····@stablecross.com (Bob Felts) said:

> But now that I have a MacBook and have switched to SBCL, this no longer
> runs.  I don't remember how I ever got the idea that (character ...)
> would accept an integer as an argument, since the Hyperspec says that it
> doesn't.

code-char
From: Bob Felts
Subject: Re: Integer to Character conversion
Date: 
Message-ID: <1hslycl.1phupm596ocowN%wrf3@stablecross.com>
Michael <········@ectospheno.com> wrote:

> On 2007-01-27 16:08:36 -0500, ····@stablecross.com (Bob Felts) said:
> 
> > But now that I have a MacBook and have switched to SBCL, this no longer
> > runs.  I don't remember how I ever got the idea that (character ...)
> > would accept an integer as an argument, since the Hyperspec says that it
> > doesn't.
> 
> code-char

D'oh!  Thanks.
From: Kaz Kylheku
Subject: Re: Integer to Character conversion
Date: 
Message-ID: <1169951826.577437.186530@a75g2000cwd.googlegroups.com>
On Jan 27, 1:08 pm, ····@stablecross.com (Bob Felts) wrote:
> Many months ago a co-worker and I were having a contest to see whose
> programming language was bigger^H^H^H^H^H^H better.  Someone had an
> e-mail sig of binary numbers, e.g.:
>
>    01000001 01110101 01110100 00100000 01110110 ...
>
> The task was to convert this to ASCII to see what it said.  He chose C
> while I used Lisp.

This is an actual utility you could use as a filter from a text editor 
over such article bodies to decode them. If it works. It's completely 
untested, since I don't have a C compiler installed on my machine! 
Lisp only around here. :)

#include <stdio.h>
#include <stdlib.h>

/*
 * Filter a text stream, replacing sequences of seven consecutive
 * 1's and 0's with the corresponding character in the execution
 * environment's character set. All other text is passed through,
 * including incomplete sequences of 1's and 0's.
 */

int main(void)
{
  int ch, acc = 0;
  char lexeme[7], *lp = lexeme;

  while ((ch = getchar()) != EOF) {
    switch (ch) {
    case '0': case '1':
      acc = (acc << 1) | (ch - '0');
      *lp++ = ch;
      if (lp - lexeme < 7)
        break;
      ch = acc;
      acc = 0;
      lp = lexeme;
      /* fall through */
    default:
      if (lp > lexeme)
        printf("%.*s", lp - lexeme, lexeme);
      putchar(ch);
    }
  }
  return 0;
}
From: Pascal Bourguignon
Subject: Re: Integer to Character conversion
Date: 
Message-ID: <874pqckt7t.fsf@thalassa.informatimago.com>
····@stablecross.com (Bob Felts) writes:

> Many months ago a co-worker and I were having a contest to see whose
> programming language was bigger^H^H^H^H^H^H better.  Someone had an
> e-mail sig of binary numbers, e.g.:
>    
>    01000001 01110101 01110100 00100000 01110110 ...
>
> The task was to convert this to ASCII to see what it said.  He chose C
> while I used Lisp.  My solution was:
>
> (defun dcb-to-ascii (dcb)
>  "Convert a list of decimal-coded-binary to ASCII"
>   (map 'string 'character
>        (mapcar #'(lambda (n) (parse-integer (write-to-string n) :radix
> 2)) dcb)))

or:

(with-input-from-string (inp " 01000001 01110101 01110100 00100000 01110110")
   (coerce
      (loop :for n = (let ((*read-base* 2)) (read inp nil nil)) :while n
            :collect (code-char n)) 
      'string))

--> "Aut v"

(assuming CODE-CHAR uses the ASCII map).

-- 
__Pascal Bourguignon__                     http://www.informatimago.com/

Nobody can fix the economy.  Nobody can be trusted with their finger
on the button.  Nobody's perfect.  VOTE FOR NOBODY.
From: ·····················@gmail.com
Subject: Re: Integer to Character conversion
Date: 
Message-ID: <1169997938.829447.9010@l53g2000cwa.googlegroups.com>
> Kent Pittman describes a way to do this conversion athttp://www.nhplace.com/kent/PS/EQUAL.html, but I'm lazy.  I don't want
> to have to type in an ASCII table.
>
Speaking of Kent Pittman, this is quite interesting: http://
www.nhplace.com/kent/PS/Ambitious.html . It talks about the use of an 
ambitious eval so we can (among other things) parse binary numbers as 
we intend without transforming them into strings first.

(INVOKE-RUBOUT-HANDLER #'EVAL-WHILE-READING)
(PROGN (SETQ *READ-BASE* 8.) 10)
=> 8.  ; not 10.
From: robert maas, see http://tinyurl.com/uh3t
Subject: Re: Integer to Character conversion
Date: 
Message-ID: <rem-2007jan29-007@yahoo.com>
> From: ·····················@gmail.com
> Speaking of Kent Pittman, this is quite interesting:
> http://www.nhplace.com/kent/PS/Ambitious.html
   ... From the user's point of view, it is as if
   the editing process backed up or was changed, but really it has been
   started over with fewer or different characters.
I don't like his approach there. IMO the *right* way to do this is
to implement screen refresh as a virtual object which can be
updated in log(n) time and traversed in linear time, such as a
balanced binary tree of some sort (AVL etc.), where the virtual
object is pointed at from a standard place the screen-refresh
mechanism knows about. Then you can perform all screen updates in
undoable fashion:
- Perform a log(n) time operation to rebuild the part of the tree
   that changes, sharing all side-branches that haven't changed.
- Overwrite the master pointer to point to that updated version
   instead of the just-previous version. The next screen update
   will see this new virtual screen instead of the previous, and
   the user will see the change appear within one video scan frame
   (1/30 sec).
Whenever you need to back up over an error, you simply overwrite
the master pointer to point to the previous state of the display.
No need to recompute the display from the very start of the line or
other major transaction group.

So to make this work with backspace and parser together, you write
your parser the same way. It uses a balanced binary tree to keep
the current parse state. As it works its way through the stream of
incoming data, at each point it rebuilds the changed part of the
parser-state tree, sharing side breanches that haven't changed. If
the parser controller gets an UNDO-CHAR event, it simply reverts to
the previous parse-state tree. If it gets an UNDO-LINE event, it
simply reverts to the checkpoint at the very start of that line. If
UNDO-LINE saves the undone state, then UNDO-UNDO could also be
implemented, like if the user accidently presses the UNDO-LINE
button on the keyboard (CTRL-U on some systems, which is right next
to CTRL-Y for yank text, CTRL-K on other systems, which is right
next to CTRL-L for refresh-screen-and-center-cursor).

Of course all side-effects must likewise be binary-tree undoable.
This may limit the kinds of read-time evaluation that are allowed.
If you keep your master program state also as a binary tree, then
undoing horrendous things like SETQ of a global would likewise be
possible. If you keep your entire filesystem in that form, even
more horrendous things like deleting files or whole directories may
be equally undoable. The original discussion was about a LISP
machine, so the idea of re-inventing the whole way filesystems are
implemented would be quite possilble in this context.

As to whether (SETQ *READ-BASE* 8.) should take effect immediately
upon being completely read (and undone the moment that last
character, the close-parens, is backspaced over, and redone if the
close-parens is typed again), thereby side-effecting the parse
in-progress, or whether parsing should run to completetion before
any of the parsed stuff is executed, should be a configuration
option settable by the user. Of course if the user says:
  (PROGN (SETQ $SIDE-EFFECT-IMMEDIATE$ T)
we get a sort of paradox however we implement it.

Good thing lisp isn't like some languages, where you can say
something like:
 DO {stuff; more stuff; even more stuff; yet even more stuff; } UNLESS mumble;
where you can't possibly know in the middle of parsing that input
whether it's an uncoditional block or conditional on something you
won't know until the end.

Hmm, what if you say (this is CMUCL if it makes any difference):
 (PROGN (QUIT)
and then how exactly do you get back into the system so you can
type rubout to avoid quitting the whole lisp environment?
From: Harald Hanche-Olsen
Subject: Re: Integer to Character conversion
Date: 
Message-ID: <pco8xfnikef.fsf@shuttle.math.ntnu.no>
+ ····@stablecross.com (Bob Felts):

| Many months ago a co-worker and I were having a contest to see whose
| programming language was bigger^H^H^H^H^H^H better.  Someone had an
| e-mail sig of binary numbers, e.g.:
|    
|    01000001 01110101 01110100 00100000 01110110 ...
|
| The task was to convert this to ASCII to see what it said.  He chose C
| while I used Lisp.  My solution was:
|
| [...]

Now that you got the code-char thing sorted out, how about this
solution:

cl-user>  (map 'string #'code-char (let ((*read-base* 2)) (read)))
(01000001 01110101 01110100 00100000 01110110)
"Aut v"

-- 
* Harald Hanche-Olsen     <URL:http://www.math.ntnu.no/~hanche/>
- It is undesirable to believe a proposition
  when there is no ground whatsoever for supposing it is true.
  -- Bertrand Russell
From: ············@googlemail.com
Subject: Re: Integer to Character conversion
Date: 
Message-ID: <1170000570.923388.87380@h3g2000cwc.googlegroups.com>
Harald Hanche-Olsen wrote:
> cl-user>  (map 'string #'code-char (let ((*read-base* 2)) (read)))
> (01000001 01110101 01110100 00100000 01110110)
> "Aut v"

A measure of Lisp's popularity is when we start seeing strings like 
this sent to webapps:

#.(error "~S" 'pwnd!)

I'm sure the same teams which ignore thousands of compiler warnings 
scrolling by will take care to use principled versions of read-from-
string...

Tayssir
From: ············@gmail.com
Subject: Re: Integer to Character conversion
Date: 
Message-ID: <1170021573.321465.276550@l53g2000cwa.googlegroups.com>
On Jan 28, 8:09 am, ············@googlemail.com wrote:
> Harald Hanche-Olsen wrote:
> > cl-user>  (map 'string #'code-char (let ((*read-base* 2)) (read)))
> > (01000001 01110101 01110100 00100000 01110110)
> > "Aut v"A measure of Lisp's popularity is when we start seeing strings like
> this sent to webapps:
>
> #.(error "~S" 'pwnd!)
>
> I'm sure the same teams which ignore thousands of compiler warnings
> scrolling by will take care to use principled versions of read-from-
> string...

That's a good point -- is there a version of READ that only accepts, 
say, certain "trusted" data types?

mfh
From: Pascal Bourguignon
Subject: Re: Integer to Character conversion
Date: 
Message-ID: <87veiqpxy2.fsf@thalassa.informatimago.com>
·············@gmail.com" <············@gmail.com> writes:

> On Jan 28, 8:09 am, ············@googlemail.com wrote:
>> Harald Hanche-Olsen wrote:
>> > cl-user>  (map 'string #'code-char (let ((*read-base* 2)) (read)))
>> > (01000001 01110101 01110100 00100000 01110110)
>> > "Aut v"A measure of Lisp's popularity is when we start seeing strings like
>> this sent to webapps:
>>
>> #.(error "~S" 'pwnd!)
>>
>> I'm sure the same teams which ignore thousands of compiler warnings
>> scrolling by will take care to use principled versions of read-from-
>> string...
>
> That's a good point -- is there a version of READ that only accepts, 
> say, certain "trusted" data types?

READ-CHAR, READ-LINE and READ-SEQUENCE do.
The parsing is up to you!

-- 
__Pascal Bourguignon__                     http://www.informatimago.com/
Until real software engineering is developed, the next best practice
is to develop with a dynamic system that has extreme late binding in
all aspects. The first system to really do this in an important way
is Lisp. -- Alan Kay
From: ············@googlemail.com
Subject: Re: Integer to Character conversion
Date: 
Message-ID: <1170088214.208236.5670@p10g2000cwp.googlegroups.com>
On Jan 28, 11:51 pm, Pascal Bourguignon <····@informatimago.com> 
wrote:
> ·············@gmail.com" <············@gmail.com> writes:
> > That's a good point -- is there a version of READ that only accepts,
> > say, certain "trusted" data types?
>
> READ-CHAR, READ-LINE and READ-SEQUENCE do.
> The parsing is up to you!

Yeah, I think if we're talking about stuff like getting data out of 
webapp forms, maybe the biggest use of READ is to conveniently parse 
numbers. But is READ's semantics what we necessarily want in this 
situation? For example, should 23.12 be a float or a rational?


Tayssir
From: John Thingstad
Subject: Re: Integer to Character conversion
Date: 
Message-ID: <op.tmv1cso1pqzri1@pandora.upc.no>
On Sun, 28 Jan 2007 22:59:33 +0100, ············@gmail.com  
<············@gmail.com> wrote:

> On Jan 28, 8:09 am, ············@googlemail.com wrote:
>> Harald Hanche-Olsen wrote:
>> > cl-user>  (map 'string #'code-char (let ((*read-base* 2)) (read)))
>> > (01000001 01110101 01110100 00100000 01110110)
>> > "Aut v"A measure of Lisp's popularity is when we start seeing strings  
>> like
>> this sent to webapps:
>>
>> #.(error "~S" 'pwnd!)
>>
>> I'm sure the same teams which ignore thousands of compiler warnings
>> scrolling by will take care to use principled versions of read-from-
>> string...
>
> That's a good point -- is there a version of READ that only accepts,
> say, certain "trusted" data types?
>
> mfh
>

setting *read-eval* to nil will flag the above as a error.
(let ((*read-eval* nil) (*read-base* 2)) (read))


-- 
Using Opera's revolutionary e-mail client: http://www.opera.com/mail/
From: Thomas A. Russ
Subject: Re: Integer to Character conversion
Date: 
Message-ID: <ymiy7nlbez9.fsf@sevak.isi.edu>
·············@gmail.com" <············@gmail.com> writes:

> 
> That's a good point -- is there a version of READ that only accepts, 
> say, certain "trusted" data types?

Are you thinking about something like:

  (let ((*read-eval* nil))
    ....)



-- 
Thomas A. Russ,  USC/Information Sciences Institute