java工具类 MB GB TB PB和Mbps之间的转换及编程实现

在计算机网络和通信中,Mbps (兆比特每秒)和MB/s (兆字节每秒)是常用的单位,用于示数据传输速度或带宽。虽然它们都表示每秒传输的数据量,但Mbps和MB/s使用的是不同的计量单位,因此在进行转换时需要注意。


首先,我们来了解Mbps和MB/s之间的换算关系。1 Mbps等于1兆比特每秒,而1字节等于8比特。因此,要将Mbps转换为MB/s, 需要将Mbps除以8。换句话说,1 MB/s等于8 Mbps。

下面是java代码实现工具类

MB GB TB PB转换Mbps

public class DataRateConverter {

    private static final double MB_TO_Mbps = 8.0;
    private static final double GB_TO_Mbps = 8.0 * 1024;
    private static final double TB_TO_Mbps = 8.0 * 1024 * 1024;
    private static final double PB_TO_Mbps = 8.0 * 1024 * 1024 * 1024;


    public static double convertToMbps(String data, String unit) {
        double value;
        try {
            value = Double.parseDouble(data);
        } catch (NumberFormatException e) {
            throw new IllegalArgumentException("Invalid data rate value.");
        }
        switch (unit) {
            case "MB":
                return value * MB_TO_Mbps;
            case "GB":
                return value * GB_TO_Mbps;
            case "TB":
                return value * TB_TO_Mbps;
            case "PB":
                return value * PB_TO_Mbps;
            default:
                throw new IllegalArgumentException("Unsupported unit. Use MB, GB, TB, or PB.");
        }
    }

    public static void main(String[] args) {
        try {
            String data = "10";
            String rate = "MB";
            double megabitsPerSecond = convertToMbps(data, rate);
            System.out.println(data + " " + rate + " is equal to " + megabitsPerSecond + " Mbps.");
        } catch (IllegalArgumentException e) {
            System.err.println("Error: " + e.getMessage());
        }
    }
}

Mbps转换MB GB TB PB

public class MbpsToDataRateConverter {

    private static final double Mbps_TO_MB = 1.0 / 8.0;
    private static final double Mbps_TO_GB = Mbps_TO_MB / 1024.0;
    private static final double Mbps_TO_TB = Mbps_TO_GB / 1024.0;
    private static final double Mbps_TO_PB = Mbps_TO_TB / 1024.0;

    public static double convertMbpsToDataRate(double mbps, String unit) {
        if (mbps < 0) {
            throw new IllegalArgumentException("Mbps value must be non-negative.");
        }

        double result;
        switch (unit.toLowerCase()) {
            case "mb":
                result = mbps / Mbps_TO_MB;
                break;
            case "gb":
                result = mbps / Mbps_TO_GB;
                break;
            case "tb":
                result = mbps / Mbps_TO_TB;
                break;
            case "pb":
                result = mbps / Mbps_TO_PB;
                break;
            default:
                throw new IllegalArgumentException("Unsupported unit. Use MB, GB, TB, or PB.");
        }
        return result;
    }

    public static void main(String[] args) {
        try {
            double mbps = 100.0;
            String unit = "GB";
            double dataRate = convertMbpsToDataRate(mbps, unit);
            System.out.println(mbps + " Mbps is equal to " + dataRate + " " + unit);
        } catch (IllegalArgumentException e) {
            System.err.println("Error: " + e.getMessage());
        }
    }
}

MB GB TB PB之间的相互转换以及转换成最大单位

public class DataSizeConverter {

    private static final long BYTES_IN_MB = 1024L * 1024;
    private static final long BYTES_IN_GB = 1024L * BYTES_IN_MB;
    private static final long BYTES_IN_TB = 1024L * BYTES_IN_GB;
    private static final long BYTES_IN_PB = 1024L * BYTES_IN_TB;

     /**
     * MB GB TB PB 直接的转换
     *
     * @param value
     * @param fromUnit 原单位
     * @param toUnit   转换后单位
     * @return double
     */
    public static double convertDataSize(double value, String fromUnit, String toUnit) {
        fromUnit = fromUnit.toLowerCase();
        toUnit = toUnit.toLowerCase();

        if (fromUnit.equals(toUnit)) {
            return value;
        }

        long bytes;
        switch (fromUnit) {
            case "mb":
                bytes = (long) value * BYTES_IN_MB;
                break;
            case "gb":
                bytes = (long) value * BYTES_IN_GB;
                break;
            case "tb":
                bytes = (long) value * BYTES_IN_TB;
                break;
            case "pb":
                bytes = (long) value * BYTES_IN_PB;
                break;
            default:
                throw new IllegalArgumentException("Unsupported 'fromUnit'. Use MB, GB, TB, or PB.");
        }

        switch (toUnit) {
            case "mb":
                return (double) bytes / BYTES_IN_MB;
            case "gb":
                return (double) bytes / BYTES_IN_GB;
            case "tb":
                return (double) bytes / BYTES_IN_TB;
            case "pb":
                return (double) bytes / BYTES_IN_PB;
            default:
                throw new IllegalArgumentException("Unsupported 'toUnit'. Use MB, GB, TB, or PB.");
        }
    }

    /**
     * 转换成最大单位
     *
     * @param value        数据
     * @param unit         MB GB TB PB
     * @return java.lang.String
     */ 
    public static String convertToLargestUnit(double value, String unit) {
        unit = unit.toLowerCase();
        long bytes;
        switch (unit) {
            case "mb":
                bytes = (long) value * BYTES_IN_MB;
                break;
            case "gb":
                bytes = (long) value * BYTES_IN_GB;
                break;
            case "tb":
                bytes = (long) value * BYTES_IN_TB;
                break;
            case "pb":
                bytes = (long) value * BYTES_IN_PB;
                break;
            default:
                throw new IllegalArgumentException("Unsupported unit. Use MB, GB, TB, or PB.");
        }

        if (bytes >= BYTES_IN_PB) {
            return String.format("%.2f PB", (double) bytes / BYTES_IN_PB);
        } else if (bytes >= BYTES_IN_TB) {
            return String.format("%.2f TB", (double) bytes / BYTES_IN_TB);
        } else if (bytes >= BYTES_IN_GB) {
            return String.format("%.2f GB", (double) bytes / BYTES_IN_GB);
        } else {
            return String.format("%.2f MB", (double) bytes / BYTES_IN_MB);
        }
    }

    public static void main(String[] args) {
        double value = 1024.0; 
        String fromUnit = "GB";
        String largestUnitResult = convertToLargestUnit(value, fromUnit);
        System.out.println(value + " " + fromUnit + " is equal to " + largestUnitResult);
    }
}