From deb54b8866e119f601fde91cb44a6ddf44f1552e Mon Sep 17 00:00:00 2001 From: winlin Date: Mon, 13 Jul 2020 14:30:44 +0800 Subject: [PATCH] Core: Refine utility string/hex --- trunk/src/kernel/srs_kernel_utility.cpp | 16 ++++++++++++++++ trunk/src/kernel/srs_kernel_utility.hpp | 13 +++++++++---- 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/trunk/src/kernel/srs_kernel_utility.cpp b/trunk/src/kernel/srs_kernel_utility.cpp index 72e2d705e..73ae0c75f 100644 --- a/trunk/src/kernel/srs_kernel_utility.cpp +++ b/trunk/src/kernel/srs_kernel_utility.cpp @@ -1078,6 +1078,22 @@ char* srs_data_to_hex(char* des, const u_int8_t* src, int len) return des; } +char* srs_data_to_hex_lowercase(char* des, const u_int8_t* src, int len) +{ + if(src == NULL || len == 0 || des == NULL){ + return NULL; + } + + const char *hex_table = "0123456789abcdef"; + + for (int i=0; i> 4]; + des[i * 2 + 1] = hex_table[src[i] & 0x0F]; + } + + return des; +} + int srs_hex_to_data(uint8_t* data, const char* p, int size) { if (size <= 0 || (size%2) == 1) { diff --git a/trunk/src/kernel/srs_kernel_utility.hpp b/trunk/src/kernel/srs_kernel_utility.hpp index 8dc54894c..e6a2f3371 100644 --- a/trunk/src/kernel/srs_kernel_utility.hpp +++ b/trunk/src/kernel/srs_kernel_utility.hpp @@ -145,12 +145,17 @@ extern srs_error_t srs_av_base64_decode(std::string cipher, std::string& plainte // Calculate the output size needed to base64-encode x bytes to a null-terminated string. #define SRS_AV_BASE64_SIZE(x) (((x)+2) / 3 * 4 + 1) -// Convert hex string to data, for example, p=config='139056E5A0' -// The output data in hex {0x13, 0x90, 0x56, 0xe5, 0xa0} as such. +// Covert hex string to uint8 data, for example: +// srs_hex_to_data(data, string("139056E5A0")) +// which outputs the data in hex {0x13, 0x90, 0x56, 0xe5, 0xa0}. extern int srs_hex_to_data(uint8_t* data, const char* p, int size); -// Convert data string to hex. -extern char *srs_data_to_hex(char *des, const uint8_t *src, int len); +// Convert data string to hex, for example: +// srs_data_to_hex(des, {0xf3, 0x3f}, 2) +// which outputs the des is string("F33F"). +extern char* srs_data_to_hex(char* des, const uint8_t* src, int len); +// Output in lowercase, such as string("f33f"). +extern char* srs_data_to_hex_lowercase(char* des, const uint8_t* src, int len); // Generate the c0 chunk header for msg. // @param cache, the cache to write header.