can someone just explain everything this function does, I know it converts integ
ID: 3757151 • Letter: C
Question
can someone just explain everything this function does, I know it converts integers to ASCII but I'm not exactly sure how
76 return output; 77 h 78 80 // Input: This function converts integers to ASCII. 81 charsint_To_ASCII(int x, char soutputvalue, int base, int sign) f 82 if (sign-1){ 83 84 85 int i, n=0; 86 char*cstr= malloc (32); x=x*-1; char *output2 malloc (32); 89 90 91 output2t0]e while (x != 0) { (x cstr[0] % base) + '0'; if (x% base > 9) { 93 n-base- (x % base); 95 strcat(output2, cstr); x/base; 98 i=0; while (iExplanation / Answer
// Function returns the character pointer stores the ASCII form of the number
// Parameters:
// int x – The number which needs to be converted to ASCII
// char *outputvalue – which will store the ASCII result
// int base – base value of the number (x)
// int sign – sign value of the number (positive or negative)
char * int_To_ASCII(int x, char *outputvalue, int base, int sign)
{
// Checks if sign is 1 then negative number
if(sign == 1)
{
x = x * -1;
}// End of if condition
int i, n = 0;
// Dynamically allocate memory of size 32
char *cstr = malloc(32);
// Dynamically allocate memory of size 32
char *output2 = malloc(32);
// Assigns null terminated character at 0 index position
output2[0] = '';
// Loops till number is not equals to zero
while(x != 0)
{
// Calculates the remainder after dividing by base and stores it at 0 index position
cstr[0] = (x % base) + '0';
printf(" cstr[0] = %c", cstr[x]);
// Checks if remainder is greater than 9 (more than one digit)
if(x % base > 9)
{
// Subtracts the remainder after dividing by base of the number from base
n = base - (x % base);
// Resulted subtracted value is subtracted from character 'G'
// and stores it in 0 index position
// 'G' - to converts to character
cstr[0] = 'G' - n;
}// End of if condition
// Stores null character at 1 index position
cstr[1] = '';
// Concatenates the cstr data at the end of output2
strcat(output2, cstr);
// Calculates the quotient after dividing by base and stores it in x
x /= base;
}// End of while loop
// Re initializes the variable i for index to zero
i = 0;
// Loops till end of the output2 string
while(i < strlen(output2))
{
// Stores the data in reverse order
// outputvalue[i] - to store the data from first
// output2[strlen(output2) - i - 1] - to assign the data from last
outputvalue[i] = output2[strlen(output2) - i - 1];
// Increase the index by one
i++;
}// End of while loop
cstr = 0;
// Release the memory
free(cstr);
// Returns the result
return outputvalue;
}// End of the function
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.