需求:将一串十六进制字符串,弄成十进制
1.截取特定位数的字符串,比如去除 前面的标识符和后面的校验码,只需要中间部分
2.拿到十六进制字符串
3.将十六进制字符串转成byte 字节数组
4.调换字节数组的顺序,
4.将新字节数组转成十进制字符串
例如:
原始字符串是:017CC411F700000000
去除无效标识符和校验码后,得到的字符串:7CC411F7
转成字符数组: 7C,C4,11,F7
调换顺序: F7 ,11, C4 ,7C
将 F711C47C 转成十进制字符串:4145136764
为什么要搞那么复杂呢?
因为我在弄一个读卡的需求,读出来的就是一串十六进制字符串,然后我们的卡号是十进制的一串,必须要把十六进制转成十进制,然后去我们的数据库里面,根据卡号,比对,查询信息,得到卡号对应的人员信息
package com.example.iosdialogdemo;import static java.util.Collections.reverse;import android.os.Bundle;
import android.util.Log;import com.example.iosdialogdemo.utils.DataChangeUtils;import androidx.appcompat.app.AppCompatActivity;import com.example.iosdialogdemo.utils.DataConversion;
import com.example.iosdialogdemo.utils.MyFunc;
import com.example.iosdialogdemo.utils.HexUtil;import com.example.iosdialogdemo.utils.DataChangeUtils;import java.util.Arrays;public class TwoActivity extends AppCompatActivity {private String code1 = "ECD8FCF6";private String code2 = "7CC411F7";private String code3 = "01ECD8FCF600000000";private String code4 = "017CC411F700000000";@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_first);/** 0 - 1 两位byte 代表读卡和卡离开的字段* 2-9 代表卡号*10-17 代表校验码* EC D8 FC F6 4143765740* 7C C4 11 F7 4145136764* * *//*String code3 = DataChangeUtils.createHex(code1);String byteArrToHex = MyFunc.ByteArrToHex(null);Log.e("TAG", ""+code5);需要做什么呢 ?30 48十六进制转十进制可以了、接下来是拆分数组 然后 调换位置00 7C C4 11 F7 00 00 00 00调换可以了 接下来 就是转十进制*//// bytesToHex( hexTobytes(code1));String str = code3.substring(2, 9);long a = DataConversion.hexToDec("30");byte[] b = DataConversion.decodeHexString(code1);int[] array = {7, 8, 9, 10, 11};Log.e("Array : ", Arrays.toString(array)); // print array// Call function to get reversed arrayint[] reversedArray = reverse(array);byte[] reversedArray2 = reverse2(b);Log.e("Reversed array : ", Arrays.toString(reversedArray));Log.e("Reversed array_two : ", Arrays.toString(b));Log.e("Reversed array_two : ", Arrays.toString(reversedArray2));String coede6= DataConversion.encodeHexString(reversedArray2);long codessss= DataConversion.hexToDec(coede6);Log.e("TAG", "" + a +"====="+coede6+"==???==="+codessss+"------------"+str); // F6FCD8EC}static int[] reverse(int[] array) {int[] newArray = new int[array.length];for (int i = 0; i < array.length; i++) {newArray[array.length - 1 - i] = array[i];}return newArray;}static byte[] reverse2(byte[] array) {byte[] newArray = new byte[array.length];for (int i = 0; i < array.length; i++) {newArray[array.length - 1 - i] = array[i];}return newArray;}}
package com.example.iosdialogdemo;import static java.util.Collections.reverse;import android.os.Bundle;
import android.util.Log;import com.example.iosdialogdemo.utils.DataChangeUtils;import androidx.appcompat.app.AppCompatActivity;import com.example.iosdialogdemo.utils.DataConversion;
import com.example.iosdialogdemo.utils.MyFunc;
import com.example.iosdialogdemo.utils.HexUtil;import com.example.iosdialogdemo.utils.DataChangeUtils;import java.util.Arrays;public class TwoActivity extends AppCompatActivity {
// private String code1 = "ECD8FCF6";private String code2 = "7CC411F7";private String code3 = "01ECD8FCF600000000";private String code4 = "017CC411F700000000";@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_first);String str = code3.substring(2, 10);byte[] b = DataConversion.decodeHexString(str);byte[] reversedArray2 = reverse2(b);String coede6= DataConversion.encodeHexString(reversedArray2);long codessss= DataConversion.hexToDec(coede6);Log.e("TAG", "" +"====="+coede6+"==???==="+codessss+"------------"+str); // F6FCD8EC}static byte[] reverse2(byte[] array) {byte[] newArray = new byte[array.length];for (int i = 0; i < array.length; i++) {newArray[array.length - 1 - i] = array[i];}return newArray;}
}
package com.example.iosdialogdemo;import static java.util.Collections.reverse;import android.os.Bundle;
import android.util.Log;import com.example.iosdialogdemo.utils.DataChangeUtils;import androidx.appcompat.app.AppCompatActivity;import com.example.iosdialogdemo.utils.DataConversion;
import com.example.iosdialogdemo.utils.MyFunc;
import com.example.iosdialogdemo.utils.HexUtil;import com.example.iosdialogdemo.utils.DataChangeUtils;import java.util.Arrays;public class TwoActivity extends AppCompatActivity {
// private String code1 = "ECD8FCF6";private String code2 = "7CC411F7";private String code3 = "01ECD8FCF600000000";private String code4 = "017CC411F700000000";@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_first);long intCode3= DataConversion.hexToDec(DataConversion.encodeHexString(reverse2(DataConversion.decodeHexString(code3.substring(2, 10)))));long intCode4= DataConversion.hexToDec(DataConversion.encodeHexString(reverse2(DataConversion.decodeHexString(code4.substring(2, 10)))));Log.e("TAG", intCode3+"========="+intCode4); // F6FCD8EC}static byte[] reverse2(byte[] array) {byte[] newArray = new byte[array.length];for (int i = 0; i < array.length; i++) {newArray[array.length - 1 - i] = array[i];}return newArray;}
}
package com.example.iosdialogdemo;import static java.util.Collections.reverse;import android.os.Bundle;
import android.util.Log;import com.example.iosdialogdemo.utils.DataChangeUtils;import androidx.appcompat.app.AppCompatActivity;import com.example.iosdialogdemo.utils.DataConversion;
import com.example.iosdialogdemo.utils.MyFunc;
import com.example.iosdialogdemo.utils.HexUtil;import com.example.iosdialogdemo.utils.DataChangeUtils;import java.util.Arrays;public class TwoActivity extends AppCompatActivity {
// private String code1 = "ECD8FCF6";private String code2 = "7CC411F7";private String code3 = "01ECD8FCF600000000";private String code4 = "017CC411F700000000";@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_first);long intCode3= DataConversion.hexToDec(DataConversion.encodeHexString(DataConversion.reverse2(DataConversion.decodeHexString(code3.substring(2, 10)))));long intCode4= DataConversion.hexToDec(DataConversion.encodeHexString(DataConversion.reverse2(DataConversion.decodeHexString(code4.substring(2, 10)))));Log.e("TAG", intCode3+"===??======"+intCode4); // F6FCD8EC}}
package com.example.iosdialogdemo.utils;/*** 数据转换工具** @author :frey* @date:2019/4/03 16:15*/
public class DataConversion {/*** 判断奇数或偶数,位运算,最后一位是1则为奇数,为0是偶数** @param num* @return*/public static int isOdd(int num) {return num & 0x1;}/*** 将int转成byte** @param number* @return*/public static byte intToByte(int number) {return hexToByte(intToHex(number));}/*** 将int转成hex字符串** @param number* @return*/public static String intToHex(int number) {String st = Integer.toHexString(number).toUpperCase();return String.format("%2s", st).replaceAll(" ", "0");}/*** 字节转十进制** @param b* @return*/public static int byteToDec(byte b) {String s = byteToHex(b);return (int) hexToDec(s);}/*** 字节数组转十进制** @param bytes* @return*/public static int bytesToDec(byte[] bytes) {String s = encodeHexString(bytes);return (int) hexToDec(s);}/*** Hex字符串转int** @param inHex* @return*/public static int hexToInt(String inHex) {return Integer.parseInt(inHex, 16);}/*** 字节转十六进制字符串** @param num* @return*/public static String byteToHex(byte num) {char[] hexDigits = new char[2];hexDigits[0] = Character.forDigit((num >> 4) & 0xF, 16);hexDigits[1] = Character.forDigit((num & 0xF), 16);return new String(hexDigits).toUpperCase();}/*** 十六进制转byte字节** @param hexString* @return*/public static byte hexToByte(String hexString) {int firstDigit = toDigit(hexString.charAt(0));int secondDigit = toDigit(hexString.charAt(1));return (byte) ((firstDigit << 4) + secondDigit);}private static int toDigit(char hexChar) {int digit = Character.digit(hexChar, 16);if (digit == -1) {throw new IllegalArgumentException("Invalid Hexadecimal Character: " + hexChar);}return digit;}/*** 字节数组转十六进制** @param byteArray* @return*/public static String encodeHexString(byte[] byteArray) {if (byteArray == null) {return "";}StringBuffer hexStringBuffer = new StringBuffer();for (int i = 0; i < byteArray.length; i++) {hexStringBuffer.append(byteToHex(byteArray[i]));}return hexStringBuffer.toString().toUpperCase();}/*** 十六进制转字节数组** @param hexString* @return*/public static byte[] decodeHexString(String hexString) {if (hexString.length() % 2 == 1) {throw new IllegalArgumentException("Invalid hexadecimal String supplied.");}byte[] bytes = new byte[hexString.length() / 2];for (int i = 0; i < hexString.length(); i += 2) {bytes[i / 2] = hexToByte(hexString.substring(i, i + 2));}return bytes;}/*** 十进制转十六进制** @param dec* @return*/public static String decToHex(int dec) {String hex = Integer.toHexString(dec);if (hex.length() == 1) {hex = '0' + hex;}return hex.toLowerCase();}/*** 十六进制转十进制** @param hex* @return*/public static long hexToDec(String hex) {return Long.parseLong(hex, 16);}public static byte[] reverse2(byte[] array) {byte[] newArray = new byte[array.length];for (int i = 0; i < array.length; i++) {newArray[array.length - 1 - i] = array[i];}return newArray;}}