#[no_mangle]
pub extern "C" fn decode(raw: *const libc::c_char) -> *mut u32 {
let str_raw = unsafe { CStr::from_ptr(raw) }.to_str().unwrap().to_string();
let bytes_raw = hex::decode(str_raw).unwrap();
let mut u8_fixed: [u32; 6] = Decode::decode(&mut &bytes_raw[..]).unwrap();
let ptr = u8_fixed.as_mut_ptr();
std::mem::forget(u8_fixed);
ptr
}
nodejs code
let libm = ffi.Library(rootPath, {
'decode': [ref.refType("uint"), ['string']],
}
let value = libm.decode("010000000200000003000000040000000500000006000000")
decode 的 value 返回值应该是 []uint{1,2,3,4,5,6}
,现在打印出 value 看起来像是个 buffer, 不知道怎么解析这个 buffer
1
nomagick 2022-08-07 22:01:01 +08:00
|
3
nomagick 2022-08-08 11:33:00 +08:00 1
@freehere 你这个函数返回的是 ref(array(uint)), 第一层是个指针,你拿到的 buffer 是指针的值,把它 deref(), 才是 array(uint)
|
4
freehere OP @nomagick 试了一下,只返回了 第一个 value, test 在这里 https://github.com/gmajor-encrypt/scale-codec-comparator/blob/main/polkadot-js/test/base.spec.ts#L167
|
5
k1263 2022-08-26 16:59:26 +08:00
Decode::decode 哪里来的?自己定义的吗?
|
6
freehere OP |
7
k1263 2022-09-07 14:01:41 +08:00
@freehere
我的这一步会出错,CStr::from_ptr(raw).to_str(), 你的出错吗? #[no_mangle] pub extern "C" fn decode(raw: *const libc::c_char) -> *mut u32 { if raw.is_null() { return ptr::null_mut(); } if let Ok(str_raw) = unsafe { CStr::from_ptr(raw).to_str() } { println!("str_raw: {:?}", str_raw); let bytes_raw = hex::decode(str_raw).unwrap(); let mut u8_fixed: [u32; 6] = Decode::decode(&mut &bytes_raw[..]).unwrap(); let ptr = u8_fixed.as_mut_ptr(); std::mem::forget(u8_fixed); ptr } else { println!("str_raw fail"); return ptr::null_mut(); } } pub fn main() { let raw = "010000000200000003000000040000000500000006000000"; let aa = decode(raw.as_ptr() as *const i8); println!("{:?}", aa); } |
8
zhangyuang 319 天前
|