Nimependa hili swali, Programmers embu mtoe idea hapa

mathsjery

JF-Expert Member
Sep 26, 2015
2,208
1,755
Take the following IPv4 address: 128.32.10.1

This address has 4 octets where each octet is a single byte (or 8 bits).
  • 1st octet 128 has the binary representation: 10000000
  • 2nd octet 32 has the binary representation: 00100000
  • 3rd octet 10 has the binary representation: 00001010
  • 4th octet 1 has the binary representation: 00000001
So 128.32.10.1 == 10000000.00100000.00001010.00000001

Because the above IP address has 32 bits, we can represent it as the unsigned 32 bit number: 2149583361.

HAPA: Naamini tunafahamu mchezo wa ip address na data types in programming, Anasema,

Complete the function that takes an unsigned 32 bit number and returns a string representation of its IPv4 address
 
baada ya kugugo sanaa, mwishowe nimeifanya kwa C

Code:
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;
}

nainvoke function kwa:

String getTheIP = u32IntToIP(2149583361); // 2149583361 ni argument aka input

Serial.println(getTheIP);


result.PNG
 
Nakusalimu katika jina la C, iko poa, nakuja
baada ya kugugo sanaa, mwishowe nimeifanya kwa C

Code:
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;
}

nainvoke function kwa:

String getTheIP = u32IntToIP(2149583361); // 2149583361 ni argument aka input

Serial.println(getTheIP);


View attachment 1798170
 

Similar Discussions

Back
Top Bottom