Search code examples
arduinointegerip

How to get the last octet of an IP address into 3 separate ints?


I need to get the last octet of an IP address into 3 separate ints to control a MAX7219.

I know the octet is an uint8_t

Using IPAddress ip = Wifi.localIP();, say my ip[3] was 148, I need:
int b1 = 1
int b2 = 4
int b3 = 8
but say ip[3] was only 8, then b1 and b2 have to be 0.


Solution

  • First thing that came to mind; there are probably more elegant ways to do this, but it works:

    b3 = ip[3] % 10;
    b2 = ((ip[3] % 100) - b3) / 10;
    b1 = (ip[3] - (10 * b2) - b3) / 100;
    

    I don't know why you would need separate integers, though.