NFC读取卡号转换程RFID设备IC读卡器读取的卡号

NFC设备读取的IC卡号,一般为16进制形式字节数组,然后转换成十六进制字符串。

而IC读卡器读取的卡号为10进制的字符串,在位数不足十位的情况下会在字符串头部自动补0,然后会在字符串两边添加特定的标识符,如换行、空格、分号等字符。

要想把NFC读取的卡号转换和IC读取的卡号相同就需要知道NFC读卡的规则

如NFC读取的卡号为41ef5664  

在转换成10进制的时候  需要把NFC读出的字符串每两位翻转

41 ef 56 64  -> 64 56 ef 41

这时候 这用IC读卡器读取卡号 1683418945

计算转换后的字符串 -> 转换程10进制   -> 1683418945

两者相同!

相关代码如下:

把NFC读取到卡号字符串数组转换程十六进制字符串

      
	public static String bytes2HexString(byte[] src) {
		StringBuilder builder = new StringBuilder();
		if (src == null || src.length <= 0) {
			return null;
		}
		char[] buffer = new char[2];
		for (int i = 0; i < src.length; i++) {
			buffer[0] = Character.forDigit((src[i] >>> 4) & 0x0F, 16);
			buffer[1] = Character.forDigit(src[i] & 0x0F, 16);
			builder.append(buffer);
		}
		return builder.toString();
	}

把十六进制字符串翻转后,转换程十进制字符串

	public static String hex2Decimal(String hex) {
		StringBuilder builder = new StringBuilder();
		if(hex.length() == 8) {
			for(int i = 0; i<4; i++) {
				String str = hex.substring(hex.length()-2 * (i+1), hex.length()-2*i);
				builder.append(str);
			}
		}
		String decimal = String.valueOf(Long.parseLong(builder.toString(), 16));
		while (decimal.length() < 10) {
			decimal = "0" + decimal;
		}
		
		return decimal;
	}




  • 5
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
以下是一个简单的安卓 NFC 读取卡号的示例代码: 首先,确保在 AndroidManifest.xml 文件中添加了以下权限: ``` <uses-permission android:name="android.permission.NFC" /> ``` 然后,在相应的 Activity 中添加以下代码: ```java public class MainActivity extends Activity { private NfcAdapter nfcAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 获取 NFC 适配器 nfcAdapter = NfcAdapter.getDefaultAdapter(this); if (nfcAdapter == null) { Toast.makeText(this, "设备不支持 NFC", Toast.LENGTH_SHORT).show(); finish(); return; } // 检查 NFC 是否开启 if (!nfcAdapter.isEnabled()) { Toast.makeText(this, "请在系统设置中先启用 NFC 功能", Toast.LENGTH_SHORT).show(); finish(); return; } // 创建 PendingIntent 对象,并在检测到卡片时调用 onNewIntent 方法 PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0); IntentFilter filter = new IntentFilter(NfcAdapter.ACTION_TAG_DISCOVERED); IntentFilter[] filters = new IntentFilter[]{filter}; nfcAdapter.enableForegroundDispatch(this, pendingIntent, filters, null); } @Override protected void onResume() { super.onResume(); // 检查 NFC 是否开启 if (nfcAdapter != null && !nfcAdapter.isEnabled()) { Toast.makeText(this, "请在系统设置中先启用 NFC 功能", Toast.LENGTH_SHORT).show(); finish(); return; } } @Override protected void onPause() { super.onPause(); // 关闭前台调度 if (nfcAdapter != null) { nfcAdapter.disableForegroundDispatch(this); } } @Override protected void onNewIntent(Intent intent) { super.onNewIntent(intent); String cardNumber = ByteArrayToHexString(intent.getByteArrayExtra(NfcAdapter.EXTRA_ID)); Toast.makeText(this, "卡号: " + cardNumber, Toast.LENGTH_SHORT).show(); } /** * 将字节数组转换为十六进制字符串 * @param bytes 字节数组 * @return 十六进制字符串 */ private String ByteArrayToHexString(byte[] bytes) { StringBuilder sb = new StringBuilder(); for (byte aByte : bytes) { String hex = Integer.toHexString(aByte & 0xFF); if (hex.length() == 1) { sb.append('0'); } sb.append(hex); } return sb.toString().toUpperCase(); } } ``` 在 AndroidManifest.xml 文件中,需要添加以下声明: ```xml <activity android:name=".MainActivity" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> <intent-filter> <action android:name="android.nfc.action.TAG_DISCOVERED" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </activity> ``` 以上代码中,主要实现了以下功能: 1. 获取 NFC 适配器,并检查设备是否支持 NFC。 2. 检查 NFC 是否开启。 3. 创建 PendingIntent 对象,并在检测到卡片时调用 onNewIntent 方法。 4. 将字节数组转换为十六进制字符串,获取卡号并显示在 Toast 中。 实际使用时,还需要根据具体的需求进行修改和完善。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值