Reputation: 37
I have an array of uint16 coming from WinAPI PROCESSENTRY32.szExeFile
that I wanna convert to a string.
Here's my var type
var hello [260]uint16
now I need to convert hello to a string. How can I do that?
Edit
Here's what I've tried:
func szExeFileToString(ByteString [260]uint16) string {
b := make([]byte, len(ByteString))
for i, v := range ByteString {
b[i] = byte(v)
}
return string(b)
}
However, this returns pretty weird strings...
result (the function should convert Windows process names in the PROCESSENTRY32.szExeFile
(-> [260]uint16
) type to string)
Upvotes: 1
Views: 1855
Reputation: 37
Fixed it. Here's the fixed function to help anyone that gets into this question to convert PROCESSENTRY32.szExeFile
result to a string.
Note: I've also forgot to use kernel32.NewProc("Process32FirstW")
and kernel32.NewProc("Process32NextW")
instead of kernel32.NewProc("Process32First")
func szExeFileToString(ByteString [260]uint16) string {
var End = 0
for i, _ := range ByteString {
if ByteString[i] == 0 {
End = i
break
}
}
return syscall.UTF16ToString(ByteString[:End])
}
Upvotes: -1
Reputation: 166569
import "golang.org/x/sys/windows"
func UTF16ToString(s []uint16) string
UTF16ToString returns the UTF-8 encoding of the UTF-16 sequence s, with a terminating NUL removed.
Use windows.UTF16ToString
. For example,
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)
}
Output:
exeFile
Upvotes: 3