@@ -1904,6 +1904,68 @@ public static String bin2Hex(String bin) {
19041904 return hex ;
19051905 }
19061906
1907+ /**
1908+ * Convert a decimal string to a hex string.
1909+ * @param dec Decimal string to convert.
1910+ * @return Converted hex string.
1911+ */
1912+ public static String dec2Hex (String dec ) {
1913+ String [] parts = dec .split (" " );
1914+ StringBuilder hex = new StringBuilder ();
1915+ for (String part : parts ) {
1916+ int value = Integer .parseInt (part );
1917+ hex .append (String .format ("%02X" , value ));
1918+ }
1919+ return hex .toString ();
1920+ }
1921+
1922+ /**
1923+ * Convert a hex string to a decimal string.
1924+ * @param hex Hex string to convert.
1925+ * @return Converted decimal string.
1926+ */
1927+ public static String hex2Dec (String hex ) {
1928+ StringBuilder dec = new StringBuilder ();
1929+ for (int i = 0 ; i < hex .length (); i += 2 ) {
1930+ int value = Integer .parseInt (hex .substring (i , i + 2 ), 16 );
1931+ if (i > 0 ) {
1932+ dec .append (" " );
1933+ }
1934+ dec .append (value );
1935+ }
1936+ return dec .toString ();
1937+ }
1938+
1939+ /**
1940+ * Check if a string represents decimal bytes (0-255, space-separated).
1941+ * @param dec Decimal string to check.
1942+ * @param context The Context in which an error Toast will be shown.
1943+ * @return True if string is valid decimal, false otherwise.
1944+ */
1945+ public static boolean isDec (String dec , Context context ) {
1946+ if (dec == null || dec .isEmpty ()) {
1947+ Toast .makeText (context , R .string .info_not_dec_data ,
1948+ Toast .LENGTH_LONG ).show ();
1949+ return false ;
1950+ }
1951+ String [] parts = dec .split (" " );
1952+ for (String part : parts ) {
1953+ try {
1954+ int value = Integer .parseInt (part );
1955+ if (value < 0 || value > 255 ) {
1956+ Toast .makeText (context , R .string .info_not_dec_data ,
1957+ Toast .LENGTH_LONG ).show ();
1958+ return false ;
1959+ }
1960+ } catch (NumberFormatException e ) {
1961+ Toast .makeText (context , R .string .info_not_dec_data ,
1962+ Toast .LENGTH_LONG ).show ();
1963+ return false ;
1964+ }
1965+ }
1966+ return true ;
1967+ }
1968+
19071969 /**
19081970 * Create a colored string.
19091971 * @param data The text to be colored.
0 commit comments