Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <unistd.h>
- #include <stdlib.h>
- #include <cstring>
- #include <cstdio>
- #include <cstdlib>
- #include <stdint.h>
- // c++
- #include <iostream>
- #include <string>
- #include </usr/include/linux/kernel.h>
- using namespace std;
- static void __reverse (char* start, char* end)
- {
- char tmp;
- while (end > start)
- {
- tmp = *end; /* save end of the string int tmp variable */
- *end-- = *start; /* write to the end of the string its beginning */
- *start++ = tmp; /* and write to the beginning stuff from tmp */
- }
- }
- char* itoa (int value, char* buffer, int base, int * length)
- {
- static const char digits [] = "0123456789ABCDEF";
- char* tmpBuffer = buffer;
- int sign = 0;
- int quot, rem;
- *length = 3;
- if ( (base >= 2) && (base <= 16) ) /* check if the base is valid */
- {
- if (base == 10 && (sign = value) <0) /* check base & sign of value in the tmp copy */
- {
- value = -value;
- }
- do /* calculate quotient & reminder */
- {
- quot = value / base;
- rem = value % base;
- *buffer ++ = digits[rem]; /* append the reminder to the string */
- *length += 1;
- } while ((value = quot));
- *length -= 3;
- if (sign < 0) /* if negative value add a sign */
- {
- *buffer++ = '-';
- }
- __reverse(tmpBuffer, buffer - 1); /* reverse the string */
- }
- *buffer = '\0';
- return tmpBuffer;
- }
- /*==============================================================================
- Static function definitions
- ==============================================================================*/
- /*------------------------------------------------------------------------------
- function name: __reverse
- description: local function reversing order of a string
- parameters: pointer to the beginning of a string, pointer to the end of a string
- returned value: none
- ------------------------------------------------------------------------------*/
- int main(int argc, char **argv)
- {
- int i;
- uint8_t array[] = {0, 1, 6};
- unsigned int length = 3;
- uint8_t ret=0;
- char str[64]; // TODO use malloc over length here !!
- char c;
- // char ** str = (char **) malloc(length * sizeof(char *));
- unsigned int iter=0;
- for (iter=0; iter<length;iter++)
- {
- int sublength;
- itoa(array[iter],&c,10,&sublength);
- str[iter]=c;
- printf("itoa of %d = %d ; str[%d] now is %d \n\r",iter,c,iter,str[iter]);
- }
- //Now the first length character of str are the number (e.g if array= {4,5,6} then str should be {'4','5','6','whatever','whateve',...}
- for(i=0;i<length;i++)
- {
- printf("buffer[%d]= %d\n\r",i,str[i]);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement