高朗input := []uint{1,2,3,4,5,6}o := C.fixU32_encode((*C.uint)(unsafe.Pointer(&input[0])), C.size_t(len(input)))return C.GoString(o)Cchar* fixU32_encode(unsigned int* ptr,size_t length);锈pub extern "C" fn fixU32_encode(ptr: *const u32, length: libc::size_t) -> *const libc::c_char { assert!(!ptr.is_null()); let slice = unsafe { std::slice::from_raw_parts(ptr, length as usize) }; println!("{:?}", slice);// there will print [1,0,2,0,3,0] println!("{:?}", length); let mut arr = [0u32; 6]; for (&x, p) in slice.iter().zip(arr.iter_mut()) { *p = x; } CString::new(hex::encode(arr.encode())).unwrap().into_raw()}这样会传入,但是rust接收到的数组是这样的 [1,0,2,0,3,0]
1 回答

千巷猫影
TA贡献1829条经验 获得超7个赞
在 Go 中,uint 是 64 位的(参见https://golangbyexample.com/go-size-range-int-uint/)。因此,您将 64 位整数存储在input
.
C 代码和 Rust 代码处理input
现在的 32 位无符号整数(小端格式)。所以 64bit 中 0x1 的第一个输入:
00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000001
分别变为 0x1 和 0x0。由于小字节序,首先读取最低有效位。
您希望在 Go 中具体使用 32 位使用uint32
或确保您的 C 代码与 Go 中的机器相关整数类型匹配。
- 1 回答
- 0 关注
- 117 浏览
添加回答
举报
0/150
提交
取消