string/append and base64

Started by cormullion, October 05, 2006, 02:59:33 PM

Previous topic - Next topic

cormullion

Not sure what I'm doing, but I don't understand why these two give different results:


(println (append "AUTH PLAIN " (base64-enc (append "00" "user.name" "00" "password"))))
(println (append "AUTH PLAIN " (base64-enc (string "00" "user.name" "00" "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?

Lutz

#1
'append' works on binary (non displayable ASCII). 'string' does not, it will take a 00 as a string terminator, like 'print' and 'println' which are based on 'string':


> (string "00")
""
> (append "00")
"00"
>


Lutz



ps: see docs for 'append' and 'string'. Do we need a chapter/paragraph explaining this, perhaps in

http://newlisp.org/downloads/newlisp_manual.html#type_ids">http://newlisp.org/downloads/newlisp_ma ... l#type_ids">http://newlisp.org/downloads/newlisp_manual.html#type_ids ?

cormullion

#2
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:


(set 's (base64-dec "AHVzZXIubmFtZQBwYXNzd29yZA=="))
(println s)


- of course println returns the value correctly ("00user.name00password") 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?

Lutz

#3
Use plain 'replace' without the regular expressions option:


(replace "00" (base64-dec "AHVzZXIubmFtZQBwYXNzd29yZA==") " ")
=> " user.name password"


to change the "00" to a space or some other character.



Lutz

cormullion

#4
Thanks! In fact. I'll replace it with "\000" just so that I know what's happening...