-
Notifications
You must be signed in to change notification settings - Fork 5
/
Conversions.cpp
108 lines (89 loc) · 3.03 KB
/
Conversions.cpp
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
101
102
103
104
105
/******************************************************************************************
* Copyright 2017 Ideetron B.V.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
******************************************************************************************/
/****************************************************************************************
* File: Conversions.cpp
* Author: Gerben den Hartog
* Compagny: Ideetron B.V.
* Website: http://www.ideetron.nl/LoRa
* E-mail: [email protected]
****************************************************************************************/
/****************************************************************************************
* Created on: 12-01-2017
* Supported Hardware: ID150119-02 Nexus board with RFM95
****************************************************************************************/
/*
*****************************************************************************************
* INCLUDE FILES
*****************************************************************************************
*/
#include "Conversions.h"
void Hex2ASCII(unsigned char ASCII, unsigned char *Upper_Nibble, unsigned char *Lower_Nibble)
{
//Load upper nibble
*Upper_Nibble = (ASCII >> 4) & 0x0F;
//Load lower nibbel
*Lower_Nibble = ASCII & 0x0F;
//Convert upper nibble to ascii
if(*Upper_Nibble > 0x09)
{
*Upper_Nibble = *Upper_Nibble + 0x37;
}
else
{
*Upper_Nibble = *Upper_Nibble + 0x30;
}
//Convert Lower nibble
if(*Lower_Nibble > 0x09)
{
*Lower_Nibble = *Lower_Nibble + 0x37;
}
else
{
*Lower_Nibble = *Lower_Nibble + 0x30;
}
}
unsigned char ASCII2Hex(unsigned char Upper_Nibble, unsigned char Lower_Nibble)
{
unsigned char ASCII;
// High Nibble
if(Upper_Nibble >= '0' && Upper_Nibble <= '9')
{
ASCII =(Upper_Nibble - '0') * 16;
}
if(Upper_Nibble >= 'A' && Upper_Nibble <= 'F')
{
ASCII = (Upper_Nibble - 'A' + 10) * 16;
}
if(Upper_Nibble >= 'a' && Upper_Nibble <= 'f')
{
ASCII = (Upper_Nibble - 'a' + 10) * 16;
}
// Low Nibble
if(Lower_Nibble >= '0' && Lower_Nibble <= '9')
{
ASCII = ASCII + (Lower_Nibble - '0');
}
if(Lower_Nibble >= 'A' && Lower_Nibble <= 'F')
{
ASCII = ASCII + (Lower_Nibble - 'A' + 10);
}
if(Lower_Nibble >= 'a' && Lower_Nibble <= 'f')
{
ASCII = ASCII + (Lower_Nibble - 'a' + 10);
}
return ASCII;
}