@@ -1758,9 +1758,9 @@ public static String byte2FmtString(byte[] bytes, int fmt) {
17581758 case 2 :
17591759 byte [] revBytes = bytes .clone ();
17601760 reverseByteArrayInPlace (revBytes );
1761- return hex2Dec (bytes2Hex (revBytes ));
1761+ return hex2DecBE (bytes2Hex (revBytes ));
17621762 case 1 :
1763- return hex2Dec (bytes2Hex (bytes ));
1763+ return hex2DecBE (bytes2Hex (bytes ));
17641764 }
17651765 return bytes2Hex (bytes );
17661766 }
@@ -1771,7 +1771,7 @@ public static String byte2FmtString(byte[] bytes, int fmt) {
17711771 * @param hex The hexadecimal value to convert.
17721772 * @return String representation of the decimal value of hexString.
17731773 */
1774- public static String hex2Dec (String hex ) {
1774+ public static String hex2DecBE (String hex ) {
17751775 if (!(hex != null && hex .length () % 2 == 0
17761776 && hex .matches ("[0-9A-Fa-f]+" ))) {
17771777 return null ;
@@ -1788,6 +1788,35 @@ public static String hex2Dec(String hex) {
17881788 return ret ;
17891789 }
17901790
1791+ /**
1792+ * Convert a hexadecimal string to a Little Endian decimal string.
1793+ * Uses BigInteger only if the hexadecimal string is longer than 7 bytes.
1794+ * @param hex The hexadecimal value to convert.
1795+ * @return String representation of the decimal value of hexString.
1796+ */
1797+ public static String hex2DecLE (String hex ) {
1798+ if (!(hex != null && hex .length () % 2 == 0
1799+ && hex .matches ("[0-9A-Fa-f]+" ))) {
1800+ return null ;
1801+ }
1802+ // Reverse the byte order (pairs of characters) for Little Endian
1803+ StringBuilder reversedHex = new StringBuilder (hex .length ());
1804+ for (int i = hex .length () - 2 ; i >= 0 ; i -= 2 ) {
1805+ reversedHex .append (hex , i , i + 2 );
1806+ }
1807+ String leHex = reversedHex .toString ();
1808+ String ret ;
1809+ if (leHex == null || leHex .isEmpty ()) {
1810+ ret = "0" ;
1811+ } else if (leHex .length () <= 14 ) {
1812+ ret = Long .toString (Long .parseLong (leHex , 16 ));
1813+ } else {
1814+ BigInteger bigInteger = new BigInteger (leHex , 16 );
1815+ ret = bigInteger .toString ();
1816+ }
1817+ return ret ;
1818+ }
1819+
17911820 /**
17921821 * Convert an array of bytes into a string of hex values.
17931822 * @param bytes Bytes to convert.
@@ -1904,6 +1933,61 @@ public static String bin2Hex(String bin) {
19041933 return hex ;
19051934 }
19061935
1936+ /**
1937+ * Convert a decimal string to a hex string.
1938+ * Uses BigInteger only if the decimal string is longer than 18 digits.
1939+ * @param dec Decimal string to convert.
1940+ * @return Converted hex string.
1941+ */
1942+ public static String decBE2Hex (String dec ) {
1943+ if (!(dec != null && !dec .isEmpty ()
1944+ && dec .matches ("\\ d+" ))) {
1945+ return null ;
1946+ }
1947+ String hex ;
1948+ if (dec .length () <= 18 ) {
1949+ hex = Long .toHexString (Long .parseLong (dec )).toUpperCase ();
1950+ } else {
1951+ BigInteger bigInt = new BigInteger (dec , 10 );
1952+ hex = bigInt .toString (16 ).toUpperCase ();
1953+ }
1954+ // Pad left with a zero if the hex string has an odd length
1955+ if (hex .length () % 2 != 0 ) {
1956+ hex = "0" + hex ;
1957+ }
1958+ return hex ;
1959+ }
1960+
1961+ /**
1962+ * Convert a decimal (Little Endian) to a hex string.
1963+ * Uses BigInteger only if the decimal string is longer than 18 digits.
1964+ * @param dec Decimal string to convert.
1965+ * @return Converted hex string.
1966+ */
1967+ public static String decLE2Hex (String dec ) {
1968+ if (!(dec != null && !dec .isEmpty ()
1969+ && dec .matches ("\\ d+" ))) {
1970+ return null ;
1971+ }
1972+ String hex ;
1973+ if (dec .length () <= 18 ) {
1974+ hex = Long .toHexString (Long .parseLong (dec )).toUpperCase ();
1975+ } else {
1976+ BigInteger bigInt = new BigInteger (dec , 10 );
1977+ hex = bigInt .toString (16 ).toUpperCase ();
1978+ }
1979+ // Pad left with a zero if the hex string has an odd length
1980+ if (hex .length () % 2 != 0 ) {
1981+ hex = "0" + hex ;
1982+ }
1983+ // Reverse the byte order (pairs of characters) for Little Endian
1984+ StringBuilder reversedHex = new StringBuilder (hex .length ());
1985+ for (int i = hex .length () - 2 ; i >= 0 ; i -= 2 ) {
1986+ reversedHex .append (hex , i , i + 2 );
1987+ }
1988+ return reversedHex .toString ();
1989+ }
1990+
19071991 /**
19081992 * Create a colored string.
19091993 * @param data The text to be colored.
0 commit comments