From 44a284dabcf8bbe68ad0c67abbee41a62ca7277f Mon Sep 17 00:00:00 2001 From: wangh <123456> Date: Thu, 4 Jul 2024 15:27:18 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=20ascii=20=E8=BD=AC=20?= =?UTF-8?q?=E6=98=8E=E6=96=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../jingyuanmesc5106/MainActivity.java | 3 +- .../jingyuanmesc5106/base/BaseActivity.java | 4 +- .../jingyuanmesc5106/uitls/ASCIIUtil.java | 66 +++++++++++++++++++ 3 files changed, 70 insertions(+), 3 deletions(-) create mode 100644 app/src/main/java/com/example/jingyuanmesc5106/uitls/ASCIIUtil.java diff --git a/app/src/main/java/com/example/jingyuanmesc5106/MainActivity.java b/app/src/main/java/com/example/jingyuanmesc5106/MainActivity.java index 5e8f414..c50a39e 100644 --- a/app/src/main/java/com/example/jingyuanmesc5106/MainActivity.java +++ b/app/src/main/java/com/example/jingyuanmesc5106/MainActivity.java @@ -17,6 +17,7 @@ import android.widget.TextView; import android.widget.Toast; import com.example.jingyuanmesc5106.base.BaseActivity; +import com.example.jingyuanmesc5106.uitls.ASCIIUtil; import com.example.jingyuanmesc5106.uitls.MyAlertDialog; import com.example.jingyuanmesc5106.uitls.MyRecultCall; import com.example.jingyuanmesc5106.uitls.MyResult; @@ -65,7 +66,7 @@ public class MainActivity extends BaseActivity implements DialogCall { music.start(); String string = Tools.Bytes2HexString(readBytes, readBytes.length); Log.e("TAG", "san_epc:" + string) ; - palletInfoCode.setText(string); + palletInfoCode.setText(ASCIIUtil.hex2Str(string)); } } //提交 diff --git a/app/src/main/java/com/example/jingyuanmesc5106/base/BaseActivity.java b/app/src/main/java/com/example/jingyuanmesc5106/base/BaseActivity.java index 880b12e..70f2837 100644 --- a/app/src/main/java/com/example/jingyuanmesc5106/base/BaseActivity.java +++ b/app/src/main/java/com/example/jingyuanmesc5106/base/BaseActivity.java @@ -35,8 +35,8 @@ import okhttp3.MediaType; public class BaseActivity extends AppCompatActivity { - public static String url = "http://175.27.215.92:7018/prod-api"; -// public static String url = "http://10.11.40.120:7018/dev-api"; +// public static String url = "http://172.16.12.100:7018/prod-api"; + public static String url = "http://10.10.3.63:7018/dev-api"; public ProgressDialog dialog; public Gson gson; public MediaType JSON = MediaType.parse("application/json; charset=utf-8"); diff --git a/app/src/main/java/com/example/jingyuanmesc5106/uitls/ASCIIUtil.java b/app/src/main/java/com/example/jingyuanmesc5106/uitls/ASCIIUtil.java new file mode 100644 index 0000000..eb745e1 --- /dev/null +++ b/app/src/main/java/com/example/jingyuanmesc5106/uitls/ASCIIUtil.java @@ -0,0 +1,66 @@ +package com.example.jingyuanmesc5106.uitls; + +/** + * @author: wangh + * @description: Ass + * @date: 2019/06/20-15:18 + */ +public class ASCIIUtil { + + // String明文转ASCII码hex字符串,一个明文字符生成两个字符表示的16进制ASCII码 + public static String str2Hex(String str) { + StringBuilder sb = new StringBuilder(); + for (int i = 0; i < str.length(); i++) { + char c = str.charAt(i); + // 这里的第二个参数16表示十六进制 + sb.append(Integer.toString(c, 16)); + // 或用toHexString方法直接转成16进制 + // sb.append(Integer.toHexString(c)); + } + return sb.toString(); + } + + // ASCII码hex字符串转String明文 + public static String hex2Str(String hex) { + StringBuilder sb = new StringBuilder(); + for (int i = 0; i < hex.length() - 1; i += 2) { + String h = hex.substring(i, (i + 2)); + int decimal = Integer.parseInt(h, 16); + sb.append((char) decimal); + } + return sb.toString(); + + } + public static String hexTo4ZN(String hex) throws Exception { + StringBuilder sb = new StringBuilder(); + for (int i = 0; i < hex.length() - 1; i += 4) { + String h = hex.substring(i, (i + 4)); + + sb.append((char) Integer.parseInt(h, 16)); + } + return sb.toString(); + + } + + + public static String str4Hex(String str) { + StringBuilder sb = new StringBuilder(); + for (int i = 0; i < str.length(); i++) { + String s = Integer.toString(str.charAt(i), 16); + sb.append(s.length()==2?"00"+s:s); + } + return sb.toString(); + } + public static String convertToAscii(String chineseText) { + StringBuilder asciiText = new StringBuilder(); + + for (int i = 0; i < chineseText.length(); i++) { + char c = chineseText.charAt(i); + int asciiValue = (int) c; + + asciiText.append(asciiValue).append(" "); + } + + return asciiText.toString().trim(); + } +}