编码转换 C#

static std::string cwstr_to_utf8(const std::wstring& in)
{
    std::string s(in.length() * 3 + 1, ' ');
    size_t len = ::WideCharToMultiByte(CP_UTF8, 0, in.c_str(), in.length(), &s[0], s.length(), NULL, NULL);
    s.resize(len);
    return s;
}

static std::wstring cutf8_to_wstr(const std::string& in)
{
    std::wstring s(in.length(), L' ');
    size_t len = ::MultiByteToWideChar(CP_UTF8, 0, in.c_str(), in.length(), &s[0], s.length());
    s.resize(len);
    return s;
}

 User user = new User();
            user.Name = "张撒";
            user.Desc = "你好";
            JsonSerializerSettings setting = new JsonSerializerSettings();
            setting.StringEscapeHandling = StringEscapeHandling.Default;
            string json = JsonConvert.SerializeObject(user, setting);
            byte[] unicodeBytes = System.Text.Encoding.UTF8.GetBytes(json);
            string jsonutf8 = Encoding.UTF8.GetString(unicodeBytes);

你可能感兴趣的:(算法,数据结构)