PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, October 17, 2022

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

 October 17, 2022     arduino, integer, ip     No comments   

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)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

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

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing