1 /* 2 hex.h 3 Copyright (C) 2013 Jonathan Neuschäfer <j.neuschaefer@gmx.net> 4 */ 5 6 /* 7 This file is part of Freeminer. 8 9 Freeminer is free software: you can redistribute it and/or modify 10 it under the terms of the GNU General Public License as published by 11 the Free Software Foundation, either version 3 of the License, or 12 (at your option) any later version. 13 14 Freeminer is distributed in the hope that it will be useful, 15 but WITHOUT ANY WARRANTY; without even the implied warranty of 16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 GNU General Public License for more details. 18 19 You should have received a copy of the GNU General Public License 20 along with Freeminer. If not, see <http://www.gnu.org/licenses/>. 21 */ 22 23 #ifndef HEX_HEADER 24 #define HEX_HEADER 25 26 #include <string> 27 28 static const char hex_chars[] = "0123456789abcdef"; 29 hex_encode(const char * data,unsigned int data_size)30static inline std::string hex_encode(const char *data, unsigned int data_size) 31 { 32 std::string ret; 33 char buf2[3]; 34 buf2[2] = '\0'; 35 36 for(unsigned int i = 0; i < data_size; i++) 37 { 38 unsigned char c = (unsigned char) data[i]; 39 buf2[0] = hex_chars[(c & 0xf0) >> 4]; 40 buf2[1] = hex_chars[c & 0x0f]; 41 ret.append(buf2); 42 } 43 44 return ret; 45 } 46 hex_encode(const std::string & data)47static inline std::string hex_encode(const std::string &data) 48 { 49 return hex_encode(data.c_str(), data.size()); 50 } 51 hex_digit_decode(char hexdigit,unsigned char & value)52static inline bool hex_digit_decode(char hexdigit, unsigned char &value) 53 { 54 if(hexdigit >= '0' && hexdigit <= '9') 55 value = hexdigit - '0'; 56 else if(hexdigit >= 'A' && hexdigit <= 'F') 57 value = hexdigit - 'A' + 10; 58 else if(hexdigit >= 'a' && hexdigit <= 'f') 59 value = hexdigit - 'a' + 10; 60 else 61 return false; 62 return true; 63 } 64 65 #endif 66