Skip to content

Commit 9213243

Browse files
authored
Merge pull request #547 from Real96/add_dec_conversion
Add decimal conversion
2 parents 3ed708d + 122011f commit 9213243

5 files changed

Lines changed: 197 additions & 3 deletions

File tree

Mifare Classic Tool/app/src/main/java/de/syss/MifareClassicTool/Activities/DataConversionTool.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ public class DataConversionTool extends BasicActivity {
4040
EditText mAscii;
4141
EditText mHex;
4242
EditText mBin;
43+
EditText mDecBE;
44+
EditText mDecLE;
4345

4446
/**
4547
* Initialize the some member variables.
@@ -51,6 +53,8 @@ protected void onCreate(Bundle savedInstanceState) {
5153
mAscii = findViewById(R.id.editTextDataConversionToolAscii);
5254
mHex = findViewById(R.id.editTextDataConversionToolHex);
5355
mBin = findViewById(R.id.editTextDataConversionToolBin);
56+
mDecBE = findViewById(R.id.editTextDataConversionToolDecBE);
57+
mDecLE = findViewById(R.id.editTextDataConversionToolDecLE);
5458
}
5559

5660
/**
@@ -75,6 +79,16 @@ public void onConvert(View view) {
7579
if (isBin(bin, this)) {
7680
convertData(Common.bin2Hex(bin));
7781
}
82+
} else if (id == R.id.imageButtonDataConversionToolDecBE) {
83+
String dec = mDecBE.getText().toString();
84+
if (isDec(dec, this)) {
85+
convertData(Common.decBE2Hex(dec));
86+
}
87+
} else if (id == R.id.imageButtonDataConversionToolDecLE) {
88+
String dec = mDecLE.getText().toString();
89+
if (isDec(dec, this)) {
90+
convertData(Common.decLE2Hex(dec));
91+
}
7892
}
7993
}
8094

@@ -100,6 +114,10 @@ private void convertData(String hex) {
100114
}
101115
// Bin.
102116
mBin.setText(Common.hex2Bin(hex));
117+
// Dec (BE).
118+
mDecBE.setText(Common.hex2DecBE(hex));
119+
// Dec (LE).
120+
mDecLE.setText(Common.hex2DecLE(hex));
103121
}
104122

105123
/**
@@ -118,6 +136,21 @@ private boolean isBin(String bin, Context context) {
118136
return false;
119137
}
120138

139+
/**
140+
* Check if a string represents a decimal number.
141+
* @param dec Decimal string to check.
142+
* @param context The Context in which an error Toast will be shown.
143+
* @return True if string is decimal. False otherwise.
144+
*/
145+
private boolean isDec(String dec, Context context) {
146+
if (dec == null || dec.isEmpty() || !dec.matches("\\d+")) {
147+
Toast.makeText(context, R.string.info_not_dec_data,
148+
Toast.LENGTH_LONG).show();
149+
return false;
150+
}
151+
return true;
152+
}
153+
121154
/**
122155
* Open a generic online type converter with the input from {@link #mHex}.
123156
* https://hexconverter.scadacore.com/

Mifare Classic Tool/app/src/main/java/de/syss/MifareClassicTool/Common.java

Lines changed: 87 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -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.

Mifare Classic Tool/app/src/main/res/layout/activity_data_conversion_tool.xml

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,80 @@
140140

141141
</LinearLayout>
142142

143+
<!-- Dec (BE) -->
144+
<LinearLayout
145+
android:layout_width="match_parent"
146+
android:layout_height="match_parent"
147+
android:orientation="horizontal">
148+
149+
<TextView
150+
android:id="@+id/textViewDataConversionToolDecBE"
151+
android:layout_width="wrap_content"
152+
android:layout_height="wrap_content"
153+
android:minEms="4"
154+
android:textAppearance="?android:attr/textAppearanceMedium"
155+
android:gravity="right"
156+
android:paddingRight="5dp"
157+
android:text="@string/text_dec_be"
158+
android:labelFor="@+id/editTextDataConversionToolDecBE" />
159+
160+
<EditText
161+
android:id="@+id/editTextDataConversionToolDecBE"
162+
android:layout_width="0dp"
163+
android:layout_height="wrap_content"
164+
android:layout_weight="1"
165+
android:inputType="number"
166+
android:typeface="monospace"
167+
android:imeOptions="actionDone"
168+
android:importantForAutofill="no" />
169+
170+
<ImageButton
171+
android:id="@+id/imageButtonDataConversionToolDecBE"
172+
android:layout_width="wrap_content"
173+
android:layout_height="wrap_content"
174+
android:src="@android:drawable/stat_notify_sync"
175+
android:contentDescription="@string/text_convert"
176+
android:onClick="onConvert" />
177+
178+
</LinearLayout>
179+
180+
<!-- Dec (LE) -->
181+
<LinearLayout
182+
android:layout_width="match_parent"
183+
android:layout_height="match_parent"
184+
android:orientation="horizontal">
185+
186+
<TextView
187+
android:id="@+id/textViewDataConversionToolDecLE"
188+
android:layout_width="wrap_content"
189+
android:layout_height="wrap_content"
190+
android:minEms="4"
191+
android:textAppearance="?android:attr/textAppearanceMedium"
192+
android:gravity="right"
193+
android:paddingRight="5dp"
194+
android:text="@string/text_dec_le"
195+
android:labelFor="@+id/editTextDataConversionToolDecLE" />
196+
197+
<EditText
198+
android:id="@+id/editTextDataConversionToolDecLE"
199+
android:layout_width="0dp"
200+
android:layout_height="wrap_content"
201+
android:layout_weight="1"
202+
android:inputType="number"
203+
android:typeface="monospace"
204+
android:imeOptions="actionDone"
205+
android:importantForAutofill="no" />
206+
207+
<ImageButton
208+
android:id="@+id/imageButtonDataConversionToolDecLE"
209+
android:layout_width="wrap_content"
210+
android:layout_height="wrap_content"
211+
android:src="@android:drawable/stat_notify_sync"
212+
android:contentDescription="@string/text_convert"
213+
android:onClick="onConvert" />
214+
215+
</LinearLayout>
216+
143217
<TextView
144218
android:id="@+id/textViewDataConversionToolMoreOptions"
145219
android:layout_width="wrap_content"

Mifare Classic Tool/app/src/main/res/values-it/strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,7 @@
350350
<string name="info_block_out_of_range">Errore: Il blocco non è compreso nell\'intervallo</string>
351351
<string name="info_not_hex_data">Errore: I dati devono essere in esadecimale (0-9 e A-F)</string>
352352
<string name="info_not_bin_data">Errore: I dati devono essere in binario (0/1, multipli di 8)</string>
353+
<string name="info_not_dec_data">Errore: I dati devono essere in decimale (0-9)</string>
353354
<string name="info_not_16_byte">Errore: I dati devono essere lunghi esattamente 16 byte (32 caratteri)</string>
354355
<string name="info_is_not_vb">Errore: Non è un Value Block</string>
355356
<string name="info_block_not_in_sector">Errore: Il settore non contiene un blocco col numero dato.</string>

Mifare Classic Tool/app/src/main/res/values/strings.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,8 @@
371371
(0-9 and A-F)</string>
372372
<string name="info_not_bin_data">Error: Data must be in binary
373373
(0/1, multiple of 8)</string>
374+
<string name="info_not_dec_data">Error: Data must be in decimal
375+
(0-9)</string>
374376
<string name="info_not_16_byte">Error: Data must be 16 bytes
375377
(32 characters) long</string>
376378
<string name="info_is_not_vb">Error: Not a value block.</string>

0 commit comments

Comments
 (0)