将转义的代码点转换为Unicode字符 [英] Convert escaped codepoint to unicode character

查看:138
本文介绍了将转义的代码点转换为Unicode字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取一个包含字符串的JSON块,这些字符串包含文字字符\u009e,我想将这些字符转换为其关联的单个unicode字符,在这种情况下为é.

I am trying to take a chunk of JSON that has strings which contain the literal characters \u009e and I would like to convert those characters to its associated single unicode character, in this case é.

我使用curl或wget下载看起来像的json:

I use curl or wget to download the json which looks like:

{ "name": "Kitsun\u00e9" }

并且需要在Vim中将其翻译为:

And need to translate this in Vim to:

{ "name": "Kitsuné" }

我的第一个想法是使用Vim的iconv,但它不会将字符串评估为单个字符,而只是返回输入.

My first thought was to use Vim's iconv, but it does not evaluate the string as a single character and just returns the input.

let code = '\u00e9'
echo iconv(code, "UTF-8", "UTF-8")
" Prints \u00e9

我最终希望使用类似的东西

I want to eventually use something like

%s;\\u[0-9abcdef]*;\=iconv(submatch(0),"UTF-8", "UTF-8");g

推荐答案

此行适用于您的示例:

s#\\u[0-9a-f]*#\=eval('"'.submatch(0).'"')#

s#\v\\u([0-9a-f]{4})#\=nr2char(str2nr(submatch(1),16))#

这篇关于将转义的代码点转换为Unicode字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