Kilobyte (Selektionen)
Ein Kilogramm ist bekanntlich 1000 Gramm. Die ISO-Abkürzung für Kilo (1000) ist k. Somit ist ein Kilobyte gleich 1000 Byte (kB). In der Informatik werden aber (für RAM Bausteine) häufiger 1024 Byte als Kilobyte verwendet. Das kommt daher, dass ganze Zahlen im Zweiersystem repräsentiert werden und 2 hoch 10 eben 1024 (und nicht 1000) errgibt. Die korrekte ISO-Abkürzung für 1024 Byte lautet jedoch Kibibyte, abgekürzt KiB. Analog ist ein Mebibyte (MiB) = 1024 * 1024, ein Megabyte ist hingegen nur 1 000 000 Byte. G steht für Giga, also 1000 Millionen, usw.
Schreiben Sie ein Programm, das mindestens die folgenden Größen umrechnen kann: Bit, Byte (8 Bits), kB (Kilobyte), KiB (Kibibyte), MB (Megabyte), MiB (Mebibyte), GB (Gigabyte) und GiB (Gibibyte).
Bei Ihrem Programm wird der Anwender nach der Ausgangsgröße (Bit, Byte, kB, KiB, MB, ...), der Zielgröße (Bit, ...) und dem umzurechnenden Wert gefragt.
Beispiel:
"Folgende Größen können umgerechnet werden:
Bit, Byte, kB, KiB, MB, MiB, GB, GiB"
Geben Sie die Ausgangsgröße "VON" an: KiB
Geben Sie die Zielgröße "NACH" an : Bit
Geben Sie den umzurechnenden Wert an: 2
Resultat: 2 KiB = 16384 Bits
0 Kommentare
4 Lösung(en)
package ch.programmieraufgaben.kilobyte;
import java.util.Scanner;
/**
* Rechne Informationseinheiten um: bit, byte, kB, kbit, KiB, MB, MBit, ...
* @author Philipp Gressly (phi AT gressly DOT ch)
*/
public class Informationsrechner {
public static void main(String[] args) {
new Informationsrechner().top();
}
void top() {
print("bit, byte, kByte, KiByte, kBit, MByte, MiByte, MBit, GByte, GiByte, GBit?");
String quellGroesse = eingabe("Bitte Quellgroesse eingeben");
String zielGroesse = eingabe("Bitte Zielgroesse eingeben");
double wert = eingabeDouble("Wert");
double origwert = wert;
// Dreisatz Teil 1 berechne bits
if("byte".equals(quellGroesse)) {
wert = wert * 8; }
if("kByte".equals(quellGroesse)) {
wert = wert * 8 * 1000; }
if("KiByte".equals(quellGroesse)){
wert = wert * 8 * 1024; }
if("kBit".equals(quellGroesse)) {
wert = wert * 1000; }
if("MByte".equals(quellGroesse)) {
wert = wert * 8 * 1000000; }
if("MiByte".equals(quellGroesse)) {
wert = wert * 8 * 1024 * 1024; }
if("MBit".equals(quellGroesse)) {
wert = wert * 1000000; }
if("GByte".equals(quellGroesse)) {
wert = wert * 8 * 1000000000; }
if("GiByte".equals(quellGroesse)) {
wert = wert * 8 * 1024 * 1024 * 1024; }
if("GBit".equals(quellGroesse)) {
wert = wert * 1000000000; }
// Dreisatz 2. Teil in Zielgroesse umrechnen
if("byte".equals(zielGroesse)) {
wert = wert / 8; }
if("kByte".equals(zielGroesse)) {
wert = wert / 8 / 1000; }
if("KiByte".equals(zielGroesse)){
wert = wert / 8 / 1024; }
if("kBit".equals(zielGroesse)) {
wert = wert / 1000; }
if("MByte".equals(zielGroesse)) {
wert = wert / 8 / 1000000; }
if("MiByte".equals(zielGroesse)) {
wert = wert / 8 / 1024 / 1024; }
if("MBit".equals(zielGroesse)) {
wert = wert / 1000000; }
if("GByte".equals(zielGroesse)) {
wert = wert / 8 / 1000000000; }
if("GiByte".equals(zielGroesse)) {
wert = wert / 8 / 1024 / 1024 / 1024; }
if("GBit".equals(zielGroesse)) {
wert = wert / 1000000000; }
print(origwert + " " + quellGroesse + " = " + wert + " " + zielGroesse);
}
Scanner sc = new Scanner(System.in);
double eingabeDouble(String frage) {
print(frage + ':');
return sc.nextDouble();
}
String eingabe(String frage) {
print(frage + ':');
return sc.next();
}
void print(String text) {
System.out.println(text);
}
} // end of class Informationsrechner
Lösung von: Philipp G. Freimann (BBW (Berufsbildungsschule Winterthur) https://www.bbw.ch)
package ch.santis.programmierenlernen.kapitel3;
import java.math.BigDecimal;
import java.util.Scanner;
public class Aufgabe_3_9 {
public static void main(String[] args) {
new Aufgabe_3_9().top();
}
void top() {
print("Folgende Einheiten können umgerechnet werden:" + "\n" + "Bit, Byte, kB, KiB, MB, MiB, GB, GiB");
String eingabetyp = "";
while(!eingabetyp.matches("Bit|Byte|kB|KiB|MB|MiB|GB|GiB")) {
eingabetyp = eingabeString("Geben Sie die Eingabeeinheit an:");
}
String zieltyp = "";
while(!zieltyp.matches("Bit|Byte|kB|KiB|MB|MiB|GB|GiB")) {
zieltyp = eingabeString("Geben Sie die Zieleinheit an:");
}
BigDecimal eingabewert = eingabeBig("Geben Sie den umzurechnenden Wert (Zahl) an:");
BigDecimal wertinbit = BigDecimal.ONE;
BigDecimal ausgabewert = BigDecimal.ZERO;
if(!eingabetyp.equals(zieltyp)) {
// Eingabewert zu kleinster Einheit (Bit) umrechnen
if(eingabetyp.equals("Bit")) {
wertinbit = eingabewert;
}
else if(eingabetyp.equals("Byte")) {
wertinbit = eingabewert.multiply(new BigDecimal(8));
}
else if(eingabetyp.equals("kB")) {
wertinbit = eingabewert.multiply(new BigDecimal(8000));
}
else if(eingabetyp.equals("KiB")) {
wertinbit = eingabewert.multiply(new BigDecimal(8192));
}
else if(eingabetyp.equals("MB")) {
wertinbit = eingabewert.multiply(new BigDecimal(8000000));
}
else if(eingabetyp.equals("MiB")) {
wertinbit = eingabewert.multiply(new BigDecimal(8388608));
}
else if(eingabetyp.equals("GB")) {
wertinbit = eingabewert.multiply(new BigDecimal(8000000000l)); // Literal has to be long type
}
else if(eingabetyp.equals("GiB")) {
wertinbit = eingabewert.multiply(new BigDecimal(8589934592l)); // Literal has to be long type
}
// Umgerechneter Wert in Bit zu Zieleinheit umrechnen
if(zieltyp.equals("Bit")) {
ausgabewert = wertinbit;
}
else if(zieltyp.equals("Byte")) {
ausgabewert = wertinbit.multiply(new BigDecimal(0.125));
}
else if(zieltyp.equals("kB")) {
ausgabewert = wertinbit.multiply(new BigDecimal(0.000125));
}
else if(zieltyp.equals("KiB")) {
ausgabewert = wertinbit.multiply(new BigDecimal(0.0001220703125));
}
else if(zieltyp.equals("MB")) {
ausgabewert = wertinbit.multiply(new BigDecimal(1.25E-7));
}
else if(zieltyp.equals("MiB")) {
ausgabewert = wertinbit.multiply(new BigDecimal(1.1920928955078E-7));
}
else if(zieltyp.equals("GB")) {
ausgabewert = wertinbit.multiply(new BigDecimal(1.25E-10));
}
else if(zieltyp.equals("GiB")) {
ausgabewert = wertinbit.multiply(new BigDecimal(1.1641532182693E-10));
}
}
else {
ausgabewert = eingabewert;
}
print("Eingabeeinheit " + eingabewert + " " + eingabetyp + " ist " + ausgabewert.stripTrailingZeros() + " " + zieltyp);
}
Scanner sc = new Scanner(System.in);
String eingabeString(String textstring) {
print(textstring);
return sc.next();
}
// Zahl einlesen und bei Error (wenn Eingabe keine Zahl ist) nochmals Eingabe einer Zahl erfragen
BigDecimal eingabeBig(String textstring) {
BigDecimal eingabe = BigDecimal.ZERO;
boolean check = false;
do {
try {
print(textstring);
eingabe = sc.nextBigDecimal();
check = true;
} catch(Exception e) {
print("ERROR: Das ist keine Zahl!");
sc.nextLine();
}
} while (!check);
return eingabe;
}
void print(String textstring) {
System.out.println(textstring);
}
}
Lösung von: Jan Roth (Santis Training AG)
// NET 6.x | C# 10.x | VS-2022
Console.WriteLine(Converter(TypeValue.KiB, TypeValue.bit, 2));
Console.WriteLine(Converter(TypeValue.kB, TypeValue.MiB, 2));
static string Converter(TypeValue source, TypeValue target, double value) => $"{value:#,##0.########} {source} -> {value * (double)source / (double)target:#,##0.########} {target}";
enum TypeValue : ulong {
bit = 1, B = 8, kB = 8_000, KiB = 8_192, kbit = 1_000, MB = 8_000_000,
MiB = 8_388_608, Mbit = 1_000_000, GB = 8_000_000_000, GiB = 8_589_934_592, Gbit = 1_000_000_000
}
Lösung von: Jens Kelm (@JKooP)
document.write(`
<input type="number" id="num" onchange="compute()"></input>
<select id="from" onchange="compute()"></select> nach
<select id="to" onchange="compute()"></select>
<p id="out"></p>
`);
function addOpt(text, value) {
let opt = document.createElement('option');
opt.value = value;
opt.innerText = text;
document.getElementById('to').appendChild(opt);
}
function addOpts(text, value) {
let opt = document.createElement('option'),
opt2 = document.createElement('option');
opt.value = value; opt2.value = value;
opt.innerText = text; opt2.innerText = text;
document.getElementById('from').appendChild(opt);
document.getElementById('to').appendChild(opt2);
}
addOpts('Bit (b)', 1);
addOpts('Byte (B)', 8);
addOpts('Kilobit (kb)', 1000);
addOpts('Kibibit (kib)', 1024);
addOpts('Kilobyte (kB)', 8*1000);
addOpts('Kibibyte (kiB)', 8*1024);
addOpts('Megabit (Mb)', 1000**2);
addOpts('Mebibit (Mib)', 1024**2);
addOpts('Megabyte (MB)', 8*1000**2);
addOpts('Mebibyte (MiB)', 8*1024**2);
addOpts('Gigabit (Gb)', 1000**3);
addOpts('Gibibit (Gib)', 1024**3);
addOpts('Gigabyte (GB)', 8*1000**3);
addOpts('Gibibyte (GiB)', 8*1024**3);
addOpts('Terabit (Tb)', 1000**4);
addOpts('Tebibit (Tib)', 1024**4);
addOpts('Terabyte (TB)', 8*1000**4);
addOpts('Tebibyte (TiB)', 8*1024**4);
addOpt('Schreibmaschinenseiten', 40000);
addOpt('Disketten', 11796480);
addOpt('Bibeln', 32000000);
addOpt('CD-ROMs', 5898000000);
addOpt('menschliche Genome', 10872000000);
addOpt('DVDs', 35040000000);
function compute() {
let num = document.getElementById('num'),
from = document.getElementById('from'),
to = document.getElementById('to'),
out = document.getElementById('out'),
bits = num.value * from.value;
out.innerText = (bits / to.value).toLocaleString();
}
Lösung von: Lisa Salander (Heidi-Klum-Gymnasium Bottrop)
Verifikation/Checksumme:
- 2 kB = 0.001907349 MiB
Aktionen
Neue Lösung hinzufügen
Bewertung
Durchschnittliche Bewertung:
Meta
Zeit: | 2 |
Schwierigkeit: | k.A. |
Webcode: | nk4t-g9hc |
Autor: | Philipp G. Freimann (BBW (Berufsbildungsschule Winterthur) https://www.bbw.ch) |