如何将 IP 地址的最后一个八位字节放入 3 个单独的 int 中
how to get the last octet of an IP address into 3 seperate int's
这让我抓狂
我需要将 IP 地址的最后一个八位字节放入 3 个独立的 INT 中以控制 MAX7219
我知道这个八位字节是一个 uint8_t
with IPAddress ip = Wifi.localIP();
假设我的 ip[3] 是 148
i need
int b1 = 1
int b2 = 4
int b3 = 8
but say ip[3] was only 8 b1 and b2 have to be = 0
提前致谢
首先想到的是;可能有更优雅的方法来做到这一点,但它有效:
b3 = ip[3] % 10;
b2 = ((ip[3] % 100) - b3) / 10;
b1 = (ip[3] - (10 * b2) - b3) / 100;
未经测试,但您明白了。
最终功能;注意 no_green 是带 LED 位的二进制数组
void showIP()
{
IPAddress ip = WiFi.localIP();
b3 = ip[3] % 10;
b2 = ((ip[3] % 100) - b3) / 10;
b1 = (ip[3] - (10 * b2) - b3) / 100;
// write bits to MAX7219
lc.setRow( 0, 6, byte( no_green[ b1] )); // hr leds position
lc.setRow( 0, 3, byte( no_green[ b2] )); // min leds position
lc.setRow( 0, 0, byte( no_green[ b3] )); // sec leds position
delay(6000);
}
这让我抓狂
我需要将 IP 地址的最后一个八位字节放入 3 个独立的 INT 中以控制 MAX7219
我知道这个八位字节是一个 uint8_t
with IPAddress ip = Wifi.localIP(); 假设我的 ip[3] 是 148
i need
int b1 = 1
int b2 = 4
int b3 = 8
but say ip[3] was only 8 b1 and b2 have to be = 0
提前致谢
首先想到的是;可能有更优雅的方法来做到这一点,但它有效:
b3 = ip[3] % 10;
b2 = ((ip[3] % 100) - b3) / 10;
b1 = (ip[3] - (10 * b2) - b3) / 100;
未经测试,但您明白了。
最终功能;注意 no_green 是带 LED 位的二进制数组
void showIP()
{
IPAddress ip = WiFi.localIP();
b3 = ip[3] % 10;
b2 = ((ip[3] % 100) - b3) / 10;
b1 = (ip[3] - (10 * b2) - b3) / 100;
// write bits to MAX7219
lc.setRow( 0, 6, byte( no_green[ b1] )); // hr leds position
lc.setRow( 0, 3, byte( no_green[ b2] )); // min leds position
lc.setRow( 0, 0, byte( no_green[ b3] )); // sec leds position
delay(6000);
}