如何打印*uint16指向的内容

//pacakge: syscall
func StringToUTF16Ptr(s string) *uint16

请问如何打印*uint16指向的内容,并还原出内容?

共 6 个回复


joe9i0

package main

import (
    "fmt"
    "syscall"
)

func main() {
    x := syscall.StringToUTF16Ptr("abcd")
    s := fmt.Sprintf("%v", *x)
    fmt.Println(s)
}

输出: 97

# 0

chenhao

我想问的是如何打印出整个"abcd",而不是首地址的内容

# 1

David

@chenhao,那你得转成 UTF-8 然后打印

# 2

chenhao

请问,有代码例子吗?

# 3

joe9i0

// StringToUTF16Ptr is deprecated. Use UTF16PtrFromString instead.

func main() {
    x, err := syscall.UTF16FromString("测试abcd")
    if nil != err {
        fmt.Println("Error.")
    }
    fmt.Println("%v", x)
    fmt.Println(syscall.UTF16ToString(x))
}
# 4

chenhao

那如何还原 syscall.BytePtrFromString(s string) (*byte, error)转化后的字符串呢?

# 5