BitShares-Core  4.0.0
BitShares blockchain implementation and command-line interface software
hex.cpp
Go to the documentation of this file.
1 #include <fc/crypto/hex.hpp>
3 
4 namespace fc {
5 
6  uint8_t from_hex( char c ) {
7  if( c >= '0' && c <= '9' )
8  return c - '0';
9  if( c >= 'a' && c <= 'f' )
10  return c - 'a' + 10;
11  if( c >= 'A' && c <= 'F' )
12  return c - 'A' + 10;
13  FC_THROW_EXCEPTION( exception, "Invalid hex character '${c}'", ("c", std::string(&c,1) ) );
14  return 0;
15  }
16 
17  std::string to_hex( const char* d, uint32_t s )
18  {
19  std::string r;
20  const char* to_hex="0123456789abcdef";
21  uint8_t* c = (uint8_t*)d;
22  for( uint32_t i = 0; i < s; ++i )
23  (r += to_hex[(c[i]>>4)]) += to_hex[(c[i] &0x0f)];
24  return r;
25  }
26 
27  size_t from_hex( const std::string& hex_str, char* out_data, size_t out_data_len ) {
28  std::string::const_iterator i = hex_str.begin();
29  uint8_t* out_pos = (uint8_t*)out_data;
30  uint8_t* out_end = out_pos + out_data_len;
31  while( i != hex_str.end() && out_end != out_pos ) {
32  *out_pos = from_hex( *i ) << 4;
33  ++i;
34  if( i != hex_str.end() ) {
35  *out_pos |= from_hex( *i );
36  ++i;
37  }
38  ++out_pos;
39  }
40  return out_pos - (uint8_t*)out_data;
41  }
42  std::string to_hex( const std::vector<char>& data )
43  {
44  if( data.size() )
45  return to_hex( data.data(), data.size() );
46  return "";
47  }
48 
49 }
uint8_t from_hex(char c)
Definition: hex.cpp:6
Used to generate a useful error report when an exception is thrown.At each level in the stack where t...
Definition: exception.hpp:56
#define FC_THROW_EXCEPTION(EXCEPTION, FORMAT,...)
Definition: exception.hpp:378
Defines exception&#39;s used by fc.
Definition: api.hpp:15
std::string to_hex(const char *d, uint32_t s)
Definition: hex.cpp:17