go byte 字節與 ascii 碼

一、ascii 碼介紹


ascii 碼總有有 128 位,用來表示常用的字符。
在 go 語言中我們用 byte 一個 uint8 (0-255) 來展示 ascii 字符。

二、go 表示 ascii 碼 0-9

1、表示 ascii 的 0-9


在 ascii 嗎中

utWEQ1

我們用 go 語言打印一下:

import "testing"

func TestAsciiInt(t *testing.T) {
    var asciiInt = []byte{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'}
    t.Log(asciiInt)
}

輸出:

[48 49 50 51 52 53 54 55 56 57]

2、byte 字符數組 轉字符串

import "testing"

func TestAsciiInt(t *testing.T) {
    var asciiInt = []byte{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'}
    t.Log(string(asciiInt))
}

輸出:

0123456789  //注意這是個字符串

二、一個字符對外的表現,是一個 unit8(byte) 或者 uin32 類型

我們打印一個字符 ascii 或者 Unicode

func TestAsciiInt3(t *testing.T) {
    t.Log('0')
    t.Log('你')
}

輸出:
48
20320
一個字符對外的表現,是一個 unit8(byte) 或者 uin32 類型。

三、一個字符 轉成 字符的 字符標識用 string()

func TestAsciiInt3(t *testing.T) {
    t.Log(string('0'))
    t.Log(string('你'))
}

輸出:

0

或者使用 unit8 也一樣

func TestAsciiInt4(t *testing.T) {
    t.Log(string(48))
    t.Log(string(20320))
}

輸出:

0
本文由 Readfog 進行 AMP 轉碼,版權歸原作者所有。
來源https://mp.weixin.qq.com/s/Kry6iQcIrkKTOuAzYteEYQ