32 #include <boost/lexical_cast.hpp> 33 #include <boost/property_tree/json_parser.hpp> 34 #include <boost/foreach.hpp> 51 boost::property_tree::ptree test_data;
52 boost::property_tree::read_json(
"../data/module_common/hexutils/hexutils_inp_out.json", test_data);
60 dec_invalid =boost::lexical_cast<
size_t>(test_data.get_child(
"outp.dec_val_invalid").data());
102 char input1_hex =
'A';
103 char input2_hex =
'a';
104 char input3_hex =
'1';
105 char input4_hex =
'f';
106 char input5_hex =
'I';
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();
119 std::cout << std::endl;
123 std::cout <<
"Input unsigned char: " << *it <<
" Output size_t: " << *it_output <<
" Output uchar: " << *it_output_uc << std::endl;
127 CPPUNIT_ASSERT( res_uc == *it_output_uc);
139 std::cout <<
"Invalid input =>Input: " << *it << std::endl;
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;
155 std::cout <<
"Input unsigned char: " << *it <<
" Output size_t: " << *it_output << std::endl;
157 CPPUNIT_ASSERT(ret_value == *it_output);
169 CPPUNIT_ASSERT(ret_value == 15);
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;
184 std::cout <<
"std_::string =>Input: " << *it <<
" Output: " << *it_output << std::endl;
185 std::cout <<
"Input: " << *it <<
" Output: " << *it_output << std::endl;
196 CPPUNIT_ASSERT(ret_value == 15);
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);
210 std::cout << std::endl;
212 for(itpair = ascii_json.begin(); itpair < ascii_json.end(); itpair++)
214 std::cout <<
"First: " << (*itpair).first <<
" Second: " << (*itpair).second << std::endl;
215 char *res =
new char[2];
219 char c1 = *(((*itpair).first).c_str());
220 unsigned char c = *(((*itpair).first).c_str());
223 std::cout <<
" Output res: " << res << std::endl;
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 );
238 std::cout << std::endl;
239 std::cout <<
" Input: " << hex1 <<
" Output res: " << res << std::endl;
243 std::cout <<
" Input: " << hex2 <<
" Output res2: " << res << std::endl;
247 std::cout <<
" Input: " << hex3 <<
" Output res: " << res << std::endl;
251 std::cout <<
" Input J " << hex4 <<
" Output res: " << res << std::endl;
255 std::cout <<
" Input: " << hex5 <<
" Output res: " << res << std::endl;
259 std::cout <<
" Input: " << hexG <<
" Output res: " << res << std::endl;
263 std::cout <<
" Input: " << hexa <<
" Output res: " << res << std::endl;
267 std::cout <<
" Input: " << hexf <<
" Output res: " << res << std::endl;
271 std::cout <<
" Input: " << hexg <<
" Output res: " << res << std::endl;
275 std::cout <<
" Input Z: " << hexZ <<
" Output res: " << res << std::endl;
277 char hexZ_lc[] =
"5a";
279 std::cout <<
" Input Z: " << hexZ_lc <<
" Output res: " << res << std::endl;
283 std::cout <<
" Input m " << hexm <<
" Output res: " << res << std::endl;
285 char hex_xx[] =
"7B";
287 std::cout <<
" Input {: " << hex_xx <<
" Output res: " << res_xx << std::endl;
288 std::cout << std::endl;
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);
298 std::cout << std::endl;
300 for(itpair = ascii_json.begin(); itpair < ascii_json.end(); itpair++)
302 std::cout <<
"First: " << (*itpair).first <<
" Second: " <<(*itpair).second << std::endl;
303 const char* h = ((*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;
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()) );
320 std::cout <<
" Input: " << hex1 <<
" Output tcHex2Char2 res1: " << res1 << std::endl;
324 std::cout <<
" Input: " << hex2 <<
" Output tcHex2Char2 res2: " << res2 << std::endl;
328 std::cout <<
" Input: " << hex3 <<
" Output tcHex2Char2 res2: " << res2 << std::endl;
332 std::cout <<
" Input: " << hex4 <<
" Output tcHex2Char2 res2: " << res2 << std::endl;
336 std::cout <<
" Input: " << hex5 <<
" Output tcHex2Char2 res2: " << res2 << std::endl;
340 std::cout <<
" Input: " << hexG <<
" Output tcHex2Char2 res2: " << res2 << std::endl;
344 std::cout <<
" Input: " << hexa <<
" Output tcHex2Char2 res2: " << res2 << std::endl;
348 std::cout <<
" Input: " << hexf <<
" Output tcHex2Char2 res2: " << res2 << std::endl;
352 std::cout <<
" Input: " << hexg <<
" Output tcHex2Char2 res2: " << res2 << std::endl;
356 std::cout <<
" Input 5a: " << hexZ <<
" Output tcHex2Char2 res2: " << res2 << std::endl;
360 std::cout <<
" Input 5A: " << hexZ1 <<
" Output tcHex2Char2 res2: " << res2 << std::endl;
364 std::cout <<
" Input: " << hexm <<
" Output tcHex2Char2 res2: " << res2 << std::endl;
366 char hex_xx[] =
"7b";
368 std::cout <<
" Input {: " << hex_xx <<
" Output tcHex2Char2 res2: " << res_xx << std::endl;
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);
378 for(itpair = ascii_json.begin(); itpair < ascii_json.end(); itpair++)
380 std::cout <<
" First: " << (*itpair).first <<
" Second: " <<(*itpair).second << std::endl;
381 const char* h = ((*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()) );
393 std::string hex_pairs =
"202122232425262728292A2B2C2D2E2F303132333435363738393A3B3C3D3E3F404142434445464748494A4B4C4D4E4F505152535455565758595A5B5C5D5E5F606162636465666768696A6B6C6D6E6F707172737475767778797A7B7C7D7E" ;
394 std::string char_expect =
" !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~" ;
396 const char* hex_pair = hex_pairs.c_str();
397 const char* expected_res = char_expect.c_str();
401 std::cout <<
" hex_pair: " << hex_pair <<
" Output: " << result <<
" Expected Output: " << expected_res << std::endl;
403 CPPUNIT_ASSERT_MESSAGE(
"Lower case letter (a-f) in hex representaton return wrong value",strncmp(result,expected_res,strlen(expected_res)) == 0);
409 std::string hex_pairs =
"202122232425262728292A2B2C2D2E2F303132333435363738393A3B3C3D3E3F404142434445464748494A4B4C4D4E4F505152535455565758595A5B5C5D5E5F606162636465666768696A6B6C6D6E6F707172737475767778797A7B7C7D7E" ;
410 std::string char_expect =
" !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~" ;
412 std::string char_simb =
"202122232425262728292A2B2C2D2E2F";
413 std::string char_simb_expect =
" !\"#$%&'()*+,-./";
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;
421 std::cout <<
" hex_pair: " << hex_pair <<
" Output: " << result << std::endl;
422 std::cout <<
" Expected Output: " << expected_res << std::endl;
424 CPPUNIT_ASSERT_MESSAGE(
"Only Upper case letter (A-F) in hex input",strncmp(result,expected_res,ss ) == 0);
428 CPPUNIT_ASSERT(strncmp(result_simbol,char_simb_expect.c_str(),strlen(char_simb_expect.c_str())) == 0);
434 std::string hex_pairs =
"202122232425262728292A2B2C2D2E2F303132333435363738393A3B3C3D3E3F404142434445464748494A4B4C4D4E4F505152535455565758595A5B5C5D5E5F606162636465666768696A6B6C6D6E6F707172737475767778797A7B7C7D7E" ;
435 std::string char_expect =
" !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~" ;
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();
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;
446 CPPUNIT_ASSERT(strncmp(result,expected_res,strlen(hex_pair)/2) == 0);
452 std::string hex_pairs =
"202122232425262728292A2B2C2D2E2F303132333435363738393A3B3C3D3E3F404142434445464748494A4B4C4D4E4F505152535455565758595A5B5C5D5E5F606162636465666768696A6B6C6D6E6F707172737475767778797A7B7C7D7E" ;
453 std::string char_expect =
" !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~" ;
454 const char* hex_pair = hex_pairs.c_str();
455 const char* expected_res = char_expect.c_str();
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;
463 CPPUNIT_ASSERT(strncmp(result,expected_res,strlen(hex_pair)/2) == 0);
469 std::string hex_pairs =
"202122232425262728292A2B2C2D2E2F303132333435363738393A3B3C3D3E3F404142434445464748494A4B4C4D4E4F505152535455565758595A5B5C5D5E5F606162636465666768696A6B6C6D6E6F707172737475767778797A7B7C7D7E" ;
470 std::string char_expect =
" !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~" ;
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();
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;
483 CPPUNIT_ASSERT(strncmp(result,expected_res,ss) == 0);
489 std::string expected_hex =
"202122232425262728292A2B2C2D2E2F303132333435363738393A3B3C3D3E3F404142434445464748494A4B4C4D4E4F505152535455565758595A5B5C5D5E5F606162636465666768696A6B6C6D6E6F707172737475767778797A7B7C7D7E" ;
490 std::string input_values =
" !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~" ;
491 const char* expected_hex_result = expected_hex.c_str();
492 const char* inp_values = input_values.c_str();
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;
500 CPPUNIT_ASSERT(strcmp(result,expected_hex_result) == 0);
unsigned char GetDecimalFromHexNotCS(unsigned char hexValue)
It returns the decimal value in a char from a given hex value (for example: 'A' => 10...
static std::vector< unsigned char > hex_ucase
void tcHex2Binary_3()
Test Case: It Converts Hex to Binary.
void tcGetDecimalFromHexLCase()
Test Case: Get Decimal from Hex (Lower case).
static std::vector< unsigned char > hex_upper
char * Hex2Binary(const char *hex)
It converts each pair of hex characters from a NULL-terminated string of hex characters into a binary...
void tcHex2Binary_2()
Test Case: It Converts Hex to Binary.
CPPUNIT_TEST_SUITE_REGISTRATION(TsHexUtils)
void tcGetDecimalFromHexUCase()
Test Case: Get Decimal from Hex (Upper case).
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>.
void tcBinary2Hex2_1()
Test Case: It Converts Binary to Hex.
Test suite for hex utils.
void tcGetDecimalFromHexNotCS()
Test Case: Get Decimal from Hex (no case sensitive).
static std::vector< size_t > dec_val
static std::vector< unsigned char > hex_lower
void tcBinary2Hex_size()
Test Case: It Converts Binary to Hex.
void tcHex2Char2()
Test Case: It Converts Hex to Char.
This file contains several utilities functions for dealing with HEX strings.
unsigned char dec_invalid_uc
static std::vector< unsigned char > dec_val_uchar
void tcHex2Binary_1()
Test Case: It Converts Hex to Binary.
char Hex2Char(const char *hex)
It converts the character from a hex representation to a byte.
void tcChar2Hex()
Test Case: It Converts char to Hex.
char * Hex2Binary2(const char *hex)
It converts each pair of hex characters from a NULL-terminated string of hex characters into a binary...
void tcHex2Char()
Test Case: It Converts Hex to Char.
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.
void Char2Hex(unsigned char c, char *r)
It converts the character to a hex representation.
unsigned char GetDecimalFromHexLCase(char hexValue)
It returns the decimal value in a char from a given hex value (for example: 'a' => 10...
unsigned char GetDecimalFromHexUCase(char hexValue)
It returns the decimal value in a char from a given hex value (for example: 'A' => 10...
Test suite for Hex utils.
static std::vector< unsigned char > hex_invalid_0_F
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...
static std::vector< unsigned char > hex_lcase
void tcHex2Binary2()
Test Case: It Converts Hex to Binary.