Page 1 of 1
string/append and base64
Posted: Thu Oct 05, 2006 9:59 pm
by cormullion
Not sure what I'm doing, but I don't understand why these two give different results:
Code: Select all
(println (append "AUTH PLAIN " (base64-enc (append "\000" "user.name" "\000" "password"))))
(println (append "AUTH PLAIN " (base64-enc (string "\000" "user.name" "\000" "password"))))
AUTH PLAIN AHVzZXIubmFtZQBwYXNzd29yZA==
AUTH PLAIN dXNlci5uYW1lcGFzc3dvcmQ=
I'm adapting SMTP.lsp to do authorization, but why should string be different? Is it a Unicode thing?
Posted: Fri Oct 06, 2006 12:52 am
by Lutz
'append' works on binary (non displayable ASCII). 'string' does not, it will take a \000 as a string terminator, like 'print' and 'println' which are based on 'string':
Code: Select all
> (string "\000")
""
> (append "\000")
"\000"
>
Lutz
ps: see docs for 'append' and 'string'. Do we need a chapter/paragraph explaining this, perhaps in
http://newlisp.org/downloads/newlisp_ma ... l#type_ids ?
Posted: Fri Oct 06, 2006 7:54 am
by cormullion
Thanks, Lutz. I remember now. I was confusing myself partly by working in a file rather than directly in the terminal. I was trying to decode a string:
Code: Select all
(set 's (base64-dec "AHVzZXIubmFtZQBwYXNzd29yZA=="))
(println s)
- of course println returns the value correctly ("\000user.name\000password") but it doesn't display anything.
For some reason, the AUTH part of SMTP uses these zero terminators so I got myself well confused...
What's a good way of displaying the contents of strings like this in a log file?
Posted: Fri Oct 06, 2006 11:34 am
by Lutz
Use plain 'replace' without the regular expressions option:
Code: Select all
(replace "\000" (base64-dec "AHVzZXIubmFtZQBwYXNzd29yZA==") " ")
=> " user.name password"
to change the "\000" to a space or some other character.
Lutz
Posted: Fri Oct 06, 2006 1:49 pm
by cormullion
Thanks! In fact. I'll replace it with "\\000" just so that I know what's happening...