From: frank honza Date: Sun, 18 Oct 2020 19:50:29 +0000 (+0200) Subject: util: add function converting u8-array into a hex-String X-Git-Tag: suricata-7.0.0-beta1~1734 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=e9494ddd8ffa8c14e82fdaed673b49ddbf82368c;p=thirdparty%2Fsuricata.git util: add function converting u8-array into a hex-String --- diff --git a/rust/src/common.rs b/rust/src/common.rs index d9341881f7..1d28ba5f0a 100644 --- a/rust/src/common.rs +++ b/rust/src/common.rs @@ -72,3 +72,12 @@ pub unsafe extern "C" fn rs_cstring_free(s: *mut c_char) { } drop(CString::from_raw(s)); } + +/// Convert an u8-array of data into a hexadecimal representation +pub fn to_hex(input: &[u8]) -> String { + static CHARS: &'static [u8] = b"0123456789abcdef"; + + return input.iter().map( + |b| vec![char::from(CHARS[(b >> 4) as usize]), char::from(CHARS[(b & 0xf) as usize])] + ).flatten().collect(); +}