X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fnet%2Fpterodactylus%2Futil%2Fnumber%2FHex.java;h=246fa419138b61cd21c2946402775aa29d83feab;hb=f09a940c1098882fb137ad0b96e8bc9835170d3f;hp=09bf2b06a99fa60dd525572645dcfe9dac45b79b;hpb=cf127faffabd3f07b0acc6de9ea746059d571b56;p=jSite2.git diff --git a/src/net/pterodactylus/util/number/Hex.java b/src/net/pterodactylus/util/number/Hex.java index 09bf2b0..246fa41 100644 --- a/src/net/pterodactylus/util/number/Hex.java +++ b/src/net/pterodactylus/util/number/Hex.java @@ -21,25 +21,44 @@ package net.pterodactylus.util.number; /** * Conversion routines for handling hexadecimal numbers. - * + * * @author David ‘Bombe’ Roden <bombe@freenetproject.org> */ public class Hex { /** * Converts the given byte array to a hexadecimal string. - * + * * @param dataBytes * The bytes to convert * @return A string of hexadecimal numbers */ public static String toHex(byte[] dataBytes) { StringBuilder hexString = new StringBuilder(dataBytes.length * 2); - for (byte dataByte: dataBytes) { + for (byte dataByte : dataBytes) { String hexByte = "0" + Integer.toHexString(dataByte & 0xff); hexString.append(hexByte.substring(hexByte.length() - 2)); } return hexString.toString(); } + /** + * Converts the given value to a hexadecimal string and zero-pads it to the + * given number of digits. + * + * @param value + * The value to convert + * @param digits + * The number of digits + * @return The formatted hexadecimal value + */ + public static String toHex(long value, int digits) { + StringBuilder hexString = new StringBuilder(); + hexString.append(Long.toHexString(value)); + while (hexString.length() < digits) { + hexString.insert(0, '0'); + } + return hexString.toString(); + } + }