Android与单片机通信常用数据转换方法总结

Android与单片机通信常用数据转换方法

  1.  将GB2312转化为中文,如BAFAC2DCB2B7→胡萝卜,两个字节合成一个文字

public static String stringToGbk(String string) throws Exception {
  byte[] bytes = new byte[string.length() / 2];
  for (int j = 0; j < bytes.length; j++) {
    byte high = Byte.parseByte(string.substring(j * 2, j * 2 + 1), 16);
    byte low = Byte.parseByte(string.substring(j * 2 + 1, j * 2 + 2),
        16);
    bytes[j] = (byte) (high << 4 | low);
  }
  String result = new String(bytes, "GBK");
  return result;
}

  2.将中文转化为GB2312,并且结果以byte[]形式返回,如胡萝卜→new byte[]{BA  FA C2 DC B2 B7},一个字被分为两个字节

public static byte[] gbkToString(String str) throws Exception {
  return new String(str.getBytes("GBK"), "gb2312").getBytes("gb2312");
}

  3.将十六进制的byte[]原封不动的转化为string,如byte[]{0x7e,0x80,0x11,0x20}→7e801120,可用于log打印

public static String bytesToHexString(byte[] src) {
  StringBuilder stringBuilder = new StringBuilder("");
  if (src == null || src.length <= 0) {
    return null;
  }
  for (int i = 0; i < src.length; i++) {
    int v = src[i] & 0xFF;
    String hv = Integer.toHexString(v);
    if (hv.length() < 2) {
      stringBuilder.append(0);
    }
    stringBuilder.append(hv);
  }
  return stringBuilder.toString();
}

  4.将十六进制的byte[]原封不动的转化为string,并且每个byte之间用空格分开,如byte[]{0x7e,0x80,0x11,0x20}→7e 80 11 20,,可用于log打印

public static StringBuilder byte2HexStr(byte[] data) { 

  if (data != null && data.length > 0) {
    StringBuilder stringBuilder = new StringBuilder(data.length);
    for (byte byteChar : data) {
      stringBuilder.append(String.format("%02X ", byteChar));
    }
    return stringBuilder;
  }
  return null;
}

  5.将byte[]数组转化为8、10、16等各种进制,例如byte[0x11,0x20]→4384,约等于1120(16进制)→4384,radix代表进制

public static String bytesToAllHex(byte[] bytes, int radix) {
  return new BigInteger(1, bytes).toString(radix);// 这里的1代表正数
}

  6.将String的十六进制原封不动转化为byte的十六进制,例如7e20→new byte[]{0x7e,x20}

public static byte[] HexString2Bytes(String src) {
  byte[] ret = new byte[src.length() / 2];
  byte[] tmp = src.getBytes();
  for (int i = 0; i < tmp.length / 2; i++) {
    ret[i] = uniteBytes(tmp[i * 2], tmp[i * 2 + 1]);
  }
  return ret;
}
public static byte uniteBytes(byte src0, byte src1) {
  byte _b0 = Byte.decode("0x" + new String(new byte[] { src0 }))
      .byteValue();
  _b0 = (byte) (_b0 << 4);
  byte _b1 = Byte.decode("0x" + new String(new byte[] { src1 }))
      .byteValue();
  byte ret = (byte) (_b0 ^ _b1);
  return ret;
} 

以上就是对Android 与单片机通信的资料整理,后续继续补充相关资料谢谢大家对本站的支持!

(0)

