Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Converting IPAddress into string

Status
Not open for further replies.

MrNobody

New Member
Hi, I am using TCP/IP Stack 4.18 with C18.
I need to convert the ip address into string in order to display it onto LCD.
Example is to convert "192.168.1.1" into {'1','9','2','1','6','8','0','0','1','0','0','1'}

Can anybody please advice how I can do that..?
Thanks..
 
Check out the library function itoa(int i) which converts integer i into a null terminated string.

To do it the hard way, use the integer division and modulus operators with decresing powers of 10

Example:
Code:
192 / 100 = 1, then 1 + '0' = 0x31 = '1'
192 % 100 = 92

92 / 10 = 9, then 9 + '0' = 0x39 = '9'
92 % 10 = 2, then 2 + '0' = 0x32 = '2'
 
There is also the following idea, which I found in MainDemo.c in the "TCPIP Demo App" directory installed with the Microchip TCP/IP Stack 4.18:

Code:
static void DisplayIPValue(IP_ADDR IPVal)
{
//  printf("%u.%u.%u.%u", IPVal.v[0], IPVal.v[1], IPVal.v[2], IPVal.v[3]);                                                                                                           
    BYTE IPDigit[4];
    BYTE i;
#ifdef USE_LCD
    BYTE j;
    BYTE LCDPos=16;
#endif

    for(i = 0; i < sizeof(IP_ADDR); i++)
    {
        uitoa((WORD)IPVal.v[i], IPDigit);

        #if defined(STACK_USE_UART)
            putsUART(IPDigit);
        #endif

        #ifdef USE_LCD
            for(j = 0; j < strlen((char*)IPDigit); j++)
            {
                LCDText[LCDPos++] = IPDigit[j];
            }
            if(i == sizeof(IP_ADDR)-1)
                break;
            LCDText[LCDPos++] = '.';
        #else
            if(i == sizeof(IP_ADDR)-1)
                break;
        #endif

        #if defined(STACK_USE_UART)
            while(BusyUART());
            WriteUART('.');
        #endif
    }

    #ifdef USE_LCD
        if(LCDPos < 32)
            LCDText[LCDPos] = 0;
        LCDUpdate();
    #endif
}

I haven't run or even compiled this code but it looks like it should do what you want. (I'm assuming your question meant to ask how to convert an IP address of type IPAddr to string though--is that right?) If you don't need the whole function there might be something in there you can use, at any rate.


Torben
 
Yes.. convert from IP_ADDR to string..
Actually, mine is from the NODE_INFO, but the IP_ADDR value can be extracted from there..
Thanks..

I wonder, what does the statement "printf("%u.%u.%u.%u", IPVal.v[0], IPVal.v[1], IPVal.v[2], IPVal.v[3]);" used for..? is it to print to LCD..?
 
Last edited:
Yes.. convert from IP_ADDR to string..
Actually, mine is from the NODE_INFO, but the IP_ADDR value can be extracted from there..
Thanks..

I wonder, what does the statement "printf("%u.%u.%u.%u", IPVal.v[0], IPVal.v[1], IPVal.v[2], IPVal.v[3]);" used for..? is it to print to LCD..?

I don't know; it's commented out in the original code (in other words, I just copied and pasted the code; I didn't comment that line out). However, in the uC code I've written, that would end up going to the serial port (which I tend to use as a debug console if it's not needed for something else). I doubt very much it would be to print to the LCD; if it were, then what is the point of the rest of the function? ;)


Torben
 
Last edited:
I don't know; it's commented out in the original code (in other words, I just copied and pasted the code; I didn't comment that line out). However, in the uC code I've written, that would end up going to the serial port (which I tend to use as a debug console if it's not needed for something else). I doubt very much it would be to print to the LCD; if it were, then what is the point of the rest of the function? ;)


Torben
Oh yeah.. Thanks..
 
The printf function in most compiler libraries is used to output a string of text to the "standard output" device. In the early days of computing it was typically the users console device which could have been a teleprinter or a glass teletype. The code below the commented function call must be an attempt to duplicate the function in some sense.

I see the included code makes use of the uitoa(...,...) function. I'm sure it is derived from the simpler itoa() function. Again check your compiler libraries to see what functions it has. If there is nothing there then try to score a copy of the "The Standard C Library" by P.J. Plauger. A steal at $5.49 used on amazon
Amazon.com: The Standard C Library: P. J. Plauger: Books
 
Last edited:
The printf function in most compiler libraries is used to output a string of text to the "standard output" device. In the early days of computing it was typically the users console device which could have been a teleprinter or a glass teletype. The code below the commented function call must be an attempt to duplicate the function in some sense.

I see the included code makes use of the uitoa(...,...) function. I'm sure it is derived from the simpler itoa() function. Again check your compiler libraries to see what functions it has. If there is nothing there then try to score a copy of the "The Standard C Library" by P.J. Plauger. A steal at $5.49 used on amazon
Amazon.com: The Standard C Library: P. J. Plauger: Books

Yeah, I'm not sure if PICs are the same way but at least with 8051s you can (must, with sdcc) provide your own putchar() to determine where printf() sends its output. I wonder if the MPLAB IDE provides a debug console which would take the printf() by default?

If the included C library is fairly complete you may find inet_ntoa() and friends in <arpa/inet.h> which is probably the easiest way to convert an IP address from in_addr_t to a dotted quad string. inet_aton() does the reverse conversion. I think this may be lower level than the C18 TCP/IP stack though (again, I know very, very little about C18's TCP/IP stack). It's just another idea, if it's available to you.


Torben
 
Up to now I have used the CCS compiler for LCD projects but the C18 look easy enough.

Download MPLAB_C18_Libraries_51297f.pdf from Microchips site. The number part of the name may have changed if the updated it.

Look in section 4.7 CHARACTER OUTPUT FUNCTIONS

As suggested you need a putc function.

int fprintf (FILE *f, const rom char *fmt, ...);​

I think that all you have to do is modify C:\MCC18\src\traditional\stdclib\ _user_putc.c

Code:
/* this default version should not do anything. it is entirely a
   placeholder symbol. To keep code size at a minimum, it is declared
   without a return value or parameters. The caller will still clean up 
   the stack frame correctly. 

   When using the _H_USER stream, the function will be implemented in
   application code with the prototype:   
    int _user_putc (char c);
  */
void
_user_putc (void)
{
}

Then call printf with the USER stream whatever it name is :) Wife has ice creame ready have to run :)
 
This is all good stuff and with the method used by itoa() and friends you should be able to construct precisely the thing you need.
 
Status
Not open for further replies.

New Articles From Microcontroller Tips

Back
Top