位(bit)、字节(Byte)、MB(兆位)之间的换算关系

B是Byte的缩写,意思是字节;b是bit的缩写,意思是比特位;Kb是千比特位,KB是千字节;MB意思是兆字节;

换算关系:

  1MB=1024KB=1024B*1024=1048576B;

  8bit=1Byte;

  1024KB=1MB;

  1024MB=1GB;

  1024GB=1TB;

关于传输速率和网速:

  我们通常看到USB2.0接口的传输速率是480mbps,就很自然的认为其速率是480M/S,其实这是不对的。mbps是mega bits per second的缩写,意思是Mb/S(兆位/秒),正确的理解应该是:60MB/S,其中的换算关系如上,是1:8的关系。

  平时说的100M的网速,指的是100Mbps(1024比特位),换算过来,就是12.5MB/S。理论上的下载速度可以达到12.5MB/S。

硬盘容量的计算:

  厂商在制作硬盘的时候,为了计算方便,1KB只有1000Byte,1MB只有1000KB,1G只有1000MB。。。。。。所以,500G的硬盘实际上只有

  500*1000*1000*1000/1024*1024*1024 = 465.66G

  估计一块硬盘大小的通式:

  硬盘出场大小(G)*1000*1000*1000/1024*1024*1024=实际硬盘大小(G)

/**
     * 
     * @title
     * @date 2019年1月7日
     * @author niuchuang
     * @param size
     * @param old_unit 转换前文件单位
     * @param new_unit 转换后文件单位
     * @return
     */
    public static long unitConvert(long size,String old_unit,String new_unit){
    	long new_size=0;
    	if (StringUtils.isNotBlank(old_unit) && StringUtils.isNotBlank(new_unit)) {
    		old_unit=old_unit.toLowerCase().trim();
    		new_unit=new_unit.toLowerCase().trim();
    		if (!old_unit.equals(new_unit)) {
    			if ((FILE_UNIT_BIT.equals(old_unit) || FILE_UNIT_BYTE.equals(old_unit)
    					|| FILE_UNIT_KB.equals(old_unit) || FILE_UNIT_MB.equals(old_unit)
    					|| FILE_UNIT_GB.equals(old_unit) || FILE_UNIT_TB.equals(old_unit))
    					&& (FILE_UNIT_BIT.equals(new_unit) || FILE_UNIT_BYTE.equals(new_unit)
    							|| FILE_UNIT_KB.equals(new_unit) || FILE_UNIT_MB.equals(new_unit)
    							|| FILE_UNIT_GB.equals(new_unit) || FILE_UNIT_TB.equals(new_unit))) {
    				if (isFileUnit(old_unit, new_unit)) {//小单位转大单位
    					if (FILE_UNIT_BIT.equals(old_unit) && FILE_UNIT_KB.equals(new_unit)) {//比特bit 转 千字节kb
    						size=size/8/1024;
    					}else if (FILE_UNIT_BIT.equals(old_unit) && FILE_UNIT_MB.equals(new_unit)) {//比特bit 转 兆字节mb
    						size=size/8/1024/1024;
    					}else if (FILE_UNIT_BIT.equals(old_unit) && FILE_UNIT_GB.equals(new_unit)) {//比特bit 转 千兆字节gb
    						size=size/8/1024/1024/1024;
    					}else if (FILE_UNIT_BYTE.equals(old_unit) && FILE_UNIT_KB.equals(new_unit)) {//字节byte 转 千字节kb
    						size=size/1024;
    					}else if (FILE_UNIT_BYTE.equals(old_unit) && FILE_UNIT_MB.equals(new_unit)) {//字节byte 转 兆字节mb
    						size=size/1024/1024;
    					}else if (FILE_UNIT_BYTE.equals(old_unit) && FILE_UNIT_GB.equals(new_unit)) {//字节byte 转 千兆字节gb
    						size=size/1024/1024/1024;
    					}else if (FILE_UNIT_KB.equals(old_unit) && FILE_UNIT_MB.equals(new_unit)) {//千字节kb 转 兆字节mb
    						size=size/1024;
    					}else if (FILE_UNIT_KB.equals(old_unit) && FILE_UNIT_GB.equals(new_unit)) {//千字节kb 转 千兆字节gb
    						size=size/1024/1024;
    					}else if (FILE_UNIT_MB.equals(old_unit) && FILE_UNIT_GB.equals(new_unit)) {//兆字节mb 转 千兆字节gb
    						size=size/1024;
    					}else{//更多类型转换
    						boolean isRe=false;
    						double sized=size;
    						if (FILE_UNIT_BIT.equals(old_unit)) {//比特bit 转 字节byte
    							sized=sized/8;
    							old_unit=FILE_UNIT_BYTE;
    						}
    						isRe=(isRe || old_unit.equals(new_unit))?true:false;
    						if (FILE_UNIT_BYTE.equals(old_unit) && !isRe) {//字节byte 转 千字节kb
    							sized=sized/1024;
    							old_unit=FILE_UNIT_KB;
    						}
    						isRe=(isRe || old_unit.equals(new_unit))?true:false;
    						if (FILE_UNIT_KB.equals(old_unit) && !isRe) {//千字节kb 转 兆字节mb
    							sized=sized/1024;
    							old_unit=FILE_UNIT_MB;
    						}
    						isRe=(isRe || old_unit.equals(new_unit))?true:false;
    						if (FILE_UNIT_MB.equals(old_unit) && !isRe) {//兆字节mb 转 千兆字节gb
    							sized=sized/1024;
    							old_unit=FILE_UNIT_GB;
    						}
    						isRe=(isRe || old_unit.equals(new_unit))?true:false;
    						if (FILE_UNIT_GB.equals(old_unit) && !isRe) {//千兆字节gb 转 太字节tb
    							sized=sized/1024;
    						}
    						size=(long) sized;
    					}
    				}else{//大单位转小单位
    					if (FILE_UNIT_BIT.equals(new_unit) && FILE_UNIT_KB.equals(old_unit)) {//千字节kb 转 比特bit
    						size=size*8*1024;
    					}else if (FILE_UNIT_BIT.equals(new_unit) && FILE_UNIT_MB.equals(old_unit)) {//兆字节mb 转 比特bit
    						size=size*8*1024*1024;
    					}else if (FILE_UNIT_BIT.equals(new_unit) && FILE_UNIT_GB.equals(old_unit)) {//千兆字节gb 转 比特bit
    						size=size*8*1024*1024*1024;
    					}else if (FILE_UNIT_BYTE.equals(new_unit) && FILE_UNIT_KB.equals(old_unit)) {//千字节kb 转 字节byte
    						size=size*1024;
    					}else if (FILE_UNIT_BYTE.equals(new_unit) && FILE_UNIT_MB.equals(old_unit)) {//兆字节mb 转 字节byte
    						size=size*1024*1024;
    					}else if (FILE_UNIT_BYTE.equals(new_unit) && FILE_UNIT_GB.equals(old_unit)) {//千兆字节gb 转 字节byte
    						size=size*1024*1024*1024;
    					}else if (FILE_UNIT_KB.equals(new_unit) && FILE_UNIT_MB.equals(old_unit)) {//兆字节mb 转 千字节kb
    						size=size*1024;
    					}else if (FILE_UNIT_KB.equals(new_unit) && FILE_UNIT_GB.equals(old_unit)) {//千兆字节gb 转 千字节kb
    						size=size*1024*1024;
    					}else if (FILE_UNIT_MB.equals(new_unit) && FILE_UNIT_GB.equals(old_unit)) {//千兆字节gb 转 兆字节mb
    						size=size*1024;
    					}else{//更多类型转换
    						boolean isRe=false;
    						double sized=size;
    						if (FILE_UNIT_TB.equals(old_unit) && !isRe) {//太字节tb 转 千兆字节gb
    							sized=sized*1024;
    							old_unit=FILE_UNIT_GB;
    						}
    						isRe=(isRe || old_unit.equals(new_unit))?true:false;
    						if (FILE_UNIT_GB.equals(old_unit) && !isRe) {//千兆字节gb 转 兆字节mb
    							sized=sized*1024;
    							old_unit=FILE_UNIT_MB;
    						}
    						isRe=(isRe || old_unit.equals(new_unit))?true:false;
    						if (FILE_UNIT_MB.equals(old_unit) && !isRe) {//兆字节mb 转 千字节kb
    							sized=sized*1024;
    							old_unit=FILE_UNIT_KB;
    						}
    						isRe=(isRe || old_unit.equals(new_unit))?true:false;
    						if (FILE_UNIT_KB.equals(old_unit) && !isRe) {//千字节kb 转 字节byte
    							sized=sized*1024;
    							old_unit=FILE_UNIT_BYTE;
    						}
    						isRe=(isRe || old_unit.equals(new_unit))?true:false;
    						if (FILE_UNIT_BYTE.equals(old_unit) && !isRe) {//字节byte 转 比特bit
    							sized=sized*8;
    						}
    						size=(long) sized;
    					}
    				}
    				new_size=size;
    			}else{
    				throw new RuntimeException("不再转换范围,转换前单位["+old_unit+"],转换后单位["+new_unit+"]");
    			}
			}else{
				new_size=size;
				log.warn("相同单位无需转换,转换前单位["+old_unit+"],转换后单位["+new_unit+"]");
			}
		}
    	return new_size;
    }
    /**
     * 旧单位是否比新单位小
     * @title
     * @date 2019年1月7日
     * @author niuchuang
     * @param old_unit
     * @param new_unit
     * @return
     */
    private static boolean isFileUnit(String old_unit,String new_unit){
    	if (old_unit.equals(FILE_UNIT_BIT) && !new_unit.equals(FILE_UNIT_BIT)) {
			return true;
		}else if (old_unit.equals(FILE_UNIT_BYTE) && (!new_unit.equals(FILE_UNIT_BIT) && !new_unit.equals(FILE_UNIT_BYTE))) {
			return true;
		}else if (old_unit.equals(FILE_UNIT_KB) && (!new_unit.equals(FILE_UNIT_BIT) && !new_unit.equals(FILE_UNIT_BYTE)
				&& !new_unit.equals(FILE_UNIT_KB))) {
			return true;
		}else if (old_unit.equals(FILE_UNIT_MB) && (!new_unit.equals(FILE_UNIT_BIT) && !new_unit.equals(FILE_UNIT_BYTE)
				&& !new_unit.equals(FILE_UNIT_KB) && !new_unit.equals(FILE_UNIT_MB))) {
			return true;
		}else if (old_unit.equals(FILE_UNIT_GB) && (!new_unit.equals(FILE_UNIT_BIT) && !new_unit.equals(FILE_UNIT_BYTE)
				&& !new_unit.equals(FILE_UNIT_KB) && !new_unit.equals(FILE_UNIT_MB) && !new_unit.equals(FILE_UNIT_GB))) {
			return true;
		}else if (old_unit.equals(FILE_UNIT_TB) && (!new_unit.equals(FILE_UNIT_BIT) && !new_unit.equals(FILE_UNIT_BYTE)
				&& !new_unit.equals(FILE_UNIT_KB) && !new_unit.equals(FILE_UNIT_MB) && !new_unit.equals(FILE_UNIT_GB)
				&& !new_unit.equals(FILE_UNIT_TB))) {
			throw new RuntimeException("不支持该单位类型转换,转换前单位["+old_unit+"],转换后单位["+new_unit+"]");
		}else{
			return false;
		}
    }

 

  • 3
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值