-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaddHexPrefix.c
More file actions
43 lines (36 loc) · 1.16 KB
/
addHexPrefix.c
File metadata and controls
43 lines (36 loc) · 1.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <stdlib.h>
#include<string.h>
#include "main.h"
/**
* add0xPrefixToHex - Adds "0x" prefix to a given hexadecimal string.
*
* @hexString: The input hexadecimal string to which the "0x" prefix will be added.
*
* Description:
* This function takes an input hexadecimal string and returns a new string with "0x" added as a
* prefix to the original string. The resulting string is null-terminated. The function allocates
* memory for the new string and ensures it can accommodate the original string, "0x" prefix, and
* a null terminator.
*
* Return: A dynamically allocated string containing the original hexadecimal string with the "0x"
* prefix added, or NULL if the input string is NULL or on memory allocation failure.
*/
char* add0xPrefixToHex(const char* hexString)
{
size_t inputLength;
size_t newLength;
char* result;
if (hexString == NULL) {
return (NULL);
}
inputLength = strlen(hexString);
newLength = inputLength + 2;
result = (char*)malloc(newLength + 1);
if (result == NULL)
{
return (NULL);
}
strcpy(result, "0x");
strcat(result, hexString);
return (result);
}