Note: Goby version 1 (shown here) is now considered obsolete. Please use version 2 for new projects, and consider upgrading old projects.

Goby Underwater Autonomy Project  Series: 1.1, revision: 163, released on 2013-02-06 14:23:27 -0500
util/binary.h
00001 // copyright 2010 t. schneider tes@mit.edu
00002 // ocean engineering graudate student - mit / whoi joint program
00003 // massachusetts institute of technology (mit)
00004 // laboratory for autonomous marine sensing systems (lamss)    
00005 // 
00006 // this file is part of goby-util, a collection of utility libraries
00007 //
00008 //
00009 // This program is free software: you can redistribute it and/or modify
00010 // it under the terms of the GNU General Public License as published by
00011 // the Free Software Foundation, either version 3 of the License, or
00012 // (at your option) any later version.
00013 //
00014 // This software is distributed in the hope that it will be useful,
00015 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00016 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017 // GNU General Public License for more details.
00018 //
00019 // You should have received a copy of the GNU General Public License
00020 // along with this software.  If not, see <http://www.gnu.org/licenses/>.
00021 
00022 #ifndef BINARY20100713H
00023 #define BINARY20100713H
00024 
00025 #include <iomanip>
00026 #include <cmath>
00027 #include <sstream>
00028 
00029 #include <boost/dynamic_bitset.hpp>
00030 #include <bitset>
00031 
00032 namespace goby
00033 {
00034     namespace util
00035     {
00037 
00038 
00039         
00040         
00047         inline bool char_array2hex_string(const unsigned char* c, std::string& s, const unsigned int n)
00048         {
00049             std::stringstream ss;
00050             for (unsigned int i=0; i<n; i++)
00051                 ss << std::hex << std::setw(2) << std::setfill('0') << static_cast<unsigned int>(c[i]);
00052         
00053             s = ss.str();
00054             return true;
00055         }
00056 
00058         inline bool hex_string2char_array(unsigned char* c, const std::string& s, const unsigned int n)
00059         {
00060             for (unsigned int i = 0; i<n; i++)
00061             {
00062                 std::stringstream ss;
00063                 unsigned int in;
00064                 ss <<  s.substr(2*i, 2);
00065                 ss >> std::hex >> in;
00066                 c[i] = static_cast<unsigned char>(in); 
00067             }
00068             return true;
00069         }
00070 
00072         inline std::string long2binary_string(unsigned long l, unsigned short bits)
00073         {
00074             char s [bits+1];
00075             for (unsigned int i=0; i<bits; i++)
00076             {
00077                 s[bits-i-1] = (l&1) ? '1' : '0';
00078                 l >>= 1;
00079             }
00080             s[bits] = '\0';    
00081             return (std::string)s;
00082         }
00083     
00085         inline std::string binary_string2hex_string(const std::string & bs)
00086         {
00087             std::string hs;
00088             unsigned int bytes = (unsigned int)(ceil(bs.length()/8.0));
00089             unsigned char c[bytes];
00090     
00091             for(size_t i = 0; i < bytes; ++i)
00092             {
00093                 std::bitset<8> b(bs.substr(i*8,8));    
00094                 c[i] = (char)b.to_ulong();
00095             }
00096 
00097             char_array2hex_string(c, hs, bytes);
00098 
00099             return hs;
00100         }
00101 
00103         inline std::string dyn_bitset2hex_string(const boost::dynamic_bitset<unsigned char>& bits, unsigned trim_to_bytes_size = 0)
00104         {
00105             std::stringstream binary;
00106             binary << bits;
00107             std::string out = binary_string2hex_string(binary.str());
00108 
00109             if(trim_to_bytes_size)
00110                 return out.substr(out.length() - trim_to_bytes_size*2);
00111             else
00112                 return out;
00113         }
00114     
00118         inline std::string hex_string2binary_string(const std::string & bs)
00119         {
00120             int bytes = bs.length()/2;
00121             unsigned char c[bytes];
00122     
00123             hex_string2char_array(c, bs, bytes);
00124 
00125             std::string hs;
00126     
00127             for (size_t i = 0; i < (size_t)bytes; i++)
00128                 hs += long2binary_string((unsigned long)c[i], 8);
00129 
00130             return hs;
00131         }
00132 
00133 
00135         inline boost::dynamic_bitset<unsigned char> hex_string2dyn_bitset(const std::string & hs, unsigned bits_size = 0)
00136         {
00137             boost::dynamic_bitset<unsigned char> bits;
00138             std::stringstream bin_str;
00139             bin_str << hex_string2binary_string(hs);       
00140             bin_str >> bits;
00141 
00142             if(bits_size) bits.resize(bits_size);        
00143             return bits;
00144         }
00145 
00149         template <typename T> bool hex_string2number(const std::string & s, T & t)
00150         {
00151             std::stringstream ss;
00152             ss << s;
00153             ss >> std::hex >> t;
00154             return !ss.fail();        
00155         }
00156         
00157         
00164         template <typename T> bool number2hex_string(std::string & s, const T & t, unsigned int width = 2)
00165         {
00166             std::stringstream ss;
00167             ss << std::hex << std::setw(width) << std::setfill('0') << static_cast<unsigned int>(t);
00168             s  = ss.str();
00169             return !ss.fail();        
00170         }
00171 
00177         template <typename T> std::string number2hex_string(const T & t, unsigned int width = 2)
00178         {
00179             std::string s;
00180             number2hex_string(s,t,width);
00181             return s;
00182         }
00183 
00185     }
00186 }
00187 
00188 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends