How to convert uintptr to []byte in golang?

huangapple go评论79阅读模式
英文:

How to convert uintptr to []byte in golang?

问题

我不知道如何做。
使用[]byte(uintptr(0))无法编译。

请给我一个示例。

英文:

I don't know how to do it.
It doesn't compile using []byte(uintptr(0)).

Please show me an example.

答案1

得分: 9

这取决于你的机器架构(32位或64位)以及你想要如何编码(小端或大端)。无论如何,encoding/binary 是你的答案:

var u uintptr = 42
size := unsafe.Sizeof(u)
b := make([]byte, size)
switch size {
case 4:
    binary.LittleEndian.PutUint32(b, uint32(u))
case 8:
    binary.LittleEndian.PutUint64(b, uint64(u))
default:
    panic(fmt.Sprintf("unknown uintptr size: %v", size))
}

Playground: http://play.golang.org/p/tIocqy-rAJ.

英文:

That depends on what's your machine's architecture (32- or 64-bit) and how do you want it to be encoded (little- or big-endian). Either way, encoding/binary is your answer:

var u uintptr = 42
size := unsafe.Sizeof(u)
b := make([]byte, size)
switch size {
case 4:
	binary.LittleEndian.PutUint32(b, uint32(u))
case 8:
	binary.LittleEndian.PutUint64(b, uint64(u))
default:
	panic(fmt.Sprintf("unknown uintptr size: %v", size))
}

Playground: http://play.golang.org/p/tIocqy-rAJ.

答案2

得分: 4

package main

import "fmt"
import "unsafe"

const sizeOfUintPtr = unsafe.Sizeof(uintptr(0))

func uintptrToBytes(u *uintptr) []byte {
    return (*[sizeOfUintPtr]byte)(unsafe.Pointer(u))[:]
}

func main() {

   var u = uintptr(1025)
   fmt.Println(uintptrToBytes(&u))
}
package main

import "fmt"
import "unsafe"

const sizeOfUintPtr = unsafe.Sizeof(uintptr(0))

func uintptrToBytes(u *uintptr) []byte {
    return (*[sizeOfUintPtr]byte)(unsafe.Pointer(u))[:]
}

func main() {

   var u = uintptr(1025)
   fmt.Println(uintptrToBytes(&u))
}
package main

import "fmt"
import "unsafe"

const sizeOfUintPtr = unsafe.Sizeof(uintptr(0))

func uintptrToBytes(u *uintptr) []byte {
    return (*[sizeOfUintPtr]byte)(unsafe.Pointer(u))[:]
}

func main() {

   var u = uintptr(1025)
   fmt.Println(uintptrToBytes(&u))
}
package main

import "fmt"
import "unsafe"

const sizeOfUintPtr = unsafe.Sizeof(uintptr(0))

func uintptrToBytes(u *uintptr) []byte {
    return (*[sizeOfUintPtr]byte)(unsafe.Pointer(u))[:]
}

func main() {

   var u = uintptr(1025)
   fmt.Println(uintptrToBytes(&u))
}
package main

import "fmt"
import "unsafe"

const sizeOfUintPtr = unsafe.Sizeof(uintptr(0))

func uintptrToBytes(u *uintptr) []byte {
    return (*[sizeOfUintPtr]byte)(unsafe.Pointer(u))[:]
}

func main() {

   var u = uintptr(1025)
   fmt.Println(uintptrToBytes(&u))
}
package main

import "fmt"
import "unsafe"

const sizeOfUintPtr = unsafe.Sizeof(uintptr(0))

func uintptrToBytes(u *uintptr) []byte {
    return (*[sizeOfUintPtr]byte)(unsafe.Pointer(u))[:]
}

func main() {

   var u = uintptr(1025)
   fmt.Println(uintptrToBytes(&u))
}
package main

import "fmt"
import "unsafe"

const sizeOfUintPtr = unsafe.Sizeof(uintptr(0))

func uintptrToBytes(u *uintptr) []byte {
    return (*[sizeOfUintPtr]byte)(unsafe.Pointer(u))[:]
}

func main() {

   var u = uintptr(1025)
   fmt.Println(uintptrToBytes(&u))
}
package main

import "fmt"
import "unsafe"

const sizeOfUintPtr = unsafe.Sizeof(uintptr(0))

func uintptrToBytes(u *uintptr) []byte {
    return (*[sizeOfUintPtr]byte)(unsafe.Pointer(u))[:]
}

func main() {

   var u = uintptr(1025)
   fmt.Println(uintptrToBytes(&u))
}
package main

import "fmt"
import "unsafe"

const sizeOfUintPtr = unsafe.Sizeof(uintptr(0))

func uintptrToBytes(u *uintptr) []byte {
    return (*[sizeOfUintPtr]byte)(unsafe.Pointer(u))[:]
}

func main() {

   var u = uintptr(1025)
   fmt.Println(uintptrToBytes(&u))
}
package main

import "fmt"
import "unsafe"

const sizeOfUintPtr = unsafe.Sizeof(uintptr(0))

func uintptrToBytes(u *uintptr) []byte {
    return (*[sizeOfUintPtr]byte)(unsafe.Pointer(u))[:]
}

func main() {

   var u = uintptr(1025)
   fmt.Println(uintptrToBytes(&u))
}
英文:
package main

import "fmt"
import "unsafe"

const sizeOfUintPtr = unsafe.Sizeof(uintptr(0))

func uintptrToBytes(u *uintptr) []byte {
    return (*[sizeOfUintPtr]byte)(unsafe.Pointer(u))[:]
}

func main() {

   var u = uintptr(1025)
   fmt.Println(uintptrToBytes(&u))
}

http://play.golang.org/p/tIocqy-rAJ

huangapple
  • 本文由 发表于 2015年8月26日 18:02:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/32223562.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定