Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

The final base conversion program must only use the POSIX system call write() (and possibly also _exit()). You cannot use any C library functions. The

The final base conversion program must only use the POSIX system call write() (and possibly also _exit()). You cannot use any C library functions. The only #include of standard library that you can do is #include , ans so your file should be like this:

#include  /* No other #include allowed! */ int strtonum(int base, char *bufsrc, unsigned int *ndst); int numtostr(int base, unsigned int *nsrc, char *bufdst); /* Any globals or statics should go here, but be careful */ int main(int argc, char *argv[]) { /* Write your code here */ } Here are the functions linked to this I just need the above function /*numtostr*/ int numtostr(int base, unsigned int *nsrc, char *bufdst) { if (base < 2 || base > 16) return -1; char digit[] = "0123456789ABCDEF"; unsigned int n = *nsrc; int i = 0; do { bufdst[i++] = digit[n % base]; n /= base; } while (n); bufdst[i] = '\0'; int start = 0, end = i - 1; while (start < end) { char tmp = bufdst[start]; bufdst[start++] = bufdst[end]; bufdst[end--] = tmp; } return i - (bufdst[0] == '0' && i > 1); } 

/*strtonum*/

int strtonum(int base, char *bufsrc, unsigned int *ndst) { unsigned int result = 0; unsigned int digit; char *p = bufsrc; while (*p) { if (*p >= '0' && *p <= '9') digit = *p - '0'; else if (*p >= 'a' && *p <= 'f') digit = *p - 'a' + 10; else if (*p >= 'A' && *p <= 'F') digit = *p - 'A' + 10; else return -1; // Invalid char if (digit >= base) return -1; // Invalid char result = result * base + digit; if (result < digit) return -2; // Overflow p++; } *ndst = result; return 0; }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Logics For Databases And Information Systems

Authors: Jan Chomicki ,Gunter Saake

1st Edition

1461375827, 978-1461375821

More Books

Students also viewed these Databases questions