aboutsummaryrefslogtreecommitdiff
path: root/xmpp-vala/src/util.vala
blob: 6c0d0c9bd864d96707fa18d6f8456dd83618c651 (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
namespace Xmpp.Util {

 /**
  * Parse a number from a hexadecimal representation.
  *
  * Skips any whitespace at the start of the string, parses as many valid
  * characters as hexadecimal digits as possible (possibly zero) and returns
  * them as an integer value.
  *
  * ```
  * // 0xa
  * print("0x%lx\n", from_hex("A quick brown fox jumps over the lazy dog."));
  * ```
  */

public long from_hex(string numeral) {
    long result = 0;
    bool skipping_whitespace = true;
    foreach (uint8 byte in numeral.data) {
        char c = (char)byte;
        if (skipping_whitespace && c.isspace()) {
            continue;
        }
        skipping_whitespace = false;
        int digit;
        if ('0' <= c && c <= '9') {
            digit = c - '0';
        } else if ('A' <= c && c <= 'F') {
            digit = c - 'A' + 10;
        } else if ('a' <= c && c <= 'f') {
            digit = c - 'a' + 10;
        } else {
            break;
        }
        result = (result << 4) | digit;
    }
    return result;
}

}