将 UTF-8 字符串纯 LUS 转换成 ASCII

我有一个关于发送和接收带有特殊字符(德语 umlauts)的数据的问题。

当我使用下面的代码发送字符串“Café Zeezicht”时,服务器端的字符串是正确的。

但是我该如何接收和解码包含相同字符的接收数据?现在看起来像“Caf? Zeezicht”

我正在寻找一个纯 LUS 函数,因为我没有加载库的能力。

------------------------------------------------------------
-- 将 ASCII 转换为 UTF8 的函数
------------------------------------------------------------

-- 将 char 转换为 UTF-8 字符串
local function CodeToUTF8 (Unicode)
  if (Unicode == nil) then
    return ""
  end

  if (Unicode < 0x20) then return ' '; end;

    if (Unicode <= 0x7F) then return string.char(Unicode); end;

    if (Unicode <= 0x7FF) then
      local Byte0 = 0xC0 + math.floor(Unicode / 0x40);
      local Byte1 = 0x80 + (Unicode % 0x40);
      return string.char(Byte0, Byte1);
    end;

    if (Unicode <= 0xFFFF) then
      local Byte0 = 0xE0 +  math.floor(Unicode / 0x1000);
      local Byte1 = 0x80 + (math.floor(Unicode / 0x40) % 0x40);
      local Byte2 = 0x80 + (Unicode % 0x40);
      return string.char(Byte0, Byte1, Byte2);
    end;

    return "";    -- 暂时忽略 UTF-32
end;

-- 将 ASCII 字符串转换为 UTF-8 字符串
function AsciiToUTF8(str)
  result = ""
  for i = 1, #str do
    result = result .. CodeToUTF8(string.byte(str, i, i+1))
  end
  return result
end
------------------------------------------------------------
-- 结束将 ASCII 转换为 UTF8 的函数
------------------------------------------------------------
点赞