<pre>public class HexStrUtils {
public static byte[] hexStringToBytes(String hexString) {
if (hexString == null || hexString.equals("")) {
return null;
}
hexString = hexString.toUpperCase(Locale.getDefault());
int length = hexString.length() / 2;
char[] hexChars = hexString.toCharArray();
byte[] d = new byte[length];
for (int i = 0; i < length; i++) {
int pos = i * 2;
d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));
}
return d;
}
private static byte charToByte(char c) {
return (byte) "0123456789ABCDEF".indexOf(c);
}
/**
* 数组转换成十六进制字符串
*
* @return HexString
*/
public static final String bytesToHexString(byte[] bArray) {
if (bArray == null) {
return "";
}
StringBuffer sb = new StringBuffer(bArray.length);
String sTemp;
for (int i = 0; i < bArray.length; i++) {
sTemp = Integer.toHexString(0xFF & bArray[i]);
if (sTemp.length() < 2)
sb.append(0);
sb.append(sTemp.toUpperCase(Locale.getDefault()));
}
return sb.toString();
}
public static final String splitBytesString(String byteString) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < byteString.length(); i++) {
sb.append(byteString.charAt(i));
if (sb.length() % 3 == 0 && sb.charAt(sb.length() - 1) != ' ') {
sb.insert(sb.length() - 1, ' ');
}
}
return sb.toString();
}
// 将10进制的asc码字符串转化为16进制的字符串
public static String ascStr2hexStr(String asc) {
if (TextUtils.isEmpty(asc)) {
return "";
}
StringBuffer buffer = new StringBuffer();
asc = asc.toUpperCase(Locale.getDefault());
String[] datas = asc.split(" ");
for (int i = 0; i < datas.length; i++) {
try {
String num = Integer.toHexString(Integer.parseInt(datas[i]));
buffer.append(num);
} catch (Exception e) {
e.printStackTrace();
}
}
return buffer.toString();
}
// 16进制数字字符串转换成10进制的数字字符串
public static String hexStringToStr(String hexString) {
if (hexString == null || hexString.equals("")) {
return null;
}
StringBuffer stringBuffer = new StringBuffer();
hexString = hexString.toUpperCase(Locale.getDefault());
int length = hexString.length();
char[] hexChars = hexString.toCharArray();
for (int i = 0; i < length; i += 2) {
try {
int parseInt = Integer.parseInt(hexString.substring(i, i + 2), 16);
stringBuffer.append(String.format("%02d", parseInt));
} catch (Exception e) {
e.printStackTrace();
}
}
return stringBuffer.toString();
}
// 字符串转化成ASCII码对应数字
public static String stringToAsciiNumStr(String value) {
StringBuffer sbu = new StringBuffer();
char[] chars = value.toCharArray();
for (int i = 0; i < chars.length; i++) {
if (i != chars.length - 1) {
sbu.append((int) chars[i]).append(" ");
} else {
sbu.append((int) chars[i]);
}
}
return sbu.toString();
}
// (二位)十进制字符串转化为ASCII字符
public static String stringToAscChar(String str) {
if (str == null || str.equals("")) {
return "";
}
str = str.toUpperCase(Locale.getDefault());
StringBuffer buffer = new StringBuffer();
char[] chars = str.toCharArray();
int length = chars.length;
byte[] d = new byte[length];
for (int i = 0; i < length; i += 2) {
int num = Integer.parseInt(String.valueOf(chars[i]) + String.valueOf(chars[i + 1]));
// d[i] = (byte) num;
char c = (char) num;
buffer.append(c);
}
return buffer.toString();
}
// 1. WGS84:为一种大地坐标系,也是目前广泛使用的GPS全球卫星定位系统使用的坐标系;
//
// 2. GCJ02:是由中国国家测绘局制订的地理信息系统的坐标系统,是由WGS84坐标系经加密后的坐标系;
//
// 3. BD09:百度坐标系,在GCJ02坐标系基础上再次加密。其中BD09ll表示百度经纬度坐标,BD09mc表示百度墨卡托米制坐标。
//
// 百度地图SDK在国内(包括港澳台)使用的是BD09坐标(定位SDK默认使用GCJ02坐标);在海外地区,统一使用WGS84坐标。开发者在使用百度地图相关服务时,请注意选择。
// 转换坐标
public static LatLng convertXY(LatLng sourceLatLng) {
// 将GPS设备采集的原始GPS坐标转换成百度坐标
CoordinateConverter converter = new CoordinateConverter();
converter.from(CoordinateConverter.CoordType.GPS);
// sourceLatLng待转换坐标
converter.coord(sourceLatLng);
LatLng desLatLng = converter.convert();
Log.i("tag", "convert xy " + desLatLng.toString());
return desLatLng;
}
// 字符串转化成ASCII
public static String stringToAscii(String value) {
StringBuffer sbu = new StringBuffer();
char[] chars = value.toCharArray();
for (int i = 0; i < chars.length; i++) {
if(i != chars.length - 1) {
sbu.append((int)chars[i]).append(" ");
} else {
sbu.append((int)chars[i]);
}
}
return sbu.toString();
}
/**
* 十六进制字符串转换字符串
*
* @param hexStr Byte字符串(Byte之间无分隔符 如:[616C6B])
* @return String 对应的字符串
*/
public static String hexStr2Str(String hexStr) {
String str = "0123456789ABCDEF";
char[] hexs = hexStr.toCharArray();
byte[] bytes = new byte[hexStr.length() / 2];
int n;
for (int i = 0; i < bytes.length; i++) {
n = str.indexOf(hexs[2 * i]) * 16;
n += str.indexOf(hexs[2 * i + 1]);
bytes[i] = (byte) (n & 0xff);
}
return new String(bytes);
}
/**
* 普通数值字符串转换成16进制
*/
public static String Str2Hex(String original) {
byte[] bytes = original.getBytes();
Log.i("tag", "bytes " + new String(bytes));
String hs = "";
for (int n = 0; n < bytes.length; n++) {
hs += (Integer.toHexString(bytes[n] & 0XFF));
}
Log.i("tag", "hs " + hs);
return hs.toUpperCase();
}
public static String unicodeToString(String hex) {
int t = hex.length() / 6;
StringBuilder str = new StringBuilder();
for (int i = 0; i < t; i++) {
String s = hex.substring(i * 6, (i + 1) * 6);
// 高位需要补上00再转
String s1 = s.substring(2, 4) + "00";
// 低位直接转
String s2 = s.substring(4);
// 将16进制的string转为int
int n = Integer.valueOf(s1, 16) + Integer.valueOf(s2, 16);
// 将int转换为字符
char[] chars = Character.toChars(n);
str.append(new String(chars));
}
return str.toString();
}
//高位在前,低位在后
public static byte[] int2bytes(int num) {
byte[] result = new byte[4];
result[0] = (byte) ((num >>> 24) & 0xff);//说明一
result[1] = (byte) ((num >>> 16) & 0xff);
result[2] = (byte) ((num >>> 8) & 0xff);
result[3] = (byte) ((num >>> 0) & 0xff);
Log.i("tag", "num return " + result[0]);
return result;
}
//高位在前,低位在后
public static int bytes2int(byte[] bytes) {
int result = 0;
if (bytes.length == 4) {
int a = (bytes[0] & 0xff) << 24;//说明二
int b = (bytes[1] & 0xff) << 16;
int c = (bytes[2] & 0xff) << 8;
int d = (bytes[3] & 0xff);
result = a | b | c | d;
}
return result;
}
// 格式化当前时间
private String getTime() {
Calendar calendar = Calendar.getInstance();
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
return format.format(calendar.getTime());
}
// 主机地址和端口
private static final String HOST = "47.93.198.36";
private static final int PORT = 8011;
private Socket socket;
private OutputStream outputStream;
private DataOutputStream dataOutputStream;
// udp方式连接主机
private void udpConnect() {
try {
if (null == socket) {
socket = new Socket(HOST, PORT);
// 获取Socket的OutputStream对象用于发送数据。
outputStream = socket.getOutputStream();
dataOutputStream = new DataOutputStream(outputStream);
}
sendSubscribe();
while (true) {
try {
rcvLen = dataInputStream.read(buff);
if (0 != rcvLen) {
rcvMsg = new String(buff, 0, rcvLen, "utf-8");
Log.i("tag", "run: 收到消息:" + rcvMsg);
if (rcvMsg.contains("00")) {
convertRecvMsg(rcvMsg.replace(" ", ""));
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
} catch (SocketException e) {
e.printStackTrace();
// myToast("连接主机失败");
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
// 发送订阅报文
private void sendSubscribe() {
if (SPHelper.getUserID().equals(HexStrUtils.splitBytesString(HexStrUtils.bytesToHexString(data_RFID)))) {
Log.i("tag", "different rfid " + HexStrUtils.splitBytesString(HexStrUtils.bytesToHexString(data_RFID)));
mHandler.sendEmptyMessage(handler_key.SHUA_KA.ordinal());
} else {
Log.i("tag", "new RFID " + HexStrUtils.splitBytesString(HexStrUtils.bytesToHexString(data_RFID)));
SPHelper.setUserID(HexStrUtils.splitBytesString(HexStrUtils.bytesToHexString(data_RFID)));
}
String realFRID = getRFID(HexStrUtils.bytesToHexString(data_RFID));
Log.i("tag", "realRFID " + realFRID);
sendDataBute(HexStrUtils.hexStringToBytes("02" + realFRID), true);
}
// 向主机发送数据
private void sendData(String dataStr, boolean isSub) {
if (null == socket || null == outputStream) return;
try {
// dataOutputStream.writeBytes(dataStr);
Log.i("tag","dataStr "+dataStr);
dataOutputStream.write(HexStrUtils.hexStringToBytes(dataStr));
dataOutputStream.flush();
if (isSub) {
isMonitor = true;
}
} catch (IOException e) {
e.printStackTrace();
}
}
// 向主机发送数据
private void sendDataBute(byte[] dataStr, boolean isSub) {
if (null == socket || null == outputStream) return;
try {
Log.i("tag","dataStr "+dataStr);
dataOutputStream.write(dataStr);
dataOutputStream.flush();
if (isSub) {
isMonitor = true;
}
} catch (IOException e) {
e.printStackTrace();
}
}
}</pre>
代码片段: UDP-gistfile1.txt
public class HexStrUtils {
public static byte[] hexStringToBytes(String hexString) {
if (hexString == null || hexString.equals("")) {
return null;...
- 发表于 2018-07-05 16:20
- 阅读 ( 485 )
- 分类:代码片段
你可能感兴趣的文章
- Java 添加条码、二维码到PDF文档 134 浏览
- Java 在Excel中添加筛选器并执行筛选 91 浏览
- JAVA + VUE + 简洁UI:分离式软件平台形成之旅 265 浏览
- Java 设置Word中的表格自适应的3种方式 113 浏览
- LeaRun快速开发平台,.net/java项目开发工具简析 142 浏览
- JVM学习笔记之类装载器-ClassLoader 203 浏览
随机文章
- Unable to start EmbeddedWebApplicationContext due to missing EmbeddedServletContainerFactory bean. 4324 浏览
- Spring Boot源码分析——自动配置 3021 浏览
- No auto configuration classes found in META-IN F/spring.factories. If you are using a custom packaging, make sure that file is correct. 7957 浏览
- 修改spring Boot启动时的默认图案Banner 1515 浏览
- SpringBoot中修改启动端口的方式 1111 浏览
相关问题
- java基础知识提问 1 回答
条评论
请先 登录 后评论
发送私信
如果觉得我的文章对您有用,请随意打赏。你的支持将鼓励我继续创作!