#[cfg(not(feature = "uppercase_hex"))]
const OFF: u8 = 0x27;
#[cfg(feature = "uppercase_hex")]
const OFF: u8 = 0x7;
pub fn byte_to_two_digit_hex(value: u8) -> [char; 2] {
let mut low = (value & 0xf) | 0x30;
if low > 0x39 {
low += OFF
};
let mut high = (value >> 4) | 0x30;
if high > 0x39 {
high += OFF
};
[high as char, low as char]
}
pub fn char_to_surrogate_pair(value: char) -> [char; 8] {
let number = value as u32;
let pair = (((number - 0x10000) & 0xffc00) << 6) | (number & 0x3ff) | 0xd800_dc00;
let b1 = {
let bt = (pair & 0xf) as u8 | 0x30;
if bt > 0x39 {
bt + OFF
} else {
bt
}
};
let b2 = {
let bt = (pair >> 4 & 0xf) as u8 | 0x30;
if bt > 0x39 {
bt + OFF
} else {
bt
}
};
let b3 = {
((pair >> 8 & 0xf) as u8 | 0x30) + OFF
};
let b4 = {
let bt = (pair >> 12 & 0xf) as u8 | 0x30;
bt + OFF
};
let b5 = {
let bt = (pair >> 16 & 0xf) as u8 | 0x30;
if bt > 0x39 {
bt + OFF
} else {
bt
}
};
let b6 = {
let bt = (pair >> 20 & 0xf) as u8 | 0x30;
if bt > 0x39 {
bt + OFF
} else {
bt
}
};
let b7 = {
(pair >> 24 & 0xf) as u8 | 0x30
};
let b8 = {
((pair >> 28 & 0xf) as u8 | 0x30) + OFF
};
[
b8 as char, b7 as char, b6 as char, b5 as char, b4 as char, b3 as char, b2 as char,
b1 as char,
]
}
pub fn char_to_four_digit_hex(value: char) -> [char; 4] {
let number = value as u32;
let b1 = {
let bt = (number & 0xf) as u8 | 0x30;
if bt > 0x39 {
bt + OFF
} else {
bt
}
};
let b2 = {
let bt = (number >> 4 & 0xf) as u8 | 0x30;
if bt > 0x39 {
bt + OFF
} else {
bt
}
};
let b3 = {
let bt = (number >> 8 & 0xf) as u8 | 0x30;
if bt > 0x39 {
bt + OFF
} else {
bt
}
};
let b4 = {
let bt = (number >> 12 & 0xf) as u8 | 0x30;
if bt > 0x39 {
bt + OFF
} else {
bt
}
};
[b4 as char, b3 as char, b2 as char, b1 as char]
}
pub fn char_to_six_digit_hex(value: char) -> [char; 6] {
let number = value as u32;
let b1 = {
let bt = (number & 0xf) as u8 | 0x30;
if bt > 0x39 {
bt + OFF
} else {
bt
}
};
let b2 = {
let bt = (number >> 4 & 0xf) as u8 | 0x30;
if bt > 0x39 {
bt + OFF
} else {
bt
}
};
let b3 = {
let bt = (number >> 8 & 0xf) as u8 | 0x30;
if bt > 0x39 {
bt + OFF
} else {
bt
}
};
let b4 = {
let bt = (number >> 12 & 0xf) as u8 | 0x30;
if bt > 0x39 {
bt + OFF
} else {
bt
}
};
let b5 = {
let bt = (number >> 16 & 0xf) as u8 | 0x30;
if bt > 0x39 {
bt + OFF
} else {
bt
}
};
let b6 = {
(number >> 20 & 0xf) as u8 | 0x30
};
[
b6 as char, b5 as char, b4 as char, b3 as char, b2 as char, b1 as char,
]
}
pub fn dword_to_eight_digit_hex(value: u32) -> [char; 8] {
let b1 = {
let bt = (value & 0xf) as u8 | 0x30;
if bt > 0x39 {
bt + OFF
} else {
bt
}
};
let b2 = {
let bt = (value >> 4 & 0xf) as u8 | 0x30;
if bt > 0x39 {
bt + OFF
} else {
bt
}
};
let b3 = {
let bt = (value >> 8 & 0xf) as u8 | 0x30;
if bt > 0x39 {
bt + OFF
} else {
bt
}
};
let b4 = {
let bt = (value >> 12 & 0xf) as u8 | 0x30;
if bt > 0x39 {
bt + OFF
} else {
bt
}
};
let b5 = {
let bt = (value >> 16 & 0xf) as u8 | 0x30;
if bt > 0x39 {
bt + OFF
} else {
bt
}
};
let b6 = {
let bt = (value >> 20 & 0xf) as u8 | 0x30;
if bt > 0x39 {
bt + OFF
} else {
bt
}
};
let b7 = {
let bt = (value >> 24 & 0xf) as u8 | 0x30;
if bt > 0x39 {
bt + OFF
} else {
bt
}
};
let b8 = {
let bt = (value >> 28 & 0xf) as u8 | 0x30;
if bt > 0x39 {
bt + OFF
} else {
bt
}
};
[
b8 as char, b7 as char, b6 as char, b5 as char, b4 as char, b3 as char, b2 as char,
b1 as char,
]
}
#[cfg(test)]
mod test {
use crate::strings::hex::char_to_six_digit_hex;
use super::byte_to_two_digit_hex;
use super::char_to_four_digit_hex;
use super::char_to_surrogate_pair;
use super::dword_to_eight_digit_hex;
#[cfg(not(feature = "uppercase_hex"))]
const A: char = 'a';
#[cfg(not(feature = "uppercase_hex"))]
const B: char = 'b';
#[cfg(not(feature = "uppercase_hex"))]
const C: char = 'c';
#[cfg(not(feature = "uppercase_hex"))]
const D: char = 'd';
#[cfg(not(feature = "uppercase_hex"))]
const E: char = 'e';
#[cfg(not(feature = "uppercase_hex"))]
const F: char = 'f';
#[cfg(feature = "uppercase_hex")]
const A: char = 'A';
#[cfg(feature = "uppercase_hex")]
const B: char = 'B';
#[cfg(feature = "uppercase_hex")]
const C: char = 'C';
#[cfg(feature = "uppercase_hex")]
const D: char = 'D';
#[cfg(feature = "uppercase_hex")]
const E: char = 'E';
#[cfg(feature = "uppercase_hex")]
const F: char = 'F';
#[test]
fn hex_byte_test() {
assert_eq!(byte_to_two_digit_hex(0x00), ['0', '0']);
assert_eq!(byte_to_two_digit_hex(0x01), ['0', '1']);
assert_eq!(byte_to_two_digit_hex(0x10), ['1', '0']);
assert_eq!(byte_to_two_digit_hex(0xee), [E, E]);
assert_eq!(byte_to_two_digit_hex(0xff), [F, F]);
assert_eq!(byte_to_two_digit_hex(0xf0), [F, '0']);
assert_eq!(byte_to_two_digit_hex(0x0f), ['0', F]);
assert_eq!(byte_to_two_digit_hex(0x55), ['5', '5']);
assert_eq!(byte_to_two_digit_hex(0x45), ['4', '5']);
assert_eq!(byte_to_two_digit_hex(0x54), ['5', '4']);
assert_eq!(byte_to_two_digit_hex(0x80), ['8', '0']);
}
#[test]
fn char_to_four_digit_hex_test() {
assert_eq!(char_to_four_digit_hex('\u{0000}'), ['0', '0', '0', '0']);
assert_eq!(char_to_four_digit_hex('\u{000f}'), ['0', '0', '0', F]);
assert_eq!(char_to_four_digit_hex('\u{00f0}'), ['0', '0', F, '0']);
assert_eq!(char_to_four_digit_hex('\u{0f00}'), ['0', F, '0', '0']);
assert_eq!(char_to_four_digit_hex('\u{f000}'), [F, '0', '0', '0']);
}
#[test]
fn char_to_six_digit_hex_test() {
assert_eq!(
char_to_six_digit_hex('\u{000000}'),
['0', '0', '0', '0', '0', '0']
);
assert_eq!(
char_to_six_digit_hex('\u{00000f}'),
['0', '0', '0', '0', '0', F]
);
assert_eq!(
char_to_six_digit_hex('\u{0000f0}'),
['0', '0', '0', '0', F, '0']
);
assert_eq!(
char_to_six_digit_hex('\u{000f00}'),
['0', '0', '0', F, '0', '0']
);
assert_eq!(
char_to_six_digit_hex('\u{00f000}'),
['0', '0', F, '0', '0', '0']
);
assert_eq!(
char_to_six_digit_hex('\u{0f0000}'),
['0', F, '0', '0', '0', '0']
);
assert_eq!(char_to_six_digit_hex('\u{10ffff}'), ['1', '0', F, F, F, F]);
}
#[test]
fn hex_dword_test() {
assert_eq!(
dword_to_eight_digit_hex(0x0000_0000),
['0', '0', '0', '0', '0', '0', '0', '0']
);
assert_eq!(
dword_to_eight_digit_hex(0x0000_0001),
['0', '0', '0', '0', '0', '0', '0', '1']
);
assert_eq!(
dword_to_eight_digit_hex(0x0000_0020),
['0', '0', '0', '0', '0', '0', '2', '0']
);
assert_eq!(
dword_to_eight_digit_hex(0x0000_0300),
['0', '0', '0', '0', '0', '3', '0', '0']
);
assert_eq!(
dword_to_eight_digit_hex(0x0000_4000),
['0', '0', '0', '0', '4', '0', '0', '0']
);
assert_eq!(
dword_to_eight_digit_hex(0x0005_0000),
['0', '0', '0', '5', '0', '0', '0', '0']
);
assert_eq!(
dword_to_eight_digit_hex(0x0060_0000),
['0', '0', '6', '0', '0', '0', '0', '0']
);
assert_eq!(
dword_to_eight_digit_hex(0x0700_0000),
['0', '7', '0', '0', '0', '0', '0', '0']
);
assert_eq!(
dword_to_eight_digit_hex(0x8000_0000),
['8', '0', '0', '0', '0', '0', '0', '0']
);
assert_eq!(
dword_to_eight_digit_hex(0x0000_0000),
['0', '0', '0', '0', '0', '0', '0', '0']
);
assert_eq!(
dword_to_eight_digit_hex(0x0000_000f),
['0', '0', '0', '0', '0', '0', '0', F]
);
assert_eq!(
dword_to_eight_digit_hex(0x0000_00e0),
['0', '0', '0', '0', '0', '0', E, '0']
);
assert_eq!(
dword_to_eight_digit_hex(0x0000_0d00),
['0', '0', '0', '0', '0', D, '0', '0']
);
assert_eq!(
dword_to_eight_digit_hex(0x0000_c000),
['0', '0', '0', '0', C, '0', '0', '0']
);
assert_eq!(
dword_to_eight_digit_hex(0x000b_0000),
['0', '0', '0', B, '0', '0', '0', '0']
);
assert_eq!(
dword_to_eight_digit_hex(0x00a0_0000),
['0', '0', A, '0', '0', '0', '0', '0']
);
assert_eq!(
dword_to_eight_digit_hex(0x0b00_0000),
['0', B, '0', '0', '0', '0', '0', '0']
);
assert_eq!(
dword_to_eight_digit_hex(0xc000_0000),
[C, '0', '0', '0', '0', '0', '0', '0']
);
}
#[test]
fn char_to_surrogate_test() {
assert_eq!(
char_to_surrogate_pair('\u{10037}'),
[D, '8', '0', '0', D, C, '3', '7']
);
assert_eq!(
char_to_surrogate_pair('\u{3ffff}'),
[D, '8', B, F, D, F, F, F]
);
assert_eq!(
char_to_surrogate_pair('\u{20234}'),
[D, '8', '4', '0', D, E, '3', '4']
);
assert_eq!(
char_to_surrogate_pair('\u{189e2}'),
[D, '8', '2', '2', D, D, E, '2']
);
assert_eq!(
char_to_surrogate_pair('\u{32001}'),
[D, '8', '8', '8', D, C, '0', '1']
);
assert_eq!(
char_to_surrogate_pair('\u{1000f}'),
[D, '8', '0', '0', D, C, '0', F]
);
assert_eq!(
char_to_surrogate_pair('\u{100f0}'),
[D, '8', '0', '0', D, C, F, '0']
);
assert_eq!(
char_to_surrogate_pair('\u{10f00}'),
[D, '8', '0', '3', D, F, '0', '0']
);
assert_eq!(
char_to_surrogate_pair('\u{1f000}'),
[D, '8', '3', C, D, C, '0', '0']
);
assert_eq!(
char_to_surrogate_pair('\u{30000}'),
[D, '8', '8', '0', D, C, '0', '0']
);
}
}