Advertisement
Guest User

Untitled

a guest
Nov 5th, 2017
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.61 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <stdlib.h>
  4. #include <cstring>
  5. #include <cstdio>
  6. #include <cstdlib>
  7. #include <stdint.h>
  8. // c++
  9. #include <iostream>
  10. #include <string>
  11. #include </usr/include/linux/kernel.h>
  12. using namespace std;
  13. static void __reverse (char* start, char* end)
  14. {
  15.     char tmp;
  16.  
  17.     while (end > start)
  18.     {
  19.         tmp = *end;                     /* save end of the string int tmp variable */
  20.         *end-- = *start;                /* write to the end of the string its beginning */
  21.         *start++ = tmp;             /* and write to the beginning stuff from tmp */
  22.     }
  23. }
  24.  
  25.  
  26.  
  27. char* itoa (int value, char* buffer, int base, int * length)
  28. {
  29.     static const char digits [] = "0123456789ABCDEF";
  30.     char* tmpBuffer = buffer;
  31.     int sign = 0;
  32.     int quot, rem;
  33.     *length = 3;
  34.     if ( (base >= 2) && (base <= 16) )                  /* check if the base is valid */
  35.     {
  36.         if (base == 10 && (sign = value) <0)        /* check base & sign of value in the tmp copy */
  37.         {
  38.             value = -value;
  39.         }
  40.  
  41.         do                                      /* calculate quotient & reminder */
  42.         {
  43.             quot = value / base;
  44.             rem = value % base;
  45.             *buffer ++ = digits[rem];               /* append the reminder to the string */
  46.             *length += 1;
  47.         } while ((value = quot));
  48.         *length -= 3;
  49.         if (sign < 0)                           /* if negative value add a sign */
  50.         {
  51.             *buffer++ = '-';
  52.         }
  53.  
  54.         __reverse(tmpBuffer, buffer - 1);       /* reverse the string */
  55.        
  56.     }
  57.  
  58.     *buffer = '\0';
  59.     return tmpBuffer;
  60. }
  61.  
  62. /*==============================================================================
  63.  Static function definitions
  64. ==============================================================================*/
  65. /*------------------------------------------------------------------------------
  66.  function name: __reverse
  67.  description:       local function reversing order of a string
  68.  parameters:        pointer to the beginning of a string, pointer to the end of a string
  69.  returned value:    none
  70. ------------------------------------------------------------------------------*/
  71.  
  72.  
  73.  
  74. int main(int argc, char **argv)
  75. {
  76.     int i;
  77.     uint8_t array[] = {0, 1, 6};
  78.     unsigned int length = 3;
  79.     uint8_t ret=0;
  80.     char str[64]; // TODO  use malloc over length here !!
  81.     char c;
  82.     //  char ** str = (char **) malloc(length * sizeof(char *));
  83.     unsigned int iter=0;
  84.     for (iter=0; iter<length;iter++)
  85.     {
  86.         int sublength;
  87.         itoa(array[iter],&c,10,&sublength);
  88.         str[iter]=c;      
  89.         printf("itoa of %d = %d ; str[%d] now is %d \n\r",iter,c,iter,str[iter]);
  90.     }
  91.     //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',...}
  92.     for(i=0;i<length;i++)
  93.     {
  94.         printf("buffer[%d]= %d\n\r",i,str[i]);
  95.     }
  96.     return 0;
  97.  
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement