gpt4 book ai didi

go - 将 uint16 数组转换为字符串

转载 作者:数据小太阳 更新时间:2023-10-29 03:22:23 25 4
gpt4 key购买 nike

我有一个来自 WinAPI PROCESSENTRY32.szExeFile 的 uint16 数组,我想将其转换为字符串。

这是我的变量类型

var hello [260]uint16

现在我需要将 hello 转换为字符串。我该怎么做?

编辑

这是我尝试过的:

func szExeFileToString(ByteString [260]uint16) string {
b := make([]byte, len(ByteString))

for i, v := range ByteString {
b[i] = byte(v)
}

return string(b)
}

但是,这会返回非常奇怪的字符串... result (该函数应将 PROCESSENTRY32.szExeFile (-> [260]uint16) 类型中的 Windows 进程名称转换为字符串)

最佳答案

package windows

import "golang.org/x/sys/windows"

func UTF16ToString

func UTF16ToString(s []uint16) string

UTF16ToString returns the UTF-8 encoding of the UTF-16 sequence s, with a terminating NUL removed.


使用 windows.UTF16ToString。例如,

package main

import (
"fmt"
"golang.org/x/sys/windows"
)

func main() {
var szExeFile [260]uint16
szExeFile = [260]uint16{'e', 'x', 'e', 'F', 'i', 'l', 'e'}

exeFile := windows.UTF16ToString(szExeFile[:])
fmt.Println(exeFile)
}

输出:

exeFile

关于go - 将 uint16 数组转换为字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51575163/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com