add methods for converting from hex string to bytes
add to convert/text.h: template<typename HexStringPointerType, typename BytesStringType> bool hex_string_pointer_to_bytes(const HexStringPointerType * hex_string, BytesStringType & bytes, bool clear_bytes = true); template<typename HexStringType, typename BytesStringType> bool hex_string_to_bytes(const HexStringType & hex_string, BytesStringType & bytes, bool clear_bytes = true);
This commit is contained in:
parent
a524dfa2a7
commit
d13c10c604
|
@ -5,7 +5,7 @@
|
|||
*/
|
||||
|
||||
/*
|
||||
* Copyright (c) 2017-2021, Tomasz Sowa
|
||||
* Copyright (c) 2017-2022, Tomasz Sowa
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
|
@ -44,6 +44,49 @@ namespace pt
|
|||
bool is_white(wchar_t c, bool check_additional_chars = true, bool treat_new_line_as_white = true);
|
||||
bool is_digit(wchar_t c, int base = 10, int * digit = 0);
|
||||
|
||||
|
||||
// IMPROVEME add tests
|
||||
template<typename HexStringPointerType, typename BytesStringType>
|
||||
bool hex_string_pointer_to_bytes(const HexStringPointerType * hex_string, BytesStringType & bytes, bool clear_bytes = true)
|
||||
{
|
||||
bool converted_correctly = true;
|
||||
size_t i = 0;
|
||||
|
||||
if( clear_bytes )
|
||||
bytes.clear();
|
||||
|
||||
for( ; converted_correctly && hex_string[i] != 0 && hex_string[i+1] != 0 ; i += 2)
|
||||
{
|
||||
int c1, c2;
|
||||
|
||||
converted_correctly = converted_correctly && is_digit(hex_string[i], 16, &c1);
|
||||
converted_correctly = converted_correctly && is_digit(hex_string[i+1], 16, &c2);
|
||||
|
||||
if( converted_correctly )
|
||||
{
|
||||
bytes += (char)(unsigned char)(((c1 << 4) | c2));
|
||||
}
|
||||
}
|
||||
|
||||
if( hex_string[i] != 0 )
|
||||
{
|
||||
// one digit has left
|
||||
converted_correctly = false;
|
||||
}
|
||||
|
||||
return converted_correctly;
|
||||
}
|
||||
|
||||
|
||||
// IMPROVEME add tests
|
||||
template<typename HexStringType, typename BytesStringType>
|
||||
bool hex_string_to_bytes(const HexStringType & hex_string, BytesStringType & bytes, bool clear_bytes = true)
|
||||
{
|
||||
return hex_string_pointer_to_bytes(hex_string.c_str(), bytes, clear_bytes);
|
||||
}
|
||||
|
||||
|
||||
|
||||
const char * skip_white(const char * str, bool check_additional_chars = true, bool treat_new_line_as_white = true);
|
||||
const wchar_t * skip_white(const wchar_t * str, bool check_additional_chars = true, bool treat_new_line_as_white = true);
|
||||
|
||||
|
|
Loading…
Reference in New Issue