python的ord(),chr()在等同于go? [英] Equivalent of python's ord(), chr() in 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屋!