LoginSignup
4
2

Go言語におけるAtoiとItoa

Posted at

AtoiやItoaに使われるAはASCIIのこと

AtoiとItoaは文字列と整数間の変換を行う際の関数です。
Atoiは「ASCII to Integer」の略になります。ASCII(アスキー)以外の文字列にも対応しています。
strconvのパッケージに属しています。

Atoi (ASCII to Integer):文字列→整数

このコードでは、文字列 "123" を整数 123 に変換しています。変換に失敗した場合、エラーが返されます。

package main

import (
    "fmt"
    "strconv"
)

func main() {
    str := "123"
    num, err := strconv.Atoi(str)
    fmt.Println("変換結果:", num)
}

Itoa (Integer to ASCII):整数→文字列

package main

import (
    "fmt"
    "strconv"
)

func main() {
    num := 123
    str := strconv.Itoa(num)
    fmt.Println("変換結果:", str)
}
4
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
2