lua-users home
lua-l archive

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


On Sat, 27 Feb 2010, JM Fernandez wrote:
>
> Is there some way to detect from Lua the charset used by the OS to
> proceed adequately? (probably in Linux, Windows and Mac the way,
> if exists, is different).

On Unix the terminal's charset is determined by the locale environment
variables, i.e. LANG if it is set, otherwise LC_CTYPE. The values of these
environment variables are not specified by POSIX, but you can usually look
for the substring UTF-8 to find out if you should use UTF-8.

> Apart from this, is there a simple way to convert from iso-8859-1 to utf-8
> (without iconv)

	function char_8859_1_to_utf_8(char)
		local lobits = char % 64
		local hibits = (char - lobits) / 64
		local octet1 = 0xc0 + hibits
		local octet2 = 0x80 + lobits
		return string.char(octet1,octet2)
	end

	function string_8859_1_to_utf_8(s)
		local u
		for i = 1,#s do
			u = u .. char_8859_1_to_utf_8(s,i)
		end
		return u
	end

Tony.
-- 
f.anthony.n.finch  <dot@dotat.at>  http://dotat.at/
GERMAN BIGHT HUMBER: SOUTHWEST 5 TO 7. MODERATE OR ROUGH. SQUALLY SHOWERS.
MODERATE OR GOOD.