summaryrefslogtreecommitdiff
path: root/2004/i/nono/src/utils/hexa.cc
blob: d5e471662ae5c92776aa8563651c0ac70ee1a572 (plain)
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
88
89
90
91
92
93
94
95
96
97
98
99
100
// hexa.cc
// nono - programme du robot 2004. {{{
//
// Copyright (C) 2004 Nicolas Schodet
//
// Robot APB Team/Efrei 2004.
//        Web: http://assos.efrei.fr/robot/
//      Email: robot AT efrei DOT fr
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
//
// }}}
#include "hexa.h"

// Table de conversion en hexa.
static const char *digit2hexTbl_ = "0123456789abcdef";

// Table de conversion depuis l'hexa.
#define hI hexInvalid
static const int hex2digitTbl_[] =
{
    hI,hI,hI,hI,hI,hI,hI,hI,hI,hI,hI,hI,hI,hI,hI,hI,	/* 0-15 */
    hI,hI,hI,hI,hI,hI,hI,hI,hI,hI,hI,hI,hI,hI,hI,hI,	/* 16-31 */
    hI,hI,hI,hI,hI,hI,hI,hI,hI,hI,hI,hI,hI,hI,hI,hI,	/* 32-47 */
     0, 1, 2, 3, 4, 5, 6, 7, 8, 9,hI,hI,hI,hI,hI,hI,	/* 48-63 */
    hI,10,11,12,13,14,15,hI,hI,hI,hI,hI,hI,hI,hI,hI,	/* 64-79 */
    hI,hI,hI,hI,hI,hI,hI,hI,hI,hI,hI,hI,hI,hI,hI,hI,	/* 80-95 */
    hI,10,11,12,13,14,15,hI,hI,hI,hI,hI,hI,hI,hI,hI,	/* 96-111 */
    hI,hI,hI,hI,hI,hI,hI,hI,hI,hI,hI,hI,hI,hI,hI,hI,	/* 112-127 */
    hI,hI,hI,hI,hI,hI,hI,hI,hI,hI,hI,hI,hI,hI,hI,hI,	/* 128-143 */
    hI,hI,hI,hI,hI,hI,hI,hI,hI,hI,hI,hI,hI,hI,hI,hI,	/* 144-159 */
    hI,hI,hI,hI,hI,hI,hI,hI,hI,hI,hI,hI,hI,hI,hI,hI,	/* 160-175 */
    hI,hI,hI,hI,hI,hI,hI,hI,hI,hI,hI,hI,hI,hI,hI,hI,	/* 176-191 */
    hI,hI,hI,hI,hI,hI,hI,hI,hI,hI,hI,hI,hI,hI,hI,hI,	/* 192-207 */
    hI,hI,hI,hI,hI,hI,hI,hI,hI,hI,hI,hI,hI,hI,hI,hI,	/* 208-223 */
    hI,hI,hI,hI,hI,hI,hI,hI,hI,hI,hI,hI,hI,hI,hI,hI,	/* 224-239 */
    hI,hI,hI,hI,hI,hI,hI,hI,hI,hI,hI,hI,hI,hI,hI,hI,	/* 240-255 */
};

/// Converti un caract�re hexa (0-9a-f) en chiffre (0-15).
int
hex2digit (char c)
{
    return hex2digitTbl_[c];
}

/// Converti un chiffre (0-15) en hexa (0-9a-f).
char
digit2hex (int d)
{
    return digit2hexTbl_[d];
}

/// D�code un mot non-sign� (1 octets).
unsigned int
hexUnsignedChar2int (const char *s)
{
    int h1 = hex2digit (s[0]);
    int h0 = hex2digit (s[1]);
    if (h1 == hexInvalid || h0 == hexInvalid)
	return hexInvalid;
    return (unsigned char) (h1 << 4 | h0);
}

/// D�code un mot sign� (1 octets).
int
hexSignedChar2int (const char *s)
{
    int h1 = hex2digit (s[0]);
    int h0 = hex2digit (s[1]);
    if (h1 == hexInvalid || h0 == hexInvalid)
	return hexInvalid;
    return (signed char) (h1 << 4 | h0);
}

/// D�code un mot sign� (2 octets).
int
hexSignedShort2int (const char *s)
{
    int h3 = hex2digit (s[0]);
    int h2 = hex2digit (s[1]);
    int h1 = hex2digit (s[2]);
    int h0 = hex2digit (s[3]);
    if (h3 == hexInvalid || h2 == hexInvalid || h1 == hexInvalid || h0 == hexInvalid)
	return hexInvalid;
    return (short) (h3 << 12 | h2 << 8 | h1 << 4 | h0);
}