Skip to content

Commit 98f5e99

Browse files
committed
Add decimal conversion
1 parent 9cf3a58 commit 98f5e99

4 files changed

Lines changed: 110 additions & 0 deletions

File tree

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ public class DataConversionTool extends BasicActivity {
4040
EditText mAscii;
4141
EditText mHex;
4242
EditText mBin;
43+
EditText mDec;
4344

4445
/**
4546
* Initialize the some member variables.
@@ -51,6 +52,7 @@ protected void onCreate(Bundle savedInstanceState) {
5152
mAscii = findViewById(R.id.editTextDataConversionToolAscii);
5253
mHex = findViewById(R.id.editTextDataConversionToolHex);
5354
mBin = findViewById(R.id.editTextDataConversionToolBin);
55+
mDec = findViewById(R.id.editTextDataConversionToolDec);
5456
}
5557

5658
/**
@@ -75,6 +77,11 @@ public void onConvert(View view) {
7577
if (isBin(bin, this)) {
7678
convertData(Common.bin2Hex(bin));
7779
}
80+
} else if (id == R.id.imageButtonDataConversionToolDec) {
81+
String dec = mDec.getText().toString();
82+
if (Common.isDec(dec, this)) {
83+
convertData(Common.dec2Hex(dec));
84+
}
7885
}
7986
}
8087

@@ -100,6 +107,8 @@ private void convertData(String hex) {
100107
}
101108
// Bin.
102109
mBin.setText(Common.hex2Bin(hex));
110+
// Dec.
111+
mDec.setText(Common.hex2Dec(hex));
103112
}
104113

105114
/**

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

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

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

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

141141
</LinearLayout>
142142

143+
<!-- Dec -->
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/textViewDataConversionToolDec"
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"
158+
android:labelFor="@+id/editTextDataConversionToolDec" />
159+
160+
<EditText
161+
android:id="@+id/editTextDataConversionToolDec"
162+
android:layout_width="0dp"
163+
android:layout_height="wrap_content"
164+
android:layout_weight="1"
165+
android:inputType="numberSigned"
166+
android:typeface="monospace"
167+
android:imeOptions="actionDone"
168+
android:importantForAutofill="no" />
169+
170+
<ImageButton
171+
android:id="@+id/imageButtonDataConversionToolDec"
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+
143180
<TextView
144181
android:id="@+id/textViewDataConversionToolMoreOptions"
145182
android:layout_width="wrap_content"

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,8 @@
200200
<string name="text_ascii">ASCII</string>
201201
<string name="text_hex">Hex</string>
202202
<string name="text_bin">Bin</string>
203+
<string name="text_dec">Dec</string>
204+
<string name="info_not_dec_data">Invalid decimal format</string>
203205
<string name="text_dec_be">Dec (BE)</string>
204206
<string name="text_dec_le">Dec (LE)</string>
205207
<string name="text_invalid_ac">Access Conditions are invalid</string>

0 commit comments

Comments
 (0)