python的ord(),chr()在等同于go? [英] Equivalent of python's ord(), chr() in go?

查看:1092
本文介绍了python的ord(),chr()在等同于go?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

python的 chr() golang中的ord()函数?

  chr(97)='a'
ord('a')= 97


解决方案

它们支持简单的转换

  ch:= rune(97)
n:= int('a ')
fmt.Printf(char:%c \ n,ch)
fmt.Printf(code:%d\\\
,n)

输出(尝试在 Go Playground ):

  char:a 
code:97
code>

注意:您也可以将整数数值val ue转换为字符串 ,它基本上将整数值解释为UTF-8编码值:

 s:= string(97)
fmt.Printf(text:%s\,s)//输出:text:a

$ b


将带符号或无符号整数值转换为字符串类型会生成一个包含UTF-8表示的字符串整数。超出有效Unicode代码点范围的值将转换为\\\�



What is the equivalent of python's chr() and ord() functions in golang?

chr(97) = 'a'
ord('a') = 97

解决方案

They are supported as simple conversions:

ch := rune(97)
n := int('a')
fmt.Printf("char: %c\n", ch)
fmt.Printf("code: %d\n", n)

Output (try it on the Go Playground):

char: a
code: 97

Note: you can also convert an integer numeric value to a string which basically interprets the integer value as the UTF-8 encoded value:

s := string(97)
fmt.Printf("text: %s\n", s) // Output: text: a

Converting a signed or unsigned integer value to a string type yields a string containing the UTF-8 representation of the integer. Values outside the range of valid Unicode code points are converted to "\uFFFD".

这篇关于python的ord(),chr()在等同于go?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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