aboutsummaryrefslogtreecommitdiff
path: root/hex.rs
blob: 710f572cd796867c6e53820f3380b1502d944938 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/*
 * Copyright 2013 Jack Lloyd
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

use std::{uint,vec};

pub trait ToHex {
    fn to_hex(&self) -> ~str;
}

impl<'self> ToHex for &'self [u8] {
    fn to_hex(&self) -> ~str {

        let chars = "0123456789ABCDEF".iter().collect::<~[char]>();

        let mut s = ~"";

        for uint::range(0, self.len()) |i| {

            let x = self[i];

            let xhi = (x >> 4) & 0x0F;
            let xlo = (x     ) & 0x0F;

            s.push_char(chars[xhi]);
            s.push_char(chars[xlo]);
        }

        s
    }
}

pub trait FromHex {
    fn from_hex(&self) -> ~[u8];
}

impl<'self> FromHex for &'self str {
    fn from_hex(&self) -> ~[u8] {
        let mut vec = vec::with_capacity(self.len() / 2);

        for self.iter().enumerate().advance() |(i,c)| {
            let nibble =
                if c >= '0' && c <= '9' { (c as u8) - 0x30 }
                else if c >= 'a' && c <= 'f' { (c as u8) - (0x61 - 10) }
                else if c >= 'A' && c <= 'F' { (c as u8) - (0x41 - 10) }
                else { fail!(~"bad hex character"); };

            if i % 2 == 0 {
                vec.push(nibble << 4);
            }
            else {
                vec[i/2] |= nibble;
            }
        }

        vec
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    pub fn test() {

        assert!([05u8, 0xffu8, 0x00u8, 0x59u8].to_hex() == ~"05FF0059");

        assert!("00FFA9D1F5".from_hex() == ~[0, 0xff, 0xa9, 0xd1, 0xf5]);

        assert!("00FFA9D1F5".from_hex().to_hex() == ~"00FFA9D1F5");
    }


}