rippled
strHex.h
1 //------------------------------------------------------------------------------
2 /*
3  This file is part of rippled: https://github.com/ripple/rippled
4  Copyright (c) 2012, 2013 Ripple Labs Inc.
5 
6  Permission to use, copy, modify, and/or distribute this software for any
7  purpose with or without fee is hereby granted, provided that the above
8  copyright notice and this permission notice appear in all copies.
9 
10  THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18 //==============================================================================
19 
20 // Copyright (c) 2009-2010 Satoshi Nakamoto
21 // Copyright (c) 2011 The Bitcoin developers
22 // Distributed under the MIT/X11 software license, see the accompanying
23 // file license.txt or http://www.opensource.org/licenses/mit-license.php.
24 
25 #ifndef RIPPLE_BASICS_STRHEX_H_INCLUDED
26 #define RIPPLE_BASICS_STRHEX_H_INCLUDED
27 
28 #include <cassert>
29 #include <string>
30 
31 #include <boost/algorithm/hex.hpp>
32 #include <boost/endian/conversion.hpp>
33 
34 namespace ripple {
35 
40 inline
41 char
42 charHex (unsigned int digit)
43 {
44  static
45  char const xtab[] = "0123456789ABCDEF";
46 
47  assert (digit < 16);
48 
49  return xtab[digit];
50 }
51 
57 int
58 charUnHex (unsigned char c);
59 
60 inline
61 int
62 charUnHex (char c)
63 {
64  return charUnHex (static_cast<unsigned char>(c));
65 }
68 template <class FwdIt>
70 strHex(FwdIt begin, FwdIt end)
71 {
72  static_assert(
76  "FwdIt must be a forward iterator");
77  std::string result;
78  result.reserve(2 * std::distance(begin, end));
79  boost::algorithm::hex(begin, end, std::back_inserter(result));
80  return result;
81 }
82 
83 template <class T, class = decltype(std::declval<T>().begin())>
84 std::string strHex(T const& from)
85 {
86  return strHex(from.begin(), from.end());
87 }
88 
89 inline std::string strHex (const std::uint64_t uiHost)
90 {
91  uint64_t uBig = boost::endian::native_to_big (uiHost);
92 
93  auto const begin = (unsigned char*) &uBig;
94  auto const end = begin + sizeof(uBig);
95  return strHex(begin, end);
96 }
97 }
98 
99 #endif
std::string
STL class.
ripple::charUnHex
int charUnHex(unsigned char c)
Converts a hex digit to the corresponding integer.
Definition: strHex.cpp:26
std::string::reserve
T reserve(T... args)
std::back_inserter
T back_inserter(T... args)
std::forward_iterator_tag
std::distance
T distance(T... args)
std::is_convertible
std::iterator_traits
std::uint64_t
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
std::string::begin
T begin(T... args)
cassert
ripple::charHex
char charHex(unsigned int digit)
Converts an integer to the corresponding hex digit.
Definition: strHex.h:42
ripple::strHex
std::string strHex(FwdIt begin, FwdIt end)
Definition: strHex.h:70
string