如何将字符串 "[1, 2, 3]" 转换为 Vec<u8>?

3

我正在使用AES-GCM-SIV通过TCP在客户端/服务器之间传递加密信息。接收到的缓冲区转换为String,并使用该方法分成几个Vec<&str>

let v: Vec<&str> = buffer_string.split("?+").collect();

v的示例:

["POST / HTTP/1.1\\r\\n\\", "Uaxh5NUi098q", "178", "[162, 254, 28, 241, ... ]"]

v[3] 应该是作为 Vec<u8> 的密文。如何将这个向量用作 Vec<u8>

iter().map(|c| *c as u8).collect()

这里需要使用 chars 而不是 &str

以下是一个完整的示例,可以在 Playground 上查看:链接

fn main() {
    let buffer_string = r##"POST /chat HTTP/1.1\r\n\?+rRMUG4Lg8Gi6?+178?+[136, 136, 144, 59, 173, 25, 204, 247, 151, 53, 2, 137, 100, 45, 198, 58, 65, 210, 134, 165, 163, 156, 136, 148, 46, 31, 16, 184, 179, 73, 220, 14, 113, 152, 85, 1, 233, 208, 53, 27, 124, 52, 41, 175, 86, 109, 134, 103, 93, 148, 208, 114, 123, 97, 18, 53, 149, 195, 51, 55, 213, 114, 184, 72, 109, 30, 217, 206, 212, 58, 253, 141, 9, 45, 173, 213, 96, 35, 77, 122, 113, 240, 22, 222, 194, 11, 123, 221, 176, 116, 161, 196, 84, 203, 203, 184, 140, 42, 169, 244, 211, 1, 189, 96, 16, 62, 173, 50, 65, 48, 176, 44, 176, 246, 246, 242, 18, 146, 105, 29, 13, 223, 185, 151, 114, 30, 27, 36, 48, 178, 16, 3, 250, 49, 229, 84, 121, 135, 197, 204, 42, 140, 220, 244, 73, 184, 250, 104, 125, 224, 219, 94, 111, 247, 92, 16, 168, 50, 249, 10, 65, 214, 217, 157, 7, 113, 217, 141, 174, 139, 183, 86, 17, 24, 221, 134, 222, 240]"##;

    let v: Vec<&str> = buffer_string.split("?+").collect();
    println!("Vector: v1 {:?}, v2 {:?}, v3: {:?}", v[1], v[2], v[3]);

    //only the v[3] is needed as vec<u8>
    //error with iter and &str
    //let ciphertext_vec: Vec<_> = v[3].iter().map(|c| c.parse::<u8>().unwrap()).collect();

    let ciphertext: Vec<u8> = [
        136, 136, 144, 59, 173, 25, 204, 247, 151, 53, 2, 137, 100, 45, 198, 58, 65, 210, 134, 165,
        163, 156, 136, 148, 46, 31, 16, 184, 179, 73, 220, 14, 113, 152, 85, 1, 233, 208, 53, 27,
        124, 52, 41, 175, 86, 109, 134, 103, 93, 148, 208, 114, 123, 97, 18, 53, 149, 195, 51, 55,
        213, 114, 184, 72, 109, 30, 217, 206, 212, 58, 253, 141, 9, 45, 173, 213, 96, 35, 77, 122,
        113, 240, 22, 222, 194, 11, 123, 221, 176, 116, 161, 196, 84, 203, 203, 184, 140, 42, 169,
        244, 211, 1, 189, 96, 16, 62, 173, 50, 65, 48, 176, 44, 176, 246, 246, 242, 18, 146, 105,
        29, 13, 223, 185, 151, 114, 30, 27, 36, 48, 178, 16, 3, 250, 49, 229, 84, 121, 135, 197,
        204, 42, 140, 220, 244, 73, 184, 250, 104, 125, 224, 219, 94, 111, 247, 92, 16, 168, 50,
        249, 10, 65, 214, 217, 157, 7, 113, 217, 141, 174, 139, 183, 86, 17, 24, 221, 134, 222,
        240,
    ]
    .to_vec();
    let ciphertext2: Vec<u8> = v[3].iter().map(|c| c.parse::<u8>().unwrap()).collect();
    assert_eq!(ciphertext, ciphertext2);

    // ciphertext: Vec<u8> =
}

&stras_bytes() 方法,它返回一个 &[u8],这应该可以正常工作。如果你真的需要一个 Vec,可以使用 .as_bytes().to_vec() - user2722968
我猜你正在寻找这个 -> Playground - Ömer Erden
不幸的是,.as_bytes().to_vec() 不起作用。"[136, 136, 144,...]" 将导致 [91, 49, 51, 54, 44, 32, 49, 51, 54...] 而不是 [136, 136, 144,...] - LazyEmpiricist
看起来你的问题可能可以通过 如何以函数式的方式将字符串向量转换为整数向量? 的答案得到解答。如果不是,请 [编辑] 你的问题以解释差异。否则,我们可以将此问题标记为已回答。 - Shepmaster
添加了一个带有assert_eq!的示例。 尽管它们很相似,但我无法使“如何以功能方式将字符串向量转换为整数向量”的解决方案在我的示例中起作用。 - LazyEmpiricist
1个回答

3

我相信这就是它的全部。

fn main() {
    let s = "[162, 254, 28, 241]";
    let v: Vec<u8> = s
        .trim_start_matches('[')
        .trim_end_matches(']')
        .split(',')
        .map(|c| c.trim().parse::<u8>().unwrap())
        .collect();

    for n in v {
        println!("{}", n);
    }
}

在这里尝试一下


@TheQuantumPhysicist 嗯,我不会说它需要被“修复”,但在我看来这更符合惯用语。 - Boiethios
1
@LazyEmpiricist 只要注意在生产代码中使用 unwrap 是一个坏主意。 - The Quantum Physicist
1
使用 trim_{start,end}_matches 比使用 replace 更高效。 - Shepmaster
@Shepmaster 谢谢。代码已更新。循环总是必须的,这是肯定的。但它不会遍历整个字符串。只会从开头到第一个实例,以及从后面的最后一个实例。这就是我要寻找的优化。 - The Quantum Physicist
显示剩余10条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接