web123456

The conversion relationship between bits, bytes, and MBs

B is the abbreviation of Byte, meaning byte; b is the abbreviation of bit, meaning bit; Kb is the kilobit, KB is the kilobyte; MB is the megabyte;

Conversion relationship:

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

8bit=1Byte;

1024KB=1MB;

1024MB=1GB;

1024GB=1TB;

About transmission rate and network speed:

We usually see that the transmission rate of the USB2.0 interface is 480mbps, so we naturally think that its speed is 480M/S. In fact, this is wrong. mbps is the abbreviation of mega bits per second, meaning Mb/S (megabits/second). The correct understanding should be: 60MB/S, and the conversion relationship is as above, which is a 1:8 relationship.

The 100M network speed usually refers to 100Mbps (1024 bits), which is converted into 12.5MB/S. Theoretically, the download speed can reach 12.5MB/S.

Calculation of hard disk capacity:

When manufacturers make hard disks, for the sake of calculation convenience, 1KB is only 1000Byte, 1MB is only 1000KB, and 1G is only 1000MB. . . . . . So, a 500G hard drive is actually only

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

The general formula for estimating the size of a hard disk:

Hard disk appearance size (G) * 1000*1000*1000/1024*1024*1024=actual hard disk size (G)

/**
      *
      * @title
      * @date January 7, 2019
      * @author niuchang
      * @param size
      * @param old_unit File unit before conversion
      * @param new_unit Converted file units
      * @return
      */
     public static long unitConvert(long size,String old_unit,String new_unit){
    	 long new_size=0;
    	 if ((old_unit) && (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)) {//Small unit to large unit
    					 if (FILE_UNIT_BIT.equals(old_unit) && FILE_UNIT_KB.equals(new_unit)) {//Bit bit to kilobyte kb
    						 size=size/8/1024;
    					 }else if (FILE_UNIT_BIT.equals(old_unit) && FILE_UNIT_MB.equals(new_unit)) {//Bit bit to megabyte mb
    						 size=size/8/1024/1024;
    					 }else if (FILE_UNIT_BIT.equals(old_unit) && FILE_UNIT_GB.equals(new_unit)) {//Bit bit to gigabyte gb
    						 size=size/8/1024/1024/1024;
    					 }else if (FILE_UNIT_BYTE.equals(old_unit) && FILE_UNIT_KB.equals(new_unit)) {//Byte byte to kilobyte kb
    						 size=size/1024;
    					 }else if (FILE_UNIT_BYTE.equals(old_unit) && FILE_UNIT_MB.equals(new_unit)) {//Byte byte to megabyte mb
    						 size=size/1024/1024;
    					 }else if (FILE_UNIT_BYTE.equals(old_unit) && FILE_UNIT_GB.equals(new_unit)) {//Byte byte to gigabyte gb
    						 size=size/1024/1024/1024;
    					 }else if (FILE_UNIT_KB.equals(old_unit) && FILE_UNIT_MB.equals(new_unit)) {//Kneimeter kb to megabyte mb
    						 size=size/1024;
    					 }else if (FILE_UNIT_KB.equals(old_unit) && FILE_UNIT_GB.equals(new_unit)) {//Key byte kb to Gigabyte gb
    						 size=size/1024/1024;
    					 }else if (FILE_UNIT_MB.equals(old_unit) && FILE_UNIT_GB.equals(new_unit)) {//Megabyte mb to Gigabyte gb
    						 size=size/1024;
    					 }else{//More type conversions
    						 boolean isRe=false;
    						 double sized=size;
    						 if (FILE_UNIT_BIT.equals(old_unit)) {//Bit bit to byte 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 byte to kilobyte 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) {//Key byte kb to megabyte 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) {//Megabyte mb to Gigabyte 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) {//Gigabyte gb to terabytetb
    							 sized=sized/1024;
    						 }
    						 size=(long) sized;
    					 }
    				 }else{//Big unit to small unit
    					 if (FILE_UNIT_BIT.equals(new_unit) && FILE_UNIT_KB.equals(old_unit)) {//Kneimeter kb to bit bit
    						 size=size*8*1024;
    					 }else if (FILE_UNIT_BIT.equals(new_unit) && FILE_UNIT_MB.equals(old_unit)) {//Megabyte mb to bit bit
    						 size=size*8*1024*1024;
    					 }else if (FILE_UNIT_BIT.equals(new_unit) && FILE_UNIT_GB.equals(old_unit)) {//Gigabyte gb to bit bit
    						 size=size*8*1024*1024*1024;
    					 }else if (FILE_UNIT_BYTE.equals(new_unit) && FILE_UNIT_KB.equals(old_unit)) {//Kneimeter kb to byte byte
    						 size=size*1024;
    					 }else if (FILE_UNIT_BYTE.equals(new_unit) && FILE_UNIT_MB.equals(old_unit)) {//Megabyte mb to byte byte
    						 size=size*1024*1024;
    					 }else if (FILE_UNIT_BYTE.equals(new_unit) && FILE_UNIT_GB.equals(old_unit)) {//Gigabyte gb to byte byte
    						 size=size*1024*1024*1024;
    					 }else if (FILE_UNIT_KB.equals(new_unit) && FILE_UNIT_MB.equals(old_unit)) {//Megabyte mb to kilobyte kb
    						 size=size*1024;
    					 }else if (FILE_UNIT_KB.equals(new_unit) && FILE_UNIT_GB.equals(old_unit)) {//Gigabyte gb to kilobyte kb
    						 size=size*1024*1024;
    					 }else if (FILE_UNIT_MB.equals(new_unit) && FILE_UNIT_GB.equals(old_unit)) {//Gigabyte gb to megabyte mb
    						 size=size*1024;
    					 }else{//More type conversions
    						 boolean isRe=false;
    						 double sized=size;
    						 if (FILE_UNIT_TB.equals(old_unit) && !isRe) {//Terabytet tb to gigabyte 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) {//Gigabyte gb to megabyte 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) {//Megabyte mb to kilobyte 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) {//Kneimeter kb to byte 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 byte to bit bit
    							 sized=sized*8;
    						 }
    						 size=(long) sized;
    					 }
    				 }
    				 new_size=size;
    			 }else{
    				 throw new RuntimeException("No longer convert range, unit before conversion ["+old_unit+"], unit after conversion ["+new_unit+"]");
    			 }
			 }else{
				 new_size=size;
				 ("The same unit does not need to be converted, the unit before conversion ["+old_unit+"], and the unit after conversion ["+new_unit+"]");
			 }
		 }
    	 return new_size;
     }
     /**
      * Is the old unit smaller than the new unit
      * @title
      * @date January 7, 2019
      * @author niuchang
      * @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("The unit type conversion is not supported, the unit before conversion ["+old_unit+"], and the unit after conversion ["+new_unit+"]");
		 }else{
			 return false;
		 }
     }