Printed representation of (char 0) ?

2024-02-12 Thread Thorsten Jolitz
Hello List,
it's been some time .. ;-)

I'm playing around a bit with hex<->ascii conversion in PicoLisp, and I
have the problem that (char 0) = NIL

 (hex "00")
-> 0
: (char (hex "00"))
-> NIL

 and disappears from the resulting ascii string.

: (setq X
"23230200010001000F00323032342D30322D303931302D35342D303520202424")
: (setq Y (chop X))
: (pack (make (while Y (link (char (hex (pack (cut 2 'Y)]
-> "##^B^A^A^O2024-02-0910-54-05  $$"

So how would it be possible to convert the ascii string back to the
original hex string, when all the "00" are gone in the ascii
representation? Shouldn't the (char 0) representation print to something
like ^N or so too, like (char 1), (char 2) etc?

When I try this online converter, the "00" are maintained as blanks:
Hex to ASCII Text String Converter (rapidtables.com)

[image: image.png]

and here is another representation that seems to enable the hex -> ascii ->
hex conversion, getting exactly the same hex string in the end as in the
beginning (not exactly the same conversion as above! just an example how it
might look) :
[image: image.png]
Cheers
Thorsten


Re: Printed representation of (char 0) ?

2024-02-12 Thread Thorsten Jolitz
Ah sorry, the moment I sent the question, I figured out the answer :
this is just a side effect of the final (pack ..).

When using (str ...) I get what I want:
:  (str (make (while Y (link (char (hex (pack (cut 2 'Y)]
-> "\"#\" \"#\" \"\^B\" NIL NIL NIL \"\^A\" NIL \"\^A\" NIL \"\^O\" NIL NIL
NIL NIL NIL NIL NIL \"2\" \"0\" \"2\" \"4\" \"-\" \"0\" \"2\" \"-\" \"0\"
\"9\" \"1\" \"0\" \"-\" \"5\" \"4\" \"-\" \"0\" \"5\" \" \" \" \" NIL NIL
NIL NIL NIL NIL NIL NIL \"$\" \"$\""

although somehow I still think it would be fine to have 'pack that prints
the NIL too, in this special case:
: (pack (make (while Y (link (char (hex (pack (cut 2 'Y)]
-> "##^B^A^A^O2024-02-0910-54-05  $$"

Am Mo., 12. Feb. 2024 um 23:25 Uhr schrieb Thorsten Jolitz <
tjol...@gmail.com>:

> Hello List,
> it's been some time .. ;-)
>
> I'm playing around a bit with hex<->ascii conversion in PicoLisp, and I
> have the problem that (char 0) = NIL
>
>  (hex "00")
> -> 0
> : (char (hex "00"))
> -> NIL
>
>  and disappears from the resulting ascii string.
>
> : (setq X
> "23230200010001000F00323032342D30322D303931302D35342D303520202424")
> : (setq Y (chop X))
> : (pack (make (while Y (link (char (hex (pack (cut 2 'Y)]
> -> "##^B^A^A^O2024-02-0910-54-05  $$"
>
> So how would it be possible to convert the ascii string back to the
> original hex string, when all the "00" are gone in the ascii
> representation? Shouldn't the (char 0) representation print to something
> like ^N or so too, like (char 1), (char 2) etc?
>
> When I try this online converter, the "00" are maintained as blanks:
> Hex to ASCII Text String Converter (rapidtables.com)
> 
> [image: image.png]
>
> and here is another representation that seems to enable the hex -> ascii
> -> hex conversion, getting exactly the same hex string in the end as in the
> beginning (not exactly the same conversion as above! just an example how it
> might look) :
> [image: image.png]
> Cheers
> Thorsten
>


Re: Printed representation of (char 0) ?

2024-02-12 Thread Tomas Hlavaty
On Mon 12 Feb 2024 at 23:25, Thorsten Jolitz  wrote:
> Shouldn't the (char 0) representation print to something
> like ^N or so too, like (char 1), (char 2) etc?

^@

what are you trying to achieve?

why not use base64, for example?

-- 
UNSUBSCRIBE: mailto:picolisp@software-lab.de?subject=Unsubscribe


Re: Printed representation of (char 0) ?

2024-02-12 Thread Thorsten Jolitz
I would like to achieve a roundtrip like this:
: (hex (char (char (hex "23"]
-> "23"

but not like this:
: (hex (char (char (hex "01"]
-> "1"
: (hex (char (char (hex "00"]
-> "0"

It should be possible to do this, so that W = X in the end:
: (setq X
"23230200010001000F00323032342D30322D303931302D35342D303520202424")
: (length X)
-> 96
: (setq Y (chop X))
: (length Y)
-> 96
: (setq W (pack (mapcar '((A) (hex (char A))) (make (while Y (link (char
(hex (pack (cut 2 'Y)]
->
"232320001010F000323032342D30322D303931302D35342D303520202424"
: (length W)
-> 72

Am Mo., 12. Feb. 2024 um 23:56 Uhr schrieb Tomas Hlavaty <
picolisp@software-lab.de>:

> On Mon 12 Feb 2024 at 23:25, Thorsten Jolitz 
> wrote:
> > Shouldn't the (char 0) representation print to something
> > like ^N or so too, like (char 1), (char 2) etc?
>
> ^@
>
> what are you trying to achieve?
>
> why not use base64, for example?
>
> --
> UNSUBSCRIBE: mailto:picolisp@software-lab.de?subject=Unsubscribe
>


Re: Printed representation of (char 0) ?

2024-02-12 Thread Tomas Hlavaty
On Tue 13 Feb 2024 at 00:25, Thorsten Jolitz  wrote:
> I would like to achieve a roundtrip like this:

why?

NUL is often a string sentinel value
so trying to use it as a character value
will lead to issues
do not do that

-- 
UNSUBSCRIBE: mailto:picolisp@software-lab.de?subject=Unsubscribe


Re: Printed representation of (char 0) ?

2024-02-12 Thread Alexander Burger
Hi Thorsten,

> it's been some time .. ;-)

Welcome back! :)


> I'm playing around a bit with hex<->ascii conversion in PicoLisp, and I
> have the problem that (char 0) = NIL
> 
>  (hex "00")
> -> 0
> : (char (hex "00"))
> -> NIL

This is correct.

'char' converts a number to a (transient) symbol here.

A symbol's name is a string, a null-terminated sequence of UTF-8 characters. In
case of 'char', this string has a single character and a terminating null byte.
This is the same as in other languages like C.

So the number 65 gives a symbol "A":

   : (char 65)
   -> "A"

But what happens with 0?

It gives an empty string, i.e. a null-byte

   : (char 0)
   -> NIL

and an empty string in PicoLisp is NIL.

   : ""
   -> NIL

☺/ A!ex

-- 
UNSUBSCRIBE: mailto:picolisp@software-lab.de?subject=Unsubscribe


Re: Printed representation of (char 0) ?

2024-02-12 Thread Thorsten Jolitz
Ok I understand that.
But when I have a hexadecimal message string with fixed length, and the
positions inside the string carry semantics? A certain value in a certain
position has a meaning?

Say fixed length is 96 like in the example above, and 2323 ist two start
chars, and then 01 or 02 is a handshake.

And this message has to be translated to ASCII to be understood by a third
party, that sends ASCII answers back, that has to be translated to hex.

This does not work when a round-trip conversion of 232301 results in 23231.

Tomas Hlavaty  schrieb am Di., 13. Feb. 2024,
07:49:

> On Tue 13 Feb 2024 at 00:25, Thorsten Jolitz 
> wrote:
> > I would like to achieve a roundtrip like this:
>
> why?
>
> NUL is often a string sentinel value
> so trying to use it as a character value
> will lead to issues
> do not do that
>
> --
> UNSUBSCRIBE: mailto:picolisp@software-lab.de?subject=Unsubscribe
>


Re: Printed representation of (char 0) ?

2024-02-12 Thread Thorsten Jolitz
Hi Alex,
But shouldn't hex 23232424 print to something like ##^N^N$$ instead of
##$$ ?
So the printed ASCII string (as char) carries all the information from the
hex string, and can be converted back to the exact same hex string?
At least in some special cases when it's needed?

Alexander Burger  schrieb am Di., 13. Feb. 2024,
07:56:

> Hi Thorsten,
>
> > it's been some time .. ;-)
>
> Welcome back! :)
>
>
> > I'm playing around a bit with hex<->ascii conversion in PicoLisp, and I
> > have the problem that (char 0) = NIL
> >
> >  (hex "00")
> > -> 0
> > : (char (hex "00"))
> > -> NIL
>
> This is correct.
>
> 'char' converts a number to a (transient) symbol here.
>
> A symbol's name is a string, a null-terminated sequence of UTF-8
> characters. In
> case of 'char', this string has a single character and a terminating null
> byte.
> This is the same as in other languages like C.
>
> So the number 65 gives a symbol "A":
>
>: (char 65)
>-> "A"
>
> But what happens with 0?
>
> It gives an empty string, i.e. a null-byte
>
>: (char 0)
>-> NIL
>
> and an empty string in PicoLisp is NIL.
>
>: ""
>-> NIL
>
> ☺/ A!ex
>
> --
> UNSUBSCRIBE: mailto:picolisp@software-lab.de?subject=Unsubscribe
>


Re: Printed representation of (char 0) ?

2024-02-12 Thread Tomas Hlavaty
On Tue 13 Feb 2024 at 08:28, Thorsten Jolitz  wrote:
> But when I have a hexadecimal message string with fixed length, and the
> positions inside the string carry semantics? A certain value in a certain
> position has a meaning?

it is not a string, at least not in picolisp/C/unix sense

you need to properly parse the binary data into your own representation
and also write your own code to translate your own representation to
that binary data

-- 
UNSUBSCRIBE: mailto:picolisp@software-lab.de?subject=Unsubscribe