相关推荐

  • Android 调用系统照相机拍照和录像

    本文实现android系统照相机的调用来拍照 项目的布局相当简单,只有一个Button: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_heig

  • Android开发实现带有反弹效果仿IOS反弹scrollview教程详解

    首先给大家看一下我们今天这个最终实现的效果图: 这个是ios中的反弹效果.当然我们安卓中如果想要实现这种效果,感觉不会那么生硬,滚动到底部或者顶部的时候.当然 使用scrollview是无法实现的.所以我们需要新建一个view继承ScrollView package davidbouncescrollview.qq986945193.com.davidbouncescrollview; import android.annotation.SuppressLint; import android.

  • Android draw9patch 图片制作与使用详解

    Android draw9patch 图片制作与使用 理解一下4句话: 上边 决定左右拉升不变形 左边 决定上下拉升不变形 右边 设置内容高度区域 下边 设置内容宽度区域 下面我拿6张图片分别举例说明: 1.QQ多彩气泡 聊天对话框也用.9图片制作 继承过环信IM即时通讯的同学可以去看他们的源码,聊天对话框也是这样用9patch制作的呢. 2.上边一般都是1个像素,根据这个点左右拉伸,如果右边画红线的2张图片就是拉伸的局域. 3.左边一般也是1个像素点,根据这个点上下拉伸,如右边画红线的2张图片

  • Android ListView万能适配器实例代码

    ListView是开发中最常用的控件了,但是总是会写重复的代码,浪费时间又没有意义. 最近参考一些资料,发现一个万能ListView适配器,代码量少,节省时间,总结一下分享给大家. 首先有一个自定义的Adapter继承于BaseAdapter,下面是自定义的Adapter,精华在getView()方法中 package com.example.mylistview.util; import java.util.List; import android.content.Context; impor

  • AndroidStudio 使用过程中出现的异常(Gradle sync failed)处理办法

    AndroidStudio使用过程中出现的异常 异常信息: Gradle sync failed: Unable to start the daemon process. This problem might be caused by incorrect configuration of the daemon. For example, an unrecognized jvm option is used. Please refer to the user guide chapter on th

  • Android的分辨率和屏幕适配详解

    一.为什么Android要进行分辨率与屏幕适配 最大的原因是碎片化,因为Android的开源措施和各个厂商的自己细微修改,结果就变成了这个样 需要适配的屏幕尺寸就有这么多: 这怎么可能嘛T_T. 所以我们就只照顾大部分人,根据友盟的统计数据如下: 所以只需要适配: 800x480.854x480.960x540.1184x720.1280x720.1920x1080这六种分辨率. 二.基本知识 屏幕尺寸 英寸,1英寸=2.54厘米.比如常见的屏幕尺寸有2.4.2.8.3.5.3.7.4.2.5.

  • Android 图片的颜色处理实例代码

    仿造美图秀秀移动鼠标调整seekbar,调整图片的颜色 项目布局如下: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="ma

  • Android使用addView动态添加组件的方法

    在项目开发中,我们经常需要进行动态添加组件,其中可添加的部分有两项:布局和组件 其中,添加的布局主要有RelativeLayout型(相对布局)的和LinearLayout(线性布局) 添加的组件主要有文本显示框,编辑框,按钮等组件. 下面,就让我们来进行实现: 首先我们创建一个新的项目,删除MainActivity.class中没有的代码,仅留下protected void onCreate(Bundle savedInstanceState)函数往布局文件中添加一个新的组件: 1. addV

  • Android开发悬浮按钮 Floating ActionButton的实现方法

    一.介绍 这个类是继承自ImageView的,所以对于这个控件我们可以使用ImageView的所有属性 android.support.design.widget.FloatingActionButton 二.使用准备, 在as 的 build.grade文件中写上 compile 'com.android.support:design:22.2.0' 三.使用说明 xml文件中,注意蓝色字体部分 <android.support.design.widget.FloatingActionButt

  • Android自定义view系列之99.99%实现QQ侧滑删除效果实例代码详解

    首先声明本文是基于GitHub上"baoyongzhang"的SwipeMenuListView修改而来,该项目地址: https://github.com/baoyongzhang/SwipeMenuListView 可以说这个侧滑删除效果是我见过效果最好且比较灵活的项目,没有之一!!! 但是在使用它之前需要给大家提两点注意事项: 1,该项目支持Gradle dependence,但是目前作者提供的依赖地址对应的项目不是最新的项目,依赖过后的代码与demo中使用的不一致,会提示没有B

随机推荐