TsHexUtils.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2008 National Institute For Space Research (INPE) - Brazil.
2 
3  This file is part of the TerraLib - a Framework for building GIS enabled applications.
4 
5  TerraLib is free software: you can redistribute it and/or modify
6  it under the terms of the GNU Lesser General Public License as published by
7  the Free Software Foundation, either version 3 of the License,
8  or (at your option) any later version.
9 
10  TerraLib is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public License
16  along with TerraLib. See COPYING. If not, write to
17  TerraLib Team at <terralib-team@terralib.org>.
18  */
19 
20 // Unit-Test TerraLib
21 #include "TsHexUtils.h"
22 
23 #include <terralib/common.h>
25 
26 // STL
27 #include <cstdlib>
28 #include <string>
29 #include <vector>
30 
31 // Boost
32 #include <boost/lexical_cast.hpp>
33 #include <boost/property_tree/json_parser.hpp>
34 #include <boost/foreach.hpp>
35 
37 
38 static std::vector<size_t> dec_val;
39 static std::vector<unsigned char> dec_val_uchar;
40 size_t dec_invalid;
41 unsigned char dec_invalid_uc;
42 static std::vector<unsigned char> hex_lcase;
43 static std::vector<unsigned char> hex_ucase;
44 static std::vector<unsigned char> hex_invalid_0_F;
45 static std::vector<unsigned char> hex_upper;
46 static std::vector<unsigned char> hex_lower;
47 
49 {
50  // Reading input data (hex values) from a json file and output values (decimal values 0-15)
51  boost::property_tree::ptree test_data;
52  boost::property_tree::read_json("../data/module_common/hexutils/hexutils_inp_out.json", test_data);
53  te::common::Convert(test_data.get_child("input.hex_Lcase"), hex_lcase);
54  te::common::Convert(test_data.get_child("input.hex_Ucase"), hex_ucase);
55  te::common::Convert(test_data.get_child("input.hex_A_F"), hex_upper);
56  te::common::Convert(test_data.get_child("input.hex_a_f"), hex_lower);
57  te::common::Convert(test_data.get_child("input.hex_outof_0_9_A_F"), hex_invalid_0_F);
58  te::common::Convert(test_data.get_child("outp.dec_val"), dec_val);
59  //te::common::Convert(test_data.get_child("outp.dec_val"), dec_val_uchar); //boost error
60  dec_invalid =boost::lexical_cast<size_t>(test_data.get_child("outp.dec_val_invalid").data());
61  //dec_invalid_uc =boost::lexical_cast<unsigned char>(test_data.get_child("outp.dec_val_invalid").data());//boost error
62 
63  // Boost can not read expected output data (unsigned char) from json file using lexical_cast
64  // Then, the decimal values expected are defined below and will be checked against the real returned values.
65  dec_val_uchar.push_back(0);
66  dec_val_uchar.push_back(1);
67  dec_val_uchar.push_back(2);
68  dec_val_uchar.push_back(3);
69  dec_val_uchar.push_back(4);
70  dec_val_uchar.push_back(5);
71  dec_val_uchar.push_back(6);
72  dec_val_uchar.push_back(7);
73  dec_val_uchar.push_back(8);
74  dec_val_uchar.push_back(9);
75  dec_val_uchar.push_back(10);
76  dec_val_uchar.push_back(11);
77  dec_val_uchar.push_back(12);
78  dec_val_uchar.push_back(13);
79  dec_val_uchar.push_back(14);
80  dec_val_uchar.push_back(15);
81  }
82 
84 {
85  dec_val.clear();
86  hex_lcase.clear();
87  hex_ucase.clear();
88  hex_invalid_0_F.clear();
89  hex_upper.clear();
90  hex_lower.clear();
91  dec_val_uchar.clear();
92 }
93 
95 {
96  // hex_value: [0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F]
97  // hex_value: [0,1,2,3,4,5,6,7,8,9,a,b,c,d,e,f]
98 
99  // dec-value: [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]
100 
101  // initial tests ...
102  char input1_hex = 'A'; // 10
103  char input2_hex = 'a'; // 10
104  char input3_hex = '1'; // 1
105  char input4_hex = 'f'; // 15
106  char input5_hex = 'I'; // 15 //invalid input return 15. How to detect if the input value is f/F or other invalid values?
107 
108  CPPUNIT_ASSERT( te::core::GetDecimalFromHexNotCS(input1_hex) == 10);
109  CPPUNIT_ASSERT( te::core::GetDecimalFromHexNotCS(input2_hex) == 10);
110  CPPUNIT_ASSERT( te::core::GetDecimalFromHexNotCS(input3_hex) == 1);
111  CPPUNIT_ASSERT( te::core::GetDecimalFromHexNotCS(input4_hex) == 15);
112  CPPUNIT_ASSERT( te::core::GetDecimalFromHexNotCS(input5_hex) == 15);
113 
114  // calling function over input data from json file and checking the output against the expected output (decimal: dec_val or dec_val_uchar)
115  std::vector<unsigned char>::iterator it = hex_lcase.begin();
116  std::vector<unsigned char>::iterator it_output_uc = dec_val_uchar.begin();
117  std::vector<size_t>::iterator it_output = dec_val.begin();
118 
119  std::cout << std::endl;
120  while ( it < hex_lcase.end() && it_output < dec_val.end() && it_output_uc < dec_val_uchar.end())
121  {
122  std::cout << "Input unsigned char: " << *it <<" GetDecimalFromHexUCase: " << te::core::GetDecimalFromHexNotCS(*it) << std::endl;
123  std::cout << "Input unsigned char: " << *it <<" Output size_t: " << *it_output <<" Output uchar: " << *it_output_uc << std::endl;
124  unsigned char res_uc = te::core::GetDecimalFromHexNotCS(*it);
125  CPPUNIT_ASSERT( te::core::GetDecimalFromHexNotCS(*it) == *it_output);
126  CPPUNIT_ASSERT( te::core::GetDecimalFromHexNotCS(*it) == *it_output_uc);
127  CPPUNIT_ASSERT( res_uc == *it_output_uc);
128 
129  it++ ;
130  it_output++ ;
131  it_output_uc++;
132  }
133 
134  // testing invalid values as input (out of the range 0-9, A-F)
135  it = hex_invalid_0_F.begin();
136 
137  while ( it < hex_invalid_0_F.end() )
138  {
139  std::cout << "Invalid input =>Input: " << *it << std::endl;
140  CPPUNIT_ASSERT( te::core::GetDecimalFromHexNotCS(*it) == dec_invalid);
141  it++ ;
142  }
143 }
144 
146 {
147  // calling function over input data and checking the output
148  std::vector<unsigned char>::iterator it = hex_ucase.begin();
149  std::vector<size_t>::iterator it_output = dec_val.begin();
150  std::cout << std::endl;
151 
152  while ( it < hex_ucase.end() && it_output < dec_val.end() )
153  {
154  std::cout << "Input unsigned char: " << *it <<" GetDecimalFromHexUCase: " << te::core::GetDecimalFromHexUCase(*it) << std::endl;
155  std::cout << "Input unsigned char: " << *it <<" Output size_t: " << *it_output << std::endl;
156  unsigned char ret_value = te::core::GetDecimalFromHexUCase(*it);
157  CPPUNIT_ASSERT(ret_value == *it_output);
158  CPPUNIT_ASSERT( te::core::GetDecimalFromHexUCase(*it) == *it_output);
159  it++ ;
160  it_output++ ;
161  }
162 
163  // invalid input values should return always dec_invalid = 15
164  it= hex_lower.begin();
165  while ( it < hex_lower.end() )
166  {
167  std::cout << "Input Invlaid: " << *it <<" Output: " << te::core::GetDecimalFromHexUCase(*it) <<std::endl;
168  unsigned char ret_value = te::core::GetDecimalFromHexUCase(*it);
169  CPPUNIT_ASSERT(ret_value == 15);
170  CPPUNIT_ASSERT( te::core::GetDecimalFromHexUCase(*it) == dec_invalid);
171  it++ ;
172  }
173 }
174 
176 {
177  // calling function over input data and checking the output
178  std::vector<unsigned char>::iterator it = hex_lcase.begin();
179  std::vector<size_t>::iterator it_output = dec_val.begin();
180  std::cout << std::endl;
181 
182  while ( it < hex_lcase.end() && it_output < dec_val.end() )
183  {
184  std::cout << "std_::string =>Input: " << *it <<" Output: " << *it_output << std::endl;
185  std::cout << "Input: " << *it <<" Output: " << *it_output << std::endl;
186  CPPUNIT_ASSERT( te::core::GetDecimalFromHexLCase(*it) == *it_output);
187  it++ ;
188  it_output++ ;
189  }
190  // invalid input values
191  it= hex_upper.begin();
192  while ( it < hex_upper.end() )
193  {
194  std::cout << "Input Invalid: " << *it <<" Output: " << te::core::GetDecimalFromHexLCase(*it) <<std::endl;
195  unsigned char ret_value = te::core::GetDecimalFromHexLCase(*it);
196  CPPUNIT_ASSERT(ret_value == 15);
197  CPPUNIT_ASSERT( te::core::GetDecimalFromHexLCase(*it) == dec_invalid);
198  it++ ;
199  }
200 }
201 
203 {
204  // Reading from json file into ascii_tab property tree and converting to ascii_json vector
205  std::vector<std::pair<std::string, std::string> > ascii_json;
206  std::vector<std::pair<std::string, std::string> >::iterator itpair;
207  boost::property_tree::ptree ascii_tab;
208  boost::property_tree::read_json("../data/module_common/hexutils/ascii_tab.json", ascii_tab);
209  te::common::Convert(ascii_tab.get_child("ascii_t"), ascii_json);
210  std::cout << std::endl;
211 
212  for(itpair = ascii_json.begin(); itpair < ascii_json.end(); itpair++)
213  {
214  std::cout << "First: " << (*itpair).first << " Second: " << (*itpair).second << std::endl;
215  char *res = new char[2];
216  //char* rt = new char[2];
217  //rt = "41";
218 
219  char c1 = *(((*itpair).first).c_str());
220  unsigned char c = *(((*itpair).first).c_str());
221  te::core::Char2Hex(c1,res);
222 
223  std::cout << " Output res: " << res << std::endl;
224  //res[2] = '\0';
225  std::cout << " Input: " << ((*itpair).first).c_str() << " Output Char2Hex : " << res << std::endl;
226  const char* itout = ((*itpair).second).c_str();
227  CPPUNIT_ASSERT( strncmp(res,itout,2)== 0 );
228  delete[] res;
229  }
230 }
231 
233 {
234  //Initial tests ...
235  char hex1[]= "41"; //character 'A' in hex
236  char res = te::core::Hex2Char(hex1);
237 
238  std::cout << std::endl;
239  std::cout << " Input: " << hex1 << " Output res: " << res << std::endl;
240 
241  char hex2[] = "39"; //character '9' in hex
242  res = te::core::Hex2Char(hex2);
243  std::cout << " Input: " << hex2 << " Output res2: " << res << std::endl;
244 
245  char hex3[] = "61"; //character 'a' in hex
246  res = te::core::Hex2Char(hex3);
247  std::cout << " Input: " << hex3 << " Output res: " << res << std::endl;
248 
249  char hex4[] = "4a"; //character 'J' in hex "4a" retorna �j�
250  res = te::core::Hex2Char(hex4);
251  std::cout << " Input J " << hex4 << " Output res: " << res << std::endl;
252 
253  char hex5[] = "46"; //character 'F' in hex
254  res = te::core::Hex2Char(hex5);
255  std::cout << " Input: " << hex5 << " Output res: " << res << std::endl;
256 
257  char hexG[] = "47"; //character 'G' in hex
258  res = te::core::Hex2Char(hexG);
259  std::cout << " Input: " << hexG << " Output res: " << res << std::endl;
260 
261  char hexa[] = "61"; //character 'a' in hex
262  res = te::core::Hex2Char(hexa);
263  std::cout << " Input: " << hexa << " Output res: " << res << std::endl;
264 
265  char hexf[] = "66"; //character 'f' in hex
266  res = te::core::Hex2Char(hexf);
267  std::cout << " Input: " << hexf << " Output res: " << res << std::endl;
268 
269  char hexg[] = "67"; //character 'g' in hex
270  res = te::core::Hex2Char(hexg);
271  std::cout << " Input: " << hexg << " Output res: " << res << std::endl;
272 
273  char hexZ[] = "5A"; //character 'Z' in hex
274  res = te::core::Hex2Char(hexZ);
275  std::cout << " Input Z: " << hexZ << " Output res: " << res << std::endl;
276 
277  char hexZ_lc[] = "5a"; //character 'Z' in hex ("5a" retorna 'z' e nao maiusculo)
278  res = te::core::Hex2Char(hexZ_lc);
279  std::cout << " Input Z: " << hexZ_lc << " Output res: " << res << std::endl;
280 
281  char hexm[] = "6d"; //character 'm' in hex (it does not accept lower case - return wrong value
282  res = te::core::Hex2Char(hexm);
283  std::cout << " Input m " << hexm << " Output res: " << res << std::endl;
284 
285  char hex_xx[] = "7B"; //character '{' in hex
286  char res_xx = te::core::Hex2Char(hex_xx);
287  std::cout << " Input {: " << hex_xx << " Output res: " << res_xx << std::endl;
288  std::cout << std::endl;
289 
290  // Block of final test - reading from json
291  // Reading from json file into ascii_tab property tree and converting to ascii_json vector
292  std::vector<std::pair<std::string, std::string> > ascii_json;
293  std::vector<std::pair<std::string, std::string> >::iterator itpair;
294  boost::property_tree::ptree ascii_tab;
295  boost::property_tree::read_json("../data/module_common/hexutils/ascii_tab.json", ascii_tab);
296 
297  te::common::Convert(ascii_tab.get_child("ascii_t"), ascii_json);
298  std::cout << std::endl;
299 
300  for(itpair = ascii_json.begin(); itpair < ascii_json.end(); itpair++)
301  {
302  std::cout << "First: " << (*itpair).first << " Second: " <<(*itpair).second << std::endl;
303  const char* h = ((*itpair).second).c_str();
304  char res_ascii = te::core::Hex2Char(((*itpair).second).c_str());
305  const char cres_ascci = res_ascii;
306  const char* itout = ((*itpair).first).c_str();
307  char c_itout = *(((*itpair).first).c_str());
308  std::cout << "First: " << (*itpair).first << " Second: " <<(*itpair).second << " Result: " << res_ascii << std::endl;
309 
310  CPPUNIT_ASSERT_MESSAGE("Upper case sensitive!",res_ascii == c_itout);
311  CPPUNIT_ASSERT(te::core::Hex2Char(((*itpair).second).c_str()) == *(((*itpair).first).c_str()) );
312  }
313 }
314 
316 {
317  // Initial tests
318  char hex1[]= "41"; //character 'A' in hex
319  char res1 = te::core::Hex2Char2(hex1);
320  std::cout << " Input: " << hex1 << " Output tcHex2Char2 res1: " << res1 << std::endl;
321 
322  char hex2[] = "39"; //character '9' in hex
323  char res2 = te::core::Hex2Char2(hex2);
324  std::cout << " Input: " << hex2 << " Output tcHex2Char2 res2: " << res2 << std::endl;
325 
326  char hex3[] = "61"; //character 'a' in hex
327  res2 = te::core::Hex2Char2(hex3);
328  std::cout << " Input: " << hex3 << " Output tcHex2Char2 res2: " << res2 << std::endl;
329 
330  char hex4[] = "4a"; //character 'J' in hex "4a" retorna �j�
331  res2 = te::core::Hex2Char2(hex4);
332  std::cout << " Input: " << hex4 << " Output tcHex2Char2 res2: " << res2 << std::endl;
333 
334  char hex5[] = "46"; //character 'F' in hex
335  res2 = te::core::Hex2Char2(hex5);
336  std::cout << " Input: " << hex5 << " Output tcHex2Char2 res2: " << res2 << std::endl;
337 
338  char hexG[] = "47"; //character 'G' in hex
339  res2 = te::core::Hex2Char2(hexG);
340  std::cout << " Input: " << hexG << " Output tcHex2Char2 res2: " << res2 << std::endl;
341 
342  char hexa[] = "61"; //character 'a' in hex
343  res2 = te::core::Hex2Char2(hexa);
344  std::cout << " Input: " << hexa << " Output tcHex2Char2 res2: " << res2 << std::endl;
345 
346  char hexf[] = "66"; //character 'f' in hex
347  res2 = te::core::Hex2Char2(hexf);
348  std::cout << " Input: " << hexf << " Output tcHex2Char2 res2: " << res2 << std::endl;
349 
350  char hexg[] = "67"; //character 'g' in hex
351  res2 = te::core::Hex2Char2(hexg);
352  std::cout << " Input: " << hexg << " Output tcHex2Char2 res2: " << res2 << std::endl;
353 
354  char hexZ[] = "5a"; //character 'Z' in hex ("5a" retorna 'z' e ao maiusculo)
355  res2 = te::core::Hex2Char2(hexZ);
356  std::cout << " Input 5a: " << hexZ << " Output tcHex2Char2 res2: " << res2 << std::endl;
357 
358  char hexZ1[] = "5A"; //character 'Z' in hex (Hex2Char("5a") retorna 'z' )
359  res2 = te::core::Hex2Char2(hexZ1);
360  std::cout << " Input 5A: " << hexZ1 << " Output tcHex2Char2 res2: " << res2 << std::endl;
361 
362  char hexm[] = "6d"; //character 'm' in hex (it does not accept lower case - return wrong value
363  res2 = te::core::Hex2Char2(hexm);
364  std::cout << " Input: " << hexm << " Output tcHex2Char2 res2: " << res2 << std::endl;
365 
366  char hex_xx[] = "7b"; //character '{' in hex
367  char res_xx = te::core::Hex2Char2(hex_xx);
368  std::cout << " Input {: " << hex_xx << " Output tcHex2Char2 res2: " << res_xx << std::endl;
369 
370  // Final tests - reading from json
371  // Reading from json file into ascii_tab property tree and converting to ascii_json vector
372  std::vector<std::pair<std::string, std::string> > ascii_json;
373  std::vector<std::pair<std::string, std::string> >::iterator itpair;
374  boost::property_tree::ptree ascii_tab;
375  boost::property_tree::read_json("../data/module_common/hexutils/ascii_tab.json", ascii_tab);
376  te::common::Convert(ascii_tab.get_child("ascii_t"), ascii_json);
377 
378  for(itpair = ascii_json.begin(); itpair < ascii_json.end(); itpair++)
379  {
380  std::cout << " First: " << (*itpair).first << " Second: " <<(*itpair).second << std::endl;
381  const char* h = ((*itpair).second).c_str();
382  char res_ascii = te::core::Hex2Char2(((*itpair).second).c_str());
383  const char cres_ascci = res_ascii;
384  const char* itout = ((*itpair).first).c_str();
385  char c_itout = *(((*itpair).first).c_str());
386  CPPUNIT_ASSERT(res_ascii == c_itout);
387  CPPUNIT_ASSERT(te::core::Hex2Char2(((*itpair).second).c_str()) == *(((*itpair).first).c_str()) );
388  }
389 }
390 
392 {
393  std::string hex_pairs = "202122232425262728292A2B2C2D2E2F303132333435363738393A3B3C3D3E3F404142434445464748494A4B4C4D4E4F505152535455565758595A5B5C5D5E5F606162636465666768696A6B6C6D6E6F707172737475767778797A7B7C7D7E" ;
394  std::string char_expect = " !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~" ; //expected ascci (Dec 32 to 126)
395 
396  const char* hex_pair = hex_pairs.c_str();
397  const char* expected_res = char_expect.c_str();
398 
399  char* result = te::core::Hex2Binary(hex_pair);
400  //result[len] = '\0';
401  std::cout << " hex_pair: " << hex_pair << " Output: " << result << " Expected Output: " << expected_res << std::endl;
402 
403  CPPUNIT_ASSERT_MESSAGE("Lower case letter (a-f) in hex representaton return wrong value",strncmp(result,expected_res,strlen(expected_res)) == 0);
404  delete[] result;
405 }
406 
408 {
409  std::string hex_pairs = "202122232425262728292A2B2C2D2E2F303132333435363738393A3B3C3D3E3F404142434445464748494A4B4C4D4E4F505152535455565758595A5B5C5D5E5F606162636465666768696A6B6C6D6E6F707172737475767778797A7B7C7D7E" ;
410  std::string char_expect = " !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~" ; //expected ascci (Dec 32 to 126)
411 
412  std::string char_simb = "202122232425262728292A2B2C2D2E2F";
413  std::string char_simb_expect = " !\"#$%&'()*+,-./"; //From Dec 32 - 47
414 
415  const char* hex_pair = hex_pairs.c_str();
416  const char* expected_res = char_expect.c_str();
417  size_t ss = strlen(hex_pair)/2;
418 
419  char* result = te::core::Hex2Binary(hex_pair);
420  //result[ss] = '\0';
421  std::cout << " hex_pair: " << hex_pair << " Output: " << result << std::endl;
422  std::cout << " Expected Output: " << expected_res << std::endl;
423 
424  CPPUNIT_ASSERT_MESSAGE("Only Upper case letter (A-F) in hex input",strncmp(result,expected_res,ss ) == 0);
425 
426  //Using strncmp with strlen instead of adding the null '\0' at the end of result
427  char* result_simbol = te::core::Hex2Binary(char_simb.c_str());
428  CPPUNIT_ASSERT(strncmp(result_simbol,char_simb_expect.c_str(),strlen(char_simb_expect.c_str())) == 0);
429  delete[] result;
430 }
431 
433 {
434  std::string hex_pairs = "202122232425262728292A2B2C2D2E2F303132333435363738393A3B3C3D3E3F404142434445464748494A4B4C4D4E4F505152535455565758595A5B5C5D5E5F606162636465666768696A6B6C6D6E6F707172737475767778797A7B7C7D7E" ;
435  std::string char_expect = " !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~" ; //expected ascci (Dec 32 to 126)
436  const char* hex_pair = hex_pairs.c_str();
437  char *result = new char[strlen(hex_pairs.c_str())/2+1];
438  const char* expected_res = char_expect.c_str();
439 
440  te::core::Hex2Binary(hex_pair,strlen(hex_pair),result );
441  result[strlen(hex_pair)/2] = '\0' ;
442  std::cout << " Input hex pairs: " << hex_pair << std::endl;
443  std::cout << " Output result : " << result << std::endl;
444  std::cout << " Expected result : " << expected_res << std::endl;
445 
446  CPPUNIT_ASSERT(strncmp(result,expected_res,strlen(hex_pair)/2) == 0);
447  delete[] result;
448 }
449 
451 {
452  std::string hex_pairs = "202122232425262728292A2B2C2D2E2F303132333435363738393A3B3C3D3E3F404142434445464748494A4B4C4D4E4F505152535455565758595A5B5C5D5E5F606162636465666768696A6B6C6D6E6F707172737475767778797A7B7C7D7E" ;
453  std::string char_expect = " !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~" ; //expected ascci (Dec 32 to 126)
454  const char* hex_pair = hex_pairs.c_str();
455  const char* expected_res = char_expect.c_str();
456 
457  char* result = te::core::Hex2Binary2(hex_pair);
458 
459  std::cout << " Input hex pairs: " << hex_pair << std::endl;
460  std::cout << " Output result : " << result << std::endl;
461  std::cout << " Expected result : " << expected_res << std::endl;
462 
463  CPPUNIT_ASSERT(strncmp(result,expected_res,strlen(hex_pair)/2) == 0);
464  delete[] result;
465 }
466 
468 {
469  std::string hex_pairs = "202122232425262728292A2B2C2D2E2F303132333435363738393A3B3C3D3E3F404142434445464748494A4B4C4D4E4F505152535455565758595A5B5C5D5E5F606162636465666768696A6B6C6D6E6F707172737475767778797A7B7C7D7E" ;
470  std::string char_expect = " !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~" ; //expected ascci (Dec 32 to 126)
471  const char* hex_pair = hex_pairs.c_str();
472  size_t ss = strlen(hex_pair)/2 ;
473  char *result = new char[ss+1];
474  const char* expected_res = char_expect.c_str();
475 
476  te::core::Hex2Binary2(hex_pair,strlen(hex_pair),result );
477  result[ss] = '\0';
478 
479  std::cout << " Input hex pairs: " << hex_pair << std::endl;
480  std::cout << " Output result : " << result << std::endl;
481  std::cout << " Expected result : " << expected_res << std::endl;
482 
483  CPPUNIT_ASSERT(strncmp(result,expected_res,ss) == 0);
484  delete[] result;
485 }
486 
488 {
489  std::string expected_hex = "202122232425262728292A2B2C2D2E2F303132333435363738393A3B3C3D3E3F404142434445464748494A4B4C4D4E4F505152535455565758595A5B5C5D5E5F606162636465666768696A6B6C6D6E6F707172737475767778797A7B7C7D7E" ;
490  std::string input_values = " !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~" ; //expected ascci (Dec 32 to 126)
491  const char* expected_hex_result = expected_hex.c_str();
492  const char* inp_values = input_values.c_str();
493 
494  char* result = te::core::Binary2Hex(inp_values,strlen(inp_values));
495 
496  std::cout << " Input inp_values: " << inp_values << std::endl;
497  std::cout << " Output result : " << result << std::endl;
498  std::cout << " Expected char : " << expected_hex_result << std::endl;
499 
500  CPPUNIT_ASSERT(strcmp(result,expected_hex_result) == 0);
501  delete[] result;
502 }
unsigned char GetDecimalFromHexNotCS(unsigned char hexValue)
It returns the decimal value in a char from a given hex value (for example: &#39;A&#39; => 10...
Definition: HexUtils.h:49
static std::vector< unsigned char > hex_ucase
Definition: TsHexUtils.cpp:43
void tcHex2Binary_3()
Test Case: It Converts Hex to Binary.
Definition: TsHexUtils.cpp:432
void tcGetDecimalFromHexLCase()
Test Case: Get Decimal from Hex (Lower case).
Definition: TsHexUtils.cpp:175
static std::vector< unsigned char > hex_upper
Definition: TsHexUtils.cpp:45
void setUp()
Definition: TsHexUtils.cpp:48
char * Hex2Binary(const char *hex)
It converts each pair of hex characters from a NULL-terminated string of hex characters into a binary...
Definition: HexUtils.h:469
size_t dec_invalid
Definition: TsHexUtils.cpp:40
void tcHex2Binary_2()
Test Case: It Converts Hex to Binary.
Definition: TsHexUtils.cpp:407
CPPUNIT_TEST_SUITE_REGISTRATION(TsHexUtils)
void tcGetDecimalFromHexUCase()
Test Case: Get Decimal from Hex (Upper case).
Definition: TsHexUtils.cpp:145
TECOMMONEXPORT void Convert(const boost::property_tree::ptree &p, std::map< std::string, std::string > &dict)
Converts a property tree node into a std::map<std::string, std::string>.
Definition: BoostUtils.cpp:36
void tcBinary2Hex2_1()
Test Case: It Converts Binary to Hex.
Definition: TsHexUtils.cpp:467
Test suite for hex utils.
Definition: TsHexUtils.h:37
void tcGetDecimalFromHexNotCS()
Test Case: Get Decimal from Hex (no case sensitive).
Definition: TsHexUtils.cpp:94
static std::vector< size_t > dec_val
Definition: TsHexUtils.cpp:38
static std::vector< unsigned char > hex_lower
Definition: TsHexUtils.cpp:46
void tcBinary2Hex_size()
Test Case: It Converts Binary to Hex.
Definition: TsHexUtils.cpp:487
void tearDown()
Definition: TsHexUtils.cpp:83
void tcHex2Char2()
Test Case: It Converts Hex to Char.
Definition: TsHexUtils.cpp:315
This file contains several utilities functions for dealing with HEX strings.
unsigned char dec_invalid_uc
Definition: TsHexUtils.cpp:41
static std::vector< unsigned char > dec_val_uchar
Definition: TsHexUtils.cpp:39
void tcHex2Binary_1()
Test Case: It Converts Hex to Binary.
Definition: TsHexUtils.cpp:391
char Hex2Char(const char *hex)
It converts the character from a hex representation to a byte.
Definition: HexUtils.h:270
void tcChar2Hex()
Test Case: It Converts char to Hex.
Definition: TsHexUtils.cpp:202
char * Hex2Binary2(const char *hex)
It converts each pair of hex characters from a NULL-terminated string of hex characters into a binary...
Definition: HexUtils.h:557
void tcHex2Char()
Test Case: It Converts Hex to Char.
Definition: TsHexUtils.cpp:232
This file contains include headers for the TerraLib Common Runtime module.
unsigned char Hex2Char2(const char *hex)
It converts the character from a hex representation to a byte.
Definition: HexUtils.h:301
void Char2Hex(unsigned char c, char *r)
It converts the character to a hex representation.
Definition: HexUtils.h:248
unsigned char GetDecimalFromHexLCase(char hexValue)
It returns the decimal value in a char from a given hex value (for example: &#39;a&#39; => 10...
Definition: HexUtils.h:185
unsigned char GetDecimalFromHexUCase(char hexValue)
It returns the decimal value in a char from a given hex value (for example: &#39;A&#39; => 10...
Definition: HexUtils.h:119
Test suite for Hex utils.
static std::vector< unsigned char > hex_invalid_0_F
Definition: TsHexUtils.cpp:44
char * Binary2Hex(const char *s, std::size_t size)
Each char from the array of characters in binary format is converted into a pair of hex characters...
Definition: HexUtils.h:602
static std::vector< unsigned char > hex_lcase
Definition: TsHexUtils.cpp:42
void tcHex2Binary2()
Test Case: It Converts Hex to Binary.
Definition: TsHexUtils.cpp:450