Skip to content

Commit c46c184

Browse files
committed
Add strconv examples
1 parent 5ff97d9 commit c46c184

File tree

9 files changed

+220
-84
lines changed

9 files changed

+220
-84
lines changed

examples/basic/strconvs/README.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@
44

55
|file|example name|note|
66
|----|------------|----|
7-
|hex\_to\_decimal.go|strconvs\_hex\_to\_decimal|16進数文字列を10進数に変換するサンプルです.|
8-
|bin\_to\_decimal.go|strconvs\_bin\_to\_decimal|2進数文字列を10進数に変換するサンプルです.|
7+
|hex\_to\_dec.go|strconvs\_hex\_to\_dec|16進数文字列を10進数に変換するサンプルです.|
8+
|bin\_to\_dec.go|strconvs\_bin\_to\_dec|2進数文字列を10進数に変換するサンプルです.|
9+
|dec\_to\_dec.go|strconvs\_dec\_to\_dec|10進数文字列を10進数に変換するサンプルです.|
10+
|hex\_to\_bin.go|strconvs\_hex\_to\_bin|16進数文字列を2進数文字列に変換するサンプルです.|
11+
|bin\_to\_hex.go|strconvs\_bin\_to\_hex|2進数文字列を16進数文字列に変換するサンプルです.|
912
|parseint\_tips\_bitsize.go|strconvs\_parseint\_tips\_bitsize|strconv.ParseInt() の第3引数 bitSize を指定する際のTipsです。|
1013
|parseint\_tips\_basevalue.go|strconvs\_parseint\_tips\_base|strconv.ParseInt() の第2引数 base を指定する際のTipsです。|
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package strconvs
2+
3+
import (
4+
"strconv"
5+
6+
"github.com/devlights/gomy/output"
7+
)
8+
9+
// BinToDec -- 2進数文字列を10進数に変換するサンプルです.
10+
//
11+
// REFERENCES
12+
// - https://pkg.go.dev/strconv
13+
func BinToDec() error {
14+
var (
15+
values = []string{
16+
"11111111",
17+
"11011110101011011011111011101111",
18+
}
19+
)
20+
21+
for _, v := range values {
22+
var (
23+
parsed int64
24+
err error
25+
)
26+
27+
// ParseInt() の 第2引数 base に 0 以外の値を指定している場合
28+
// prefix 付きの文字列を指定するとエラーとなる.
29+
// (ex: 0b1111 はエラーとなる。 1111 はOK)
30+
parsed, err = strconv.ParseInt(v, 2, 64)
31+
if err != nil {
32+
return err
33+
}
34+
35+
output.Stdoutl("[original]", v)
36+
output.Stdoutl("[parsed ]", parsed)
37+
output.StdoutHr()
38+
}
39+
40+
return nil
41+
}

examples/basic/strconvs/bin_to_decimal.go

Lines changed: 0 additions & 34 deletions
This file was deleted.
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package strconvs
2+
3+
import (
4+
"strconv"
5+
6+
"github.com/devlights/gomy/output"
7+
)
8+
9+
// BinToHex -- 2進数文字列から16進数文字列へ変換するサンプルです。
10+
func BinToHex() error {
11+
//
12+
// 2進数から16進数へ変換する場合は以下の2段階で変換する.
13+
// 1) strconv.ParseInt() で int へ
14+
// 2) strconv.FormatInt() で string へ
15+
//
16+
17+
var (
18+
values = []string{
19+
"0b11111111",
20+
"0b11011110101011011011111011101111",
21+
}
22+
)
23+
24+
for _, v := range values {
25+
var (
26+
parsed int64
27+
err error
28+
)
29+
30+
parsed, err = strconv.ParseInt(v, 0, 0)
31+
if err != nil {
32+
return err
33+
}
34+
35+
var (
36+
converted = strconv.FormatInt(parsed, 16)
37+
)
38+
39+
output.Stdoutl("[original]", v)
40+
output.Stdoutl("[parsed ]", parsed)
41+
output.Stdoutl("[conveted]", converted)
42+
output.StdoutHr()
43+
}
44+
45+
return nil
46+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package strconvs
2+
3+
import (
4+
"strconv"
5+
6+
"github.com/devlights/gomy/output"
7+
)
8+
9+
// DecToDec -- 10進数文字列を10進数に変換するサンプルです.
10+
//
11+
// REFERENCES
12+
// - https://pkg.go.dev/strconv
13+
func DecToDec() error {
14+
var (
15+
values = []string{
16+
"255",
17+
"3735928559",
18+
}
19+
)
20+
21+
var (
22+
parseInt = func(s string) int64 {
23+
v, _ := strconv.ParseInt(s, 10, 64)
24+
return v
25+
}
26+
atoi = func(s string) int64 {
27+
// strconv.Atoi() は strconv.ParseInt(v, 10, 0) と同じ
28+
v, _ := strconv.Atoi(s)
29+
return int64(v)
30+
}
31+
)
32+
33+
for _, v := range values {
34+
output.Stdoutl("[original]", v)
35+
output.Stdoutl("[parseInt]", parseInt(v))
36+
output.Stdoutl("[atoi ]", atoi(v))
37+
output.StderrHr()
38+
}
39+
return nil
40+
}

examples/basic/strconvs/examples.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,11 @@ func NewRegister() mapping.Register {
1313

1414
// Regist -- 登録します.
1515
func (r *register) Regist(m mapping.ExampleMapping) {
16-
m["strconvs_hex_to_decimal"] = HexToDecimal
17-
m["strconvs_bin_to_decimal"] = BinToDecimal
16+
m["strconvs_hex_to_dec"] = HexToDec
17+
m["strconvs_bin_to_dec"] = BinToDec
18+
m["strconvs_hex_to_bin"] = HexToBin
19+
m["strconvs_bin_to_hex"] = BinToHex
20+
m["strconvs_dec_to_dec"] = DecToDec
1821
m["strconvs_parseint_tips_bitsize"] = ParseIntTipsBitSize
1922
m["strconvs_parseint_tips_base"] = ParseIntTipsBaseValue
2023
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package strconvs
2+
3+
import (
4+
"strconv"
5+
6+
"github.com/devlights/gomy/output"
7+
)
8+
9+
// HexToBin -- 16進数から2進数文字列へ変換するサンプルです。
10+
func HexToBin() error {
11+
//
12+
// 16進数から2進数へ変換する場合は以下の2段階で変換する.
13+
// 1) strconv.ParseInt() で int へ
14+
// 2) strconv.FormatInt() で string へ
15+
//
16+
17+
var (
18+
values = []string{
19+
"0xff",
20+
"0xDEADBEEF",
21+
}
22+
)
23+
24+
for _, v := range values {
25+
var (
26+
parsed int64
27+
err error
28+
)
29+
30+
parsed, err = strconv.ParseInt(v, 0, 0)
31+
if err != nil {
32+
return err
33+
}
34+
35+
var (
36+
converted = strconv.FormatInt(parsed, 2)
37+
)
38+
39+
output.Stdoutl("[original]", v)
40+
output.Stdoutl("[parsed ]", parsed)
41+
output.Stdoutl("[conveted]", converted)
42+
output.StdoutHr()
43+
}
44+
45+
return nil
46+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package strconvs
2+
3+
import (
4+
"strconv"
5+
6+
"github.com/devlights/gomy/output"
7+
)
8+
9+
// HexToDec -- 16進数文字列を10進数に変換するサンプルです.
10+
func HexToDec() error {
11+
var (
12+
values = []string{
13+
"ff",
14+
"deadbeef",
15+
}
16+
)
17+
18+
for _, v := range values {
19+
var (
20+
parsed int64
21+
err error
22+
)
23+
24+
// ParseInt() の 第2引数 base に 0 以外の値を指定している場合
25+
// prefix 付きの文字列を指定するとエラーとなる.
26+
// (ex: 0xff はエラーとなる。 ff はOK)
27+
parsed, err = strconv.ParseInt(v, 16, 64)
28+
if err != nil {
29+
return err
30+
}
31+
32+
output.Stdoutl("[original]", v)
33+
output.Stdoutl("[parsed ]", parsed)
34+
output.StdoutHr()
35+
}
36+
return nil
37+
}

examples/basic/strconvs/hex_to_decimal.go

Lines changed: 0 additions & 46 deletions
This file was deleted.

0 commit comments

Comments
 (0)