UT 10: Hex string to hex array
This example shows how to convert a number from a string representation to the long integer type depending on the base used to define the number as a string message

Required Materials

1 x Waspmote 1 x MiniUSB wire 1 x Battery

Notes

This example can be executed in Waspmote v12 and Waspmote v15

Code

1
/*
2
* ------ [UT_10] - Hex string to hex array ------
3
*
4
* Explanation: this example shows how to convert a number from a
5
* string representation to the long integer type depending on the
6
* base used to define the number as a string message
7
*
8
* Copyright (C) 2016 Libelium Comunicaciones Distribuidas S.L.
9
* http://www.libelium.com
10
*
11
* This program is free software: you can redistribute it and/or modify
12
* it under the terms of the GNU General Public License as published by
13
* the Free Software Foundation, either version 3 of the License, or
14
* (at your option) any later version.
15
*
16
* This program is distributed in the hope that it will be useful,
17
* but WITHOUT ANY WARRANTY; without even the implied warranty of
18
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19
* GNU General Public License for more details.
20
*
21
* You should have received a copy of the GNU General Public License
22
* along with this program. If not, see .
23
*
24
* Version: 1.0
25
* Design: David Gascón
26
* Implementation: Yuri Carmona
27
*/
28
29
// define variables
30
char buffer1[] = "48656C6C6F20576173706D6F7465";
31
uint8_t buffer2[200];
32
char buffer3[200];
33
uint16_t size;
34
35
36
void setup()
37
{
38
USB.ON();
39
USB.println(F("Start program"));
40
41
///////////////////////////////////////////////////////////////////
42
// 1. String to Array
43
// From 'buffer1' to 'buffer2'. The ascii representation of
44
// hexadecimal digits is converted to array of bytes:
45
// From "48656C..." to {0x48 0x65 0x6C ...}
46
///////////////////////////////////////////////////////////////////
47
size = Utils.str2hex(buffer1, buffer2, sizeof(buffer2));
48
49
50
USB.println(F("----------------------------------------------------------------------"));
51
52
USB.print(F("Input string with hex digits: \""));
53
USB.print(buffer1);
54
USB.println(F("\""));
55
56
USB.print(F("Output array (ASCII): "));
57
USB.println(buffer2, size);
58
59
USB.print(F("Output array of bytes (HEX): "));
60
for (int i=0; i
Copied!

Output

1
H#
2
Start program
3
----------------------------------------------------------------------
4
Input string with hex digits: "48656C6C6F20576173706D6F7465"
5
Output array (ASCII): Hello Waspmote
6
Output array of bytes (HEX): 0x48 0x65 0x6C 0x6C 0x6F 0x20 0x57 0x61 0x73 0x70 0x6D 0x6F 0x74 0x65
7
----------------------------------------------------------------------
8
Input array of bytes (HEX): 0x48 0x65 0x6C 0x6C 0x6F 0x20 0x57 0x61 0x73 0x70 0x6D 0x6F 0x74 0x65
9
Output string : "48656C6C6F20576173706D6F7465"
10
----------------------------------------------------------------------
11
Copied!
Last modified 1yr ago