Bytes to String

let mut buffer: Vec<u8> = Vec::new();
let mut reader = BufReader::new(&stream);
reader.read_until(b'\n', &mut buffer).expect("Could not read into buffer");
let peer_public_key = Input::from(&buffer);

When I print peer_public_key, the output is,

Input { value: Slice { bytes: [35, 225, 231, 19, 248, 91, 156, 0, 31, 55, 245, 167, 6, 51, 124, 220, 39, 162, 113, 223, 165, 4, 248, 173, 177, 69, 22, 249, 138, 133, 145, 112] } }

My question is, how do I process the 'peer_public_key' so that I can access those bytes values. The aim is to encode those values to a string using hex::encode

Thanks.

hex::encode takes anything that implements AsRef<[u8]>, so you should be able to pass buffer to it directly:

hex::encode(&buffer)
1 Like

Thanks, it worked !

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.