diff --git a/DIRECTORY.md b/DIRECTORY.md index 1339bc6ffc..abed4d1536 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -34,6 +34,7 @@ * [Decimal To Octal Recursion](https://github.com/TheAlgorithms/C/blob/HEAD/conversions/decimal_to_octal_recursion.c) * [Hexadecimal To Octal](https://github.com/TheAlgorithms/C/blob/HEAD/conversions/hexadecimal_to_octal.c) * [Hexadecimal To Octal2](https://github.com/TheAlgorithms/C/blob/HEAD/conversions/hexadecimal_to_octal2.c) + * [Hexadecimal To String](https://github.com/TheAlgorithms/C/blob/HEAD/conversions/hexadecimal_to_string.c) * [Infix To Postfix](https://github.com/TheAlgorithms/C/blob/HEAD/conversions/infix_to_postfix.c) * [Infix To Postfix2](https://github.com/TheAlgorithms/C/blob/HEAD/conversions/infix_to_postfix2.c) * [Int To String](https://github.com/TheAlgorithms/C/blob/HEAD/conversions/int_to_string.c) @@ -41,6 +42,7 @@ * [Octal To Decimal](https://github.com/TheAlgorithms/C/blob/HEAD/conversions/octal_to_decimal.c) * [Octal To Hexadecimal](https://github.com/TheAlgorithms/C/blob/HEAD/conversions/octal_to_hexadecimal.c) * [Roman Numerals To Decimal](https://github.com/TheAlgorithms/C/blob/HEAD/conversions/roman_numerals_to_decimal.c) + * [String To Hexadecimal](https://github.com/TheAlgorithms/C/blob/HEAD/conversions/string_to_hexadecimal.c) * [To Decimal](https://github.com/TheAlgorithms/C/blob/HEAD/conversions/to_decimal.c) ## Data Structures diff --git a/conversions/hexadecimal_to_string.c b/conversions/hexadecimal_to_string.c new file mode 100644 index 0000000000..023351ad44 --- /dev/null +++ b/conversions/hexadecimal_to_string.c @@ -0,0 +1,39 @@ +#include +#include +#include +#include + +void hexadecimal_to_string(const char* in, char* out) +{ + uint8_t len = strlen(in); + for (int i = 0; i < len; i += 2) + { + char msb = (in[i] <= '9') ? in[i] - '0' + : (in[i] & 0x5F) - 'A' + 10; + char lsb = (in[i + 1] <= '9') ? in[i + 1] - '0' + : (in[i + 1] & 0x5F) - 'A' + 10; + out[i / 2] = (msb << 4) | lsb; + } + out[len / 2] = '\0'; +} + +void test() +{ + // given + const char* in = + "4c6f72656d20497073756d2069732073696d706c792064756d6d79207465787421"; + char out[100] = {0}; + + // when + hexadecimal_to_string(in, out); + + // then + // printf("%s\n", out); + assert(strcmp("Lorem Ipsum is simply dummy text!", out) == 0); +} + +int main() +{ + test(); + return 0; +} \ No newline at end of file diff --git a/conversions/string_to_hexadecimal.c b/conversions/string_to_hexadecimal.c new file mode 100644 index 0000000000..b0d9862906 --- /dev/null +++ b/conversions/string_to_hexadecimal.c @@ -0,0 +1,34 @@ +#include +#include +#include + +void string_to_hexadecimal(const char* in, char* out) +{ + int len = strlen(in); + for (int i = 0; i < len; ++i) + { + sprintf(out + (i * 2), "%02x", (unsigned char)in[i]); + } + out[len * 2] = '\0'; +} + +void test() +{ + // given + const char* in = "Lorem Ipsum is simply dummy text!"; + char out[100] = {0}; + + // when + string_to_hexadecimal(in, out); + + // then + // printf("%s\n", out); + assert(strcmp("4c6f72656d20497073756d2069732073696d706c792064756d6d79207465787421", + out) == 0); +} + +int main() +{ + test(); + return 0; +} \ No newline at end of file