16进制转换(文字)Demo
来源:互联网 发布:oc js调用支付宝app 编辑:程序博客网 时间:2024/05/18 01:56
16进制转换(文字)Demo
MainActivity.java
package com.administrator.bluetooth;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import java.io.ByteArrayOutputStream;
import java.io.UnsupportedEncodingException;
public class MainActivity extends ActionBarActivity {
private EditText et;
private Button btn, btn2;
private TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et = (EditText) findViewById(R.id.editText);
btn = (Button) findViewById(R.id.button);
btn2 = (Button) findViewById(R.id.button2);
tv = (TextView) findViewById(R.id.textView);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String content = et.getText().toString();
String str = content;
String hexString = "0123456789ABCDEF";
byte[] bytes;
try {
bytes = str.getBytes("GBK");//如果此处不加编码转化,得到的结果就不是理想的结果,中文转码
StringBuilder sb = new StringBuilder(bytes.length * 2);
for (int i = 0; i < bytes.length; i++) {
sb.append(hexString.charAt((bytes[i] & 0xf0) >> 4));
sb.append(hexString.charAt((bytes[i] & 0x0f) >> 0));
//sb.append("");
}
String result = sb.toString();
tv.setText(result);
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
//将editText中输入的string字符串转化成中文的形式
btn2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String content = et.getText().toString();
tv.setText(decode(content));
}
});
}
private String decode(String content) {
String hexString = "0123456789ABCDEF";
ByteArrayOutputStream baos = new ByteArrayOutputStream(content.length() / 2);
//将每2位16进制整数组装成一个字节
for (int i = 0; i < content.length(); i += 2)
baos.write((hexString.indexOf(content.charAt(i)) << 4 | hexString.indexOf(content.charAt(i + 1))));
String bb = "";
try {
bb = new String(baos.toByteArray(), "GBk");
} catch (Exception e) {
e.printStackTrace();
}
return bb;
}
}
布局R.layout.activity_main
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.hextohanz.MainActivity" >
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<EditText
android:id="@+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="47dp"
android:layout_marginTop="16dp"
android:ems="10" >
<requestFocus />
</EditText>
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editText"
android:layout_below="@+id/editText"
android:text="点击进行汉字转16进制" />
<Button
android:id="@+id/button2"
android:layout_marginTop="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button"
android:layout_below="@+id/button"
android:text="点击进行16进制转汉字" />
</RelativeLayout>
- 16进制转换(文字)Demo
- 进制转换demo
- 文字转换
- IOS文字转语音DEMO
- 进制转换 定点数 原码 补码 浮点数 海明码 循环码 CRC 文字编码
- 自定义数据类型转换Demo
- 中文日期转换Demo
- 强制类型转换Demo
- c++ demo: 单词转换
- 文字输入转换
- 简繁体文字转换工具
- ASCII码转换文字
- 图片文字快速转换
- 转换文字为符号
- 文字转换成Drawable
- 文字数值转换工具
- 文字识别转换
- 文字十六进制转换
- Netra基于Rdk平台的软件框架设计
- Storm的重要概念
- 知乎 哪些素质很重要,却是读书学不来的?-2
- K-Nearest Neighbor Classifier (邻近算法)
- 数据库连接串的配置
- 16进制转换(文字)Demo
- java gbk转utf-8
- Android_原型模式_项目应用
- SAP ABAP编程 Table Control动态隐藏列
- Storm实战之TopN
- 升级Xcode之后VVDocumenter-Xcode不能用的解决办法
- 送给还在面试的程序猿
- JAVA逻辑--用JAVA代码打印菱形
- Mysql数据库字段中去除回车和换行符