Skip to main content

to_u16

Function to_u16 

Source
pub fn to_u16(id_str: &str) -> Result<u16, &'static str>
Available on crate feature std only.
Expand description

Converts a 2-character string ID into its u16 integer representation.

The input string must consist of two characters within the printable ASCII range (0x21 to 0x7E). Uses Base-94 encoding: ID = (c1 - 0x21) * 94 + (c2 - 0x21)

ยงExamples

use pk_command::msg_id;
assert_eq!(msg_id::to_u16("!!"), Ok(0)); // minimum
assert_eq!(msg_id::to_u16("!\""), Ok(1));
assert_eq!(msg_id::to_u16("\"!"), Ok(94));
assert_eq!(msg_id::to_u16("~~"), Ok(8835)); // maximum

assert!(msg_id::to_u16("!").is_err()); // invalid length