Buch Cover Buch Cover Buch Cover Buch Cover

Web-Code: - Webcode Help

IP-Adressen (2) (Zeichenketten)

Kehren Sie die IP-Adressen (1) um: Gegeben ist eine IP-Adresse
(als vorzeichenlose ganze Zahl mit mind. 32 Bits). Gesucht ist die vierteilige Dezimalnotation (z. B. "192.168.0.203" al String). Der Prototyp sieht wie folgt aus:

ipTransformation(bin: integer): string

 

0 Kommentare

Bitte melde dich an um einen Kommentar abzugeben

4 Lösung(en)

def ipTransformation(n):
   li=[]
   for i in range(4):
      li.append(n % 256)
      n = n >> 8

   li.reverse()   
   s=''
   for i in range(len(li)):
      s=s+str(li[i])
      if i < 3:
            s=s+'.'
   print s

ipTransformation(3229232429)
ipTransformation(2207834768)
                
function transformIp(int) {
  int = int.toString(2);
  let ip = [];

  while (int.length < 32) int = '0' + int;
  for (let i = 0; i <= 24; i = i + 8)
    ip.push(parseInt(int.slice(i, i+8), 2));

  return ip.join('.');
}

// test
console.log( transformIp(1853391500) );
console.log( transformIp(3542816668) );

                

Lösung von: Lisa Salander (Heidi-Klum-Gymnasium Bottrop)

// NET 6.x | C# 10.x | VS-2022

static string ConvertIntIPToString(int n) => 
    string.Join(".", new System.Text.RegularExpressions.Regex(@"\d{8}")
        .Matches((new string('0', 32) + Convert.ToString(n, 2))[^32..])
        .Select(x => Convert.ToInt32(x.ToString(), 2)));

Console.WriteLine(ConvertIntIPToString(1853391500));
                

Lösung von: Jens Kelm (@JKooP)

// C++ 14 | VS-2022
#include <iostream>
#include <bitset>
#include <string>

const auto get_ip_from_int(int n) {
    const std::string bin{ std::bitset<32>(n).to_string() };
    std::string ip{};
    for (size_t i{ 0 }; i < bin.length(); i += 8)
        ip += (std::to_string(std::bitset<8>(bin.substr(i, 8)).to_ulong()) + ".");
    return ip.substr(0, ip.length() - 1);
}

int main() {
    std::cout << get_ip_from_int(1853391500) << "\n";
}
                

Lösung von: Jens Kelm (@JKooP)

Verifikation/Checksumme:

Prüfen Sie die Resultate mit denjenigen aus Aufgabe IP-Adressen (1).

Aktionen

Bewertung

Durchschnittliche Bewertung:

Eigene Bewertung:
Bitte zuerst anmelden

Meta

Zeit: 2
Schwierigkeit: k.A.
Webcode: ndma-4uff
Autor: Philipp G. Freimann (BBW (Berufsbildungsschule Winterthur) https://www.bbw.ch)

Download PDF

Download ZIP

Zu Aufgabenblatt hinzufügen