String u32IntToIP(uint32_t x) { // receive the u32_t number as argument
Serial.println(x);
String outputBin = ""; // initialize the main string
byte m = 0;
String fourthOctet = "", thirdOctet = "", secondOctet = "", firstOctet = "";
for ( byte i = 0; i < 32; i++ ) { // go through all 32 bits MSB to LSB
if ( i % 8 == 0 && i > 3 ) { // put delimter mark . after each OCTET
outputBin = "." + outputBin;
m++;
}
String xx = String(x % 2); // get ramainder with module sign ie: % and convert it to a string
if ( m == 0 ) {
fourthOctet = xx + fourthOctet;
}
if ( m == 1 ) {
thirdOctet = xx + thirdOctet;
}
if ( m == 2 ) {
secondOctet = xx + secondOctet;
}
if ( m == 3 ) {
firstOctet = xx + firstOctet;
}
outputBin = xx + outputBin; // attach the resulted string to the main strinng
x = x / 2;
} // end go through all 32 bits
String aa = firstOctet + '.' + secondOctet + '.' + thirdOctet + '.' + fourthOctet ;
// Serial.println(aa);
aa = binaryStringToDec(firstOctet) + '.' + binaryStringToDec(secondOctet) + '.' + binaryStringToDec(thirdOctet) + '.' + binaryStringToDec(fourthOctet) ;
// Serial.println(aa);
return aa;
}
String binaryStringToDec(String xx) { // eg: 00100000 =(0x2^7)+(0x2^6)+(1x2^5)+(0x2^4)+(0x2^3)+(0x2^2)+(0x2^1)+(0x2^0) = 32
byte len = xx.length(); // get length of the string we have ie: 8
float result = 0;
int y = len - 1;
for ( byte x = 0; x < len; x++ ) { // go thru each character
float num_ = xx.charAt(x); // exctar character at index 'x' from string, into ASCII
num_ = num_ - 48; // ASCII into normal numbers ie: asii 48 = 0, ascii 49 = 1
num_ = num_ * (pow(2, y)); // that is 2^y
result += num_; // sum up all the summations
y--; // exponent
}
String res = String(result);
res.replace(".00", "");
return res;
}