]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
util: add function converting u8-array into a hex-String
authorfrank honza <frank.honza@dcso.de>
Sun, 18 Oct 2020 19:50:29 +0000 (21:50 +0200)
committerVictor Julien <victor@inliniac.net>
Fri, 5 Mar 2021 13:47:10 +0000 (14:47 +0100)
rust/src/common.rs

index d9341881f7e33a72a03f57949095914956c3ca46..1d28ba5f0a789cd1b3a26d3612a65abd8d749221 100644 (file)
@@ -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();
+}