Cortex  10.0.0-a4
HexConversion.h
Go to the documentation of this file.
1 //
3 // Copyright (c) 2008-2010, Image Engine Design Inc. All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
7 // met:
8 //
9 // * Redistributions of source code must retain the above copyright
10 // notice, this list of conditions and the following disclaimer.
11 //
12 // * Redistributions in binary form must reproduce the above copyright
13 // notice, this list of conditions and the following disclaimer in the
14 // documentation and/or other materials provided with the distribution.
15 //
16 // * Neither the name of Image Engine Design nor the names of any
17 // other contributors to this software may be used to endorse or
18 // promote products derived from this software without specific prior
19 // written permission.
20 //
21 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
22 // IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
23 // THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
25 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
26 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
27 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
28 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 //
34 
37 
38 #ifndef IE_CORE_HEXCONVERSION_H
39 #define IE_CORE_HEXCONVERSION_H
40 
41 #include <iostream>
42 #include <cassert>
43 #include <string>
44 
45 #include "boost/type_traits/is_integral.hpp"
46 
47 namespace IECore
48 {
49 
50 template<typename T, typename OutputIterator>
51 inline void decToHex( T value, OutputIterator result )
52 {
53  BOOST_STATIC_ASSERT( boost::is_integral<T>::value );
54  typedef typename std::iterator_traits<OutputIterator>::value_type CharType;
55  BOOST_STATIC_ASSERT( ( boost::is_same<CharType, char>::value ) );
56 
57  for( int i = sizeof( T ) * 2 - 1; i >= 0 ; --i )
58  {
59  int m = ( value >> (i * 4) ) & 0xF;
60  *result = "0123456789ABCDEF"[m];
61  ++result;
62  }
63 }
64 
65 template<typename InputIterator, typename OutputIterator>
66 inline void decToHex( InputIterator first, InputIterator last, OutputIterator result )
67 {
68  for( ; first!=last; ++first )
69  {
70  decToHex( *first, result );
71  result += 2;
72  }
73 }
74 
75 template<typename RandomAccessIterator>
76 inline std::string decToHex( RandomAccessIterator first, RandomAccessIterator last )
77 {
78  typedef typename std::iterator_traits<RandomAccessIterator>::value_type ValueType;
79  std::string result; result.resize( sizeof( ValueType ) * 2 * ( last - first ) );
80  decToHex( first, last, result.begin() );
81  return result;
82 }
83 
84 template<typename T>
85 inline std::string decToHex( T n )
86 {
87  BOOST_STATIC_ASSERT( boost::is_integral<T>::value );
88 
89  std::string r; r.resize( sizeof( T ) * 2 );
90  decToHex( n, r.begin() );
91 
92  return r;
93 }
94 
95 template<typename T, typename InputIterator>
96 inline T hexToDec( InputIterator first, InputIterator last )
97 {
98  BOOST_STATIC_ASSERT( boost::is_integral<T>::value );
99  typedef typename std::iterator_traits<InputIterator>::value_type CharType;
100  BOOST_STATIC_ASSERT( ( boost::is_same<CharType, char>::value ) );
101 
102  T n = 0;
103 
104  for( ; first != last; ++first )
105  {
106  n <<= 4;
107 
108  switch ( *first )
109  {
110  case '0' :
111  n |= 0;
112  break;
113  case '1' :
114  n |= 1;
115  break;
116  case '2' :
117  n |= 2;
118  break;
119  case '3' :
120  n |= 3;
121  break;
122  case '4' :
123  n |= 4;
124  break;
125  case '5' :
126  n |= 5;
127  break;
128  case '6' :
129  n |= 6;
130  break;
131  case '7' :
132  n |= 7;
133  break;
134  case '8' :
135  n |= 8;
136  break;
137  case '9' :
138  n |= 9;
139  break;
140  case 'A' :
141  case 'a' :
142  n |= 10;
143  break;
144  case 'B' :
145  case 'b' :
146  n |= 11;
147  break;
148  case 'C' :
149  case 'c' :
150  n |= 12;
151  break;
152  case 'D' :
153  case 'd' :
154  n |= 13;
155  break;
156  case 'E' :
157  case 'e' :
158  n |= 14;
159  break;
160  case 'F' :
161  case 'f' :
162  n |= 15;
163  break;
164  default:
165  assert( false );
166  }
167  }
168 
169  return n;
170 }
171 
172 template<typename T>
173 inline T hexToDec( const std::string &s )
174 {
175  assert( s.size() <= sizeof( T ) * 2 );
176  return hexToDec<T>( s.begin(), s.end() );
177 }
178 
179 template<typename T, typename InputIterator, typename OutputIterator>
180 inline void hexToDec( InputIterator first, InputIterator last, OutputIterator result )
181 {
182  typedef typename std::iterator_traits<InputIterator>::value_type CharType;
183  BOOST_STATIC_ASSERT( ( boost::is_same<CharType, char>::value ) );
184 
185  for( ; first != last; first += sizeof( T ) * 2 )
186  {
187  *result = hexToDec<T>( first, first + sizeof( T ) * 2 );
188  ++result;
189  }
190 }
191 
192 } // namespace IECore
193 
194 #endif // IE_CORE_HEXCONVERSION_H
This namespace contains all components of the core library.
Definition: AddSmoothSkinningInfluencesOp.h:43