-- Convert input string from IBM-1047 to ISO8859-1 encoding local iconv = require("iconv") -- Converts string to hex string -- Source: luacode.org, Author: agw, License: MIT/X11 function string.c2x(s, spacer) return ( string.gsub(s,"(.)", function (c) return string.format("%02X%s", s.byte(c), spacer or "") end) ) end -- Inserts spacer after each character in a string function string.letterspace(s, spacer) return ( string.gsub(s,"(.)", function (c) return string.format("%s%s", c, spacer) end) ) end -- Codesets -- Assumes input string is IBM-1047 (not necessarily true!) local from = "IBM-1047" local to = "ISO8859-1" -- Get string to convert from command line local before = ... -- If no input string supplied, display demo if before == nil then print("Converts encoding of input string from " .. from .. " to " .. to) print("For example:") before = "(ABC) [abc] {123}" end -- Create conversion descriptor local converter = iconv.new(to, from) -- Convert string local after = converter:iconv(before) -- Print before and after print(before:letterspace(" ")) print(before:c2x(" ") .. from) print(after:c2x(" ") .. to) -- Demo output: -- Converts encoding of input string from IBM-1047 to ISO8859-1 -- For example: -- ( A B C ) [ a b c ] { 1 2 3 } -- 4D C1 C2 C3 5D 40 AD 81 82 83 BD 40 C0 F1 F2 F3 D0 IBM-1047 -- 28 41 42 43 29 20 5B 61 62 63 5D 20 7B 31 32 33 7D ISO8859-1
