+ Post New Thread
Results 1 to 3 of 3
  1. #1
    Member level 1
    Points: 378, Level: 4

    Join Date
    Jul 2017
    Posts
    33
    Helped
    0 / 0
    Points
    378
    Level
    4

    itoa Function in C : How to read all DestinationBuffer values (int) into String Forma

    For hardware implementation, I am writing this function to read the data in Hercules. In Hercules, integer data can be read in string format. So, i have tried itoa conversion in XSDK. I am running counter module for e.g. N times. That N number of data should be converted into String from int and should read in Hercules.

    I have tried with below code but single data i am able to read. How to put loop for N times for DestinationBuffer.

    Code C - [expand]
    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
    44
    45
    46
    47
    48
    
    char buf[32] = {0};
    extern u32 DestinationBuffer[10];              // Global
    int transfer_data() {
        return 0;
    }
    char *itoa(int val, int base)
    {
     int i = 30;
    for(; val && i ; --i, val /= base)
    buf[i] = "0123456789abcdef"[val % base];
    return &buf[i+1];
    }
    err_t recv_callback(void *arg, struct tcp_pcb *tpcb,
                                   struct pbuf *p, err_t err)
    {
        int i,j,Status;
        Status=aurora_rx_main();         ///FUNCTION CALL
     
        for(i=0;i<10;i++)
         {
            xil_printf(" Data:%d,",DestinationBuffer[i]);
        }
     
        int base=10; // here 10 means decimal
      char *result={0};
     
      result= itoa(DestinationBuffer[0],base);    //// How to make it into loop to read all the DestinationBuffer values into String in Hercules. Now, i am able to read only 0th position values
     
        if (!p) {
            tcp_close(tpcb);
            tcp_recv(tpcb, NULL);
            return ERR_OK;
        }
     
        /* indicate that the packet has been received */
        tcp_recved(tpcb, p->len);
     
        if (tcp_sndbuf(tpcb) > 10) {
            err = tcp_write(tpcb,result,10,1);
     
        } else
            xil_printf("no space in tcp_sndbuf\n\r");
     
        /* free the received pbuf */
        pbuf_free(p);
     
        return ERR_OK;
    }

    Data stored in DestinationBuffer should be converted into string using itoa function.

    •   AltAdvertisment

        
       

  2. #2
    Super Moderator
    Points: 27,801, Level: 40
    andre_teprom's Avatar
    Join Date
    Nov 2006
    Location
    Brazil
    Posts
    8,299
    Helped
    1053 / 1053
    Points
    27,801
    Level
    40
    Blog Entries
    5

    Re: itoa Function in C : How to read all DestinationBuffer values (int) into String F

    You have defined the return type as err_t which is likely a numeric value. Instead, you should return not only the address of the first character of the string, but also its length.
    --------------------------------------------------------------------------------------------------
    Part of the world that you live in, You are the part that you're giving ( Renaissance )



    •   AltAdvertisment

        
       

  3. #3
    Advanced Member level 3
    Points: 6,671, Level: 19

    Join Date
    Jan 2015
    Posts
    965
    Helped
    306 / 306
    Points
    6,671
    Level
    19

    Re: itoa Function in C : How to read all DestinationBuffer values (int) into String F

    I think your 'itoa' function is wrong in that it will:
    - write the characters in the reverse order into the 'buf' array
    - not null terminate the string
    - pass back a pointer to the last character of the string, not the first
    Try using pencil and paper with 'val'of (say) 23 and 'base' of 10 - alternatively just write a small program to let you step through your code to see what is really is doing.
    As for your question, you have a 'for' loop just above where you step 'i' from 0 to 9 - why not use similar code to print out each value or put the code to print each value into that loop (perhaps no need for an array in that case).
    I recommend that you look at some of the many code examples on the Internet for the 'itoa' function - why reinvent the wheel.
    Susan



--[[ ]]--