Java - convert human readable size to bytes

I found a lot of information about converting the original byte information into a readable format, but I need to do the opposite, that is, convert the string “1.6 GB” to a long value 1717990000. Is there a built-in / well-defined way to do this, or will I pretty much have to roll on your own?

[Edit]: Here is my first hit ...

static class ByteFormat extends NumberFormat { @Override public StringBuffer format(double arg0, StringBuffer arg1, FieldPosition arg2) { // TODO Auto-generated method stub return null; } @Override public StringBuffer format(long arg0, StringBuffer arg1, FieldPosition arg2) { // TODO Auto-generated method stub return null; } @Override public Number parse(String arg0, ParsePosition arg1) { return parse (arg0); } @Override public Number parse(String arg0) { int spaceNdx = arg0.indexOf(" "); double ret = Double.parseDouble(arg0.substring(0, spaceNdx)); String unit = arg0.substring(spaceNdx + 1); int factor = 0; if (unit.equals("GB")) { factor = 1073741824; } else if (unit.equals("MB")) { factor = 1048576; } else if (unit.equals("KB")) { factor = 1024; } return ret * factor; } } 
+8
java long-integer byte human-readable
source share
4 answers

I have never heard of such a famous library that implements such methods of a text parsing utility. But your solution seems like the right implementation.

The only two things I would like to fix in your code:

  • Define the Number parse(String arg0) as static because of its usefulness.

  • define factor for each type of sizing as final static .

those. it will look like this:

 private final static long KB_FACTOR = 1024; private final static long MB_FACTOR = 1024 * KB_FACTOR; private final static long GB_FACTOR = 1024 * MB_FACTOR; public static double parse(String arg0) { int spaceNdx = arg0.indexOf(" "); double ret = Double.parseDouble(arg0.substring(0, spaceNdx)); switch (arg0.substring(spaceNdx + 1)) { case "GB": return ret * GB_FACTOR; case "MB": return ret * MB_FACTOR; case "KB": return ret * KB_FACTOR; } return -1; } 
+2
source share

The revised version of Andremoniy replies that it correctly distinguishes between kilogram and kibi, etc.

 private final static long KB_FACTOR = 1000; private final static long KIB_FACTOR = 1024; private final static long MB_FACTOR = 1000 * KB_FACTOR; private final static long MIB_FACTOR = 1024 * KIB_FACTOR; private final static long GB_FACTOR = 1000 * MB_FACTOR; private final static long GIB_FACTOR = 1024 * MIB_FACTOR; public static double parse(String arg0) { int spaceNdx = arg0.indexOf(" "); double ret = Double.parseDouble(arg0.substring(0, spaceNdx)); switch (arg0.substring(spaceNdx + 1)) { case "GB": return ret * GB_FACTOR; case "GiB": return ret * GIB_FACTOR; case "MB": return ret * MB_FACTOR; case "MiB": return ret * MIB_FACTOR; case "KB": return ret * KB_FACTOR; case "KiB": return ret * KIB_FACTOR; } return -1; } 
+3
source share

I know this much later, but I was looking for a similar function that takes into account the SI prefix . Therefore, I landed, creating it myself, and I thought that it could be useful for other people.

 public static String units = "KMGTPE"; /** * Converts from human readable to byte format * @param number The number value of the amount to convert * @param unit The unit: B, KB, MB, GB, TB, PB, EB * @param si Si prefix * @return byte value */ public static double parse(double number, String unit, boolean si) { String identifier = unit.substring(0, 1); int index = units.indexOf(identifier); //not already in bytes if (index!=-1) { for (int i = 0; i <= index; i++) number = number * (si ? 1000 : 1024); } return number; } 

I am sure that this is possible with recursion. It was too easy to worry ...

+2
source share

All in one answer parses to long :

 public class SizeUtil { public static String units = "BKMGTPEZY"; public static long parse(String arg0) { int spaceNdx = arg0.indexOf(" "); double ret = Double.parseDouble(arg0.substring(0, spaceNdx)); String unitString = arg0.substring(spaceNdx+1); int unitChar = unitString.charAt(0); int power = units.indexOf(unitChar); boolean isSi = unitString.indexOf('i')!=-1; int factor = 1024; if (isSi) { factor = 1000; } return new Double(ret * Math.pow(factor, power)).longValue(); } public static void main(String[] args) { System.out.println(parse("300.00 GiB")); // requires a space System.out.println(parse("300.00 GB")); System.out.println(parse("300.00 B")); System.out.println(parse("300 EB")); } } 
+2
source share

All Articles