Monday, October 17, 2022

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

Issue

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;

Untested, but you get the idea.

Not sure why you would need separate integers, though.



Answered By - ocrdu
Answer Checked By - Timothy Miller (PHPFixing Admin)

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.