lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On Mon, Oct 14, 2013 at 12:26 PM, Thijs Schreijer <thijs@thijsschreijer.nl> wrote:
> -----Original Message-----
> From: lua-l-bounces@lists.lua.org [mailto:lua-l-bounces@lists.lua.org] On
> Behalf Of Chris Datfung
> Sent: maandag 14 oktober 2013 10:57
> To: Lua mailing list
> Subject: iconv [was: HTML Encoding]
>
> After a bit more re-engineering, I removed the need to html encode the
> data. My question is now as follows:
>
> I have a variable thats latin-1 encoded, e.g. montr\xe9al . I want to
> convert that to montréal. I think I can use iconv, but using the iconv
> documentation, I'm not getting the expected output.
>
> iconv = require("iconv")
> cd = iconv.new('latin1', 'utf8')
> city = "montr\xe9al"
> nstr, err = cd:iconv(city)
> print(nstr)
>
> returns: montrxe9al
> How can I convert montr\xe9al to montréal?
> Thanks,
> Chris

I don't know iconv, but "\" is an escape character, try this;

iconv = require("iconv")
cd = iconv.new('latin1', 'utf8')
city = "montr\\xe9al"   -- an alternative would be; city = [[montr\xe9al]]
nstr, err = cd:iconv(city)
print(nstr)


Thanks, still not working though:

$ lua5.2
Lua 5.2.2  Copyright (C) 1994-2013 Lua.org, PUC-Rio
> iconv = require("iconv")
> cd = iconv.new('latin1', 'utf8')
> city = "montr\xe9al"
> nstr, err = cd:iconv(city)
> print(nstr)
montr
> city = "montr\\xe9al"
> nstr, err = cd:iconv(city)
> print(nstr)
montr\xe9al
> city = [[montr\xe9al]]
> nstr, err = cd:iconv(city)
> print(nstr)
montr\xe9al
> city = "montr\233al"
> nstr, err = cd:iconv(city)
> print(nstr)
montr
> city = "montr\\233al"
> nstr, err = cd:iconv(city)
> print(nstr)
montr\233al

- Chris