golang实现unicode转换为字符串string的方法

作者:dotcoo 时间:2024-05-09 09:30:44 

本文实例讲述了golang实现unicode转换为字符串string的方法。分享给大家供大家参考,具体如下:

package main
import (
    "bytes"
    "encoding/binary"
    "encoding/hex"
    "fmt"
    "strings"
)
func main() {
    str := `\u5bb6\u65cf`
    fmt.Println(u2s(str))
}
func u2s(form string) (to string, err error) {
    bs, err := hex.DecodeString(strings.Replace(form, `\u`, ``, -1))
    if err != nil {
        return
    }
    for i, bl, br, r := 0, len(bs), bytes.NewReader(bs), uint16(0); i < bl; i += 2 {
        binary.Read(br, binary.BigEndian, &r)
        to += string(r)
    }
    return
}

希望本文所述对大家Go语言程序设计有所帮助。

标签:golang,unicode,转换,string
0
投稿

猜你喜欢

  • Python使用正则表达式实现爬虫数据抽取

    2021-01-22 15:00:24
  • MySQL 8.0.12的安装与卸载教程详解

    2024-01-25 23:36:58
  • Python入门教程5. 字典基本操作【定义、运算、常用函数】 <font color=red>原创</font>

    2021-04-29 19:08:04
  • MySQL之where使用详解

    2024-01-16 11:11:08
  • sqlserver附加.mdf权限问题解决

    2024-01-17 15:46:20
  • phpMyAdmin安装配置方法

    2009-09-09 20:15:00
  • python __add__()的具体使用

    2022-11-29 07:49:22
  • python淘宝准点秒杀抢单的实现示例

    2021-08-20 17:50:23
  • Python快速从注释生成文档的方法

    2022-07-11 04:55:37
  • keras 自定义loss损失函数,sample在loss上的加权和metric详解

    2021-06-03 08:11:33
  • 详解python Todo清单实战

    2021-11-03 06:24:50
  • Python 二分查找之bisect库的使用详解

    2023-10-03 01:24:29
  • Python中list列表添加元素的3种方法总结

    2022-10-03 21:40:43
  • PHP设计模式之装饰器模式定义与用法详解

    2023-09-11 18:41:48
  • numpy中np.dstack()、np.hstack()、np.vstack()用法

    2021-08-27 11:47:42
  • 浅析python3中的os.path.dirname(__file__)的使用

    2021-11-10 04:35:23
  • Python常见字符串操作函数小结【split()、join()、strip()】

    2023-07-21 04:58:15
  • python遗传算法之单/多目标规划问题

    2021-09-09 20:27:24
  • Python学习之文件的创建与写入详解

    2021-03-07 09:27:06
  • JavaScript引入方式深入解读

    2024-04-25 13:12:29
  • asp之家 网络编程 m.aspxhome.com