vlang-转换int,u32,u64,f32,f64等

发布于 2025-01-20 07:32:27 字数 580 浏览 2 评论 0 原文

我有不同数字数据类型的各种变量(intu32u64f32f64< /code> 等)并希望将它们转换为字节数组。

例如:

a := 120  // int datatype
a_bytes = some_function(a)  // What to do here?

println(a_bytes)
// [ x, `\0`, `\0`, `\0`]   should be the output in little endian format
// OR b'x\x00\x00\x00' as a binary string

在 python 中,可以按照以下帖子所示完成:此处此处

I have various variables of different numeric datatypes(int,u32,u64,f32,f64, etc) and want to convert them to an array of bytes.

For example:

a := 120  // int datatype
a_bytes = some_function(a)  // What to do here?

println(a_bytes)
// [ x, `\0`, `\0`, `\0`]   should be the output in little endian format
// OR b'x\x00\x00\x00' as a binary string

In python, it can be done as shown in these posts: here and here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

治碍 2025-01-27 07:32:27

可以使用 encoding.binary.binary @adam oates 在评论中。

以下示例是64位数据。 32位和大型的功能也可以使用。

要将 f64 转换为/从字节( u8 )转换,我们可以使用 math.f64_bits Math.f64_from_bits 将浮点转换为IEEE 754二进制表示( u64 ),后来将其转换为字节。

import encoding.binary
import math

fn convert_u64_to_bytes(u u64) []u8 {
    mut b := []u8{len: 8, init: 0}
    binary.little_endian_put_u64(mut b, u)
    return b
}

fn convert_f64_to_bytes(val f64) []u8 {
    u := math.f64_bits(val) // returns IEEE 754 binary representation of f64
    return convert_u64_to_bytes(u)
}

fn convert_bytes_to_u64(b []u8) u64 {
    return binary.little_endian_u64(b)
}

fn convert_bytes_to_f64(b []u8) f64 {
    u := binary.little_endian_u64(b)
    return math.f64_from_bits(u)
}

fn main(){
    i := i64(-120)
    u := u64(1234)
    f := f64(-1235.345)
    
    bi := convert_u64_to_bytes(u64(i))
    bu := convert_u64_to_bytes(u)
    bf := convert_f64_to_bytes(f)
    println('$bi\n$bu\n$bf')
    
    i1 := i64(convert_bytes_to_u64(bi))
    u1 := convert_bytes_to_u64(bu)
    f1 := convert_bytes_to_f64(bf)
    println('$i1\n$u1\n$f1')
}
/* OUTPUT
[0x88, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff]
[0xd2, 0x04, `\0`, `\0`, `\0`, `\0`, `\0`, `\0`]
[{, 0x14, 0xae, G, a, M, 0x93, 0xc0]
-120
1234
-1235.345
*/

在撰写本文时, v 尚未获得适当的发布。因此,语法,函数等会受到更改。

This can be done using encoding.binary as mentioned by @Adam Oates in the comments.

The following example is for 64-bit data. Similar functions are available for 32-bit and big-endian also.

To convert f64 to/from bytes(u8) we can use math.f64_bits and math.f64_from_bits which converts the float to IEEE 754 binary representation(u64) which is later converted into bytes.

import encoding.binary
import math

fn convert_u64_to_bytes(u u64) []u8 {
    mut b := []u8{len: 8, init: 0}
    binary.little_endian_put_u64(mut b, u)
    return b
}

fn convert_f64_to_bytes(val f64) []u8 {
    u := math.f64_bits(val) // returns IEEE 754 binary representation of f64
    return convert_u64_to_bytes(u)
}

fn convert_bytes_to_u64(b []u8) u64 {
    return binary.little_endian_u64(b)
}

fn convert_bytes_to_f64(b []u8) f64 {
    u := binary.little_endian_u64(b)
    return math.f64_from_bits(u)
}

fn main(){
    i := i64(-120)
    u := u64(1234)
    f := f64(-1235.345)
    
    bi := convert_u64_to_bytes(u64(i))
    bu := convert_u64_to_bytes(u)
    bf := convert_f64_to_bytes(f)
    println('$bi\n$bu\n$bf')
    
    i1 := i64(convert_bytes_to_u64(bi))
    u1 := convert_bytes_to_u64(bu)
    f1 := convert_bytes_to_f64(bf)
    println('$i1\n$u1\n$f1')
}
/* OUTPUT
[0x88, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff]
[0xd2, 0x04, `\0`, `\0`, `\0`, `\0`, `\0`, `\0`]
[{, 0x14, 0xae, G, a, M, 0x93, 0xc0]
-120
1234
-1235.345
*/

At the time of writing this article, V is yet to get a proper release. So the syntax, functions, etc. are subject to changes.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文