英文:
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))
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论