APDU: Fix wrong assertion
[bcusdk.git] / archive / hex.cpp
blob3e3da111e8ecacb3cc2acc770dffb1e34d0c4ed8
1 /*
2 BCU SDK bcu development enviroment
3 Copyright (C) 2005-2008 Martin Koegler <mkoegler@auto.tuwien.ac.at>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 #include "hex.h"
22 static const uchar hexc[] = "0123456789ABCDEF";
24 CArray
25 encode_hex (CArray c)
27 CArray erg;
28 int i, j;
29 erg.resize (c () * 2 + 1);
30 for (j = i = 0; i < c (); i++, j += 2)
32 erg[j] = hexc[(c[i] >> 4) & 0x0f];
33 erg[j + 1] = hexc[(c[i] >> 0) & 0x0f];
35 erg[j] = 0;
36 return erg;
39 CArray
40 decode_hex (CArray c)
42 CArray erg;
43 int i, j;
44 uchar c1, c2;
45 erg.resize (c () / 2);
46 if (c () % 2)
47 return CArray ();
48 for (j = i = 0; i < c (); i += 2, j++)
50 c1 = c[i];
51 c2 = c[i + 1];
52 if ('0' <= c1 && c1 <= '9')
53 c1 = c1 - '0';
54 else if ('A' <= c1 && c1 <= 'F')
55 c1 = c1 - 'A' + 0xA;
56 else if ('a' <= c1 && c1 <= 'f')
57 c1 = c1 - 'a' + 0xA;
58 else
59 c1 = 0;
60 if ('0' <= c2 && c2 <= '9')
61 c2 = c2 - '0';
62 else if ('A' <= c2 && c2 <= 'F')
63 c2 = c2 - 'A' + 0xA;
64 else if ('a' <= c2 && c2 <= 'f')
65 c2 = c2 - 'a' + 0xA;
66 else
67 c2 = 0;
68 erg[j] = (c1 << 4) | c2;
70 return erg;