atMETEO
An ATmega based weather station
utils.h
Go to the documentation of this file.
1 /*
2  * atMETEO - An ATmega based weather station
3  * Copyright (C) 2014-2015 Christian Fetzer
4  *
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.
9  *
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.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19 
20 #pragma once
21 
33 #include <sstream>
34 #include <iomanip>
35 
47 template <typename T>
48 std::string to_hex(T value)
49 {
50  std::stringstream s;
51  s << std::showbase << std::setfill('0') << std::setw(sizeof(T) * 2)
52  << std::hex << static_cast<int>(value);
53  return s.str();
54 }
55 
64 template <typename T>
65 std::string to_hex(T *data, uint16_t length)
66 {
67  std::stringstream s;
68  for (uint16_t i = 0; i < length; ++i) {
69  s << to_hex(data[i]) << " ";
70  }
71  return s.str();
72 }
73 
std::string to_hex(T value)
Converts the numeric value into its hex representation.
Definition: utils.h:48