convertutil

package
v0.0.0-...-bb65a4e Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 23, 2026 License: MIT Imports: 9 Imported by: 9

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CBytesToStr

func CBytesToStr(b []byte) string

CBytesToStr C字节数组转换为字符串

Examples:

CBytesToStr([]byte("hello\x00")) // hello

func CPtrToStr

func CPtrToStr(p *byte) string

CPtrToStr C指针转换为字符串

func CopyProperties

func CopyProperties(src, dst any) error

CopyProperties 复制结构体的属性,注意:dst必须是指针类型,且指向的结构体类型与src类型相同

Params:

src: 源对象
dst: 目标对象

Examples:

type src struct {
	Name string
	Map  map[string]int
}
type dst struct {
	Name string
	Map  map[string]int
	Age  int
}
s1 := src{Name: "test", Map: map[string]int{"a": 1}}
s2 := new(dst)
CopyProperties(s1, s2)

func DigitToChinese

func DigitToChinese(s string) string

DigitToChinese 数字逐位转汉字

Params

s: 待转换的数字,例如:138000

Examples:

DigitToChinese("138000") // 返回 一三八零零零

func Float64ToStr

func Float64ToStr(num float64, prec ...int) string

Float64ToStr float64转换为字符串

Params

num: 待转化的数字
prec: 精度, 及就是小数点后的位数, 默认为-1, 即不限制

func HexToInt64

func HexToInt64(str string) int64

HexToInt64 十六进制字符串转换为int64

func Int64ToHex

func Int64ToHex(num int64, args ...string) string

Int64ToHex int64转换为十六进制字符串

Params

num: int64
args: 可选参数, 用于指定填充的位数以及填充的字

Examples:

Int64ToHex(15) // 返回 F
Int64ToHex(15, "08") // 返回 0000000F

func Int64ToStr

func Int64ToStr(num int64) string

Int64ToStr int64转换为字符串

func IntToHex

func IntToHex[T gotool.Integer](v T, width ...int) string

IntToHex 整数转换为十六进制字符串

Params

v: 待转换的整数
width: 可选参数, 用于指定输出的长度, 默认为其字节数*2

Examples:

IntToHex(uint8(15)) // 输出 0F
IntToHex(uint16(255)) // 输出 00FF
IntToHex(int8(-15)) // 输出 F1

func IntegerToChinese

func IntegerToChinese(num int) string

IntegerToChinese 整数转中文读法

Params

num: 待转换的整数

Examples:

IntegerToChinese(138000) // 输出 十三万八千
IntegerToChinese(-10001)   // 输出 负一万零一

func StrToCBytes

func StrToCBytes(s string) []byte

StrToCBytes 字符串转换为C字节数组

Examples:

StrToCBytes("hello") // 返回 []byte{0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x0}

func StrToCPtr

func StrToCPtr(s string) *byte

StrToCPtr 字符串转换为C指针

func StrToFloat32

func StrToFloat32(s string) float32

StrToFloat32 字符串转换为float32

func StrToFloat64

func StrToFloat64(s string) float64

StrToFloat64 字符串转换为float64

func StrToInt

func StrToInt(s string) int

StrToInt 字符串转换为int

func StrToInt8

func StrToInt8(s string) int8

StrToInt8 字符串转换为int8

func StrToInt16

func StrToInt16(s string) int16

StrToInt16 字符串转换为int16

func StrToInt32

func StrToInt32(s string) int32

StrToInt32 字符串转换为int32

func StrToInt64

func StrToInt64(s string) int64

StrToInt64 字符串转换为int64

func StrToUint

func StrToUint(s string) uint

StrToUint 字符串转换为uint

func StrToUint8

func StrToUint8(s string) uint8

StrToUint8 字符串转换为uint8

func StrToUint16

func StrToUint16(s string) uint16

StrToUint16 字符串转换为uint16

func StrToUint32

func StrToUint32(s string) uint32

StrToUint32 字符串转换为uint32

func StrToUint64

func StrToUint64(s string) uint64

StrToUint64 字符串转换为uint64

func TextToChinese

func TextToChinese(text string) string

TextToChinese 中文文本口语化转换

Params:

text: 待处理的文本

Examples:

TextToChinese("12:30") // 十二点三十分
TextToChinese("50%") // 百分之五十
TextToChinese("3.14") // 三点一四

func ToStr

func ToStr(value any) string

ToStr 常用类型转换为字符串,适用于以下类型: int, int64, int32, int16, int8, uint, uint64, uint32, uint16, uint8, float64, float32, bool, []byte, error, fmt.Stringer, ptr, map, slice, struct

Note:

结构体、Map、Slice 等类型,是返回 json 序列化后的字符串

Params

value: 待转换的值

Examples:

ToStr(123) // 123
ToStr("123") // 123
ToStr(123.456) // 123.456
ToStr([]int{1, 2, 3}) // [1,2,3]

func Uint64ToStr

func Uint64ToStr(num uint64) string

Uint64ToStr uint64转换为字符串

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL