arduino 与 android 通过TCP进行字节收发
2015-11-17 16:50
519 查看
arduino
View Code
注意:
package cn.fstudio.wificracker; public class BitConvert { /** * 灏嗕竴涓崟瀛楄妭鐨刡yte杞崲鎴�32浣嶇殑int * * @param b * byte * @return convert result */ public static int unsignedByteToInt(byte b) { return (int) b & 0xFF; } /** * 灏嗕竴涓崟瀛楄妭鐨凚yte杞崲鎴愬崄鍏繘鍒剁殑鏁� * * @param b * byte * @return convert result */ public static String byteToHex(byte b) { int i = b & 0xFF; return Integer.toHexString(i); } /** * 灏嗕竴涓�4byte鐨勬暟缁勮浆鎹㈡垚32浣嶇殑int * * @param buf * bytes buffer * @param byte[]涓紑濮嬭浆鎹㈢殑浣嶇疆 * @return convert result */ public static long unsigned4BytesToInt(byte[] buf, int pos) { int firstByte = 0; int secondByte = 0; int thirdByte = 0; int fourthByte = 0; int index = pos; firstByte = (0x000000FF & ((int) buf[index+3])); secondByte = (0x000000FF & ((int) buf[index + 2])); thirdByte = (0x000000FF & ((int) buf[index + 1])); fourthByte = (0x000000FF & ((int) buf[index ])); index = index + 4; return ((long) (firstByte << 24 | secondByte << 16 | thirdByte << 8 | fourthByte)) & 0xFFFFFFFFL; } /** * 灏�16浣嶇殑short杞崲鎴恇yte鏁扮粍 * * @param s * short * @return byte[] 闀垮害涓�2 * */ public static byte[] shortToByteArray(short s) { byte[] targets = new byte[2]; for (int i = 0; i < 2; i++) { int offset = (targets.length - 1 - i) * 8; targets[i] = (byte) ((s >>> offset) & 0xff); } return targets; } /** * 灏�32浣嶆暣鏁拌浆鎹㈡垚闀垮害涓�4鐨刡yte鏁扮粍 * * @param s * int * @return byte[] * */ public static byte[] intToByteArray(int s) { byte[] targets = new byte[2]; for (int i = 0; i < 4; i++) { int offset = (targets.length - 1 - i) * 8; targets[i] = (byte) ((s >>> offset) & 0xff); } return targets; } /** * long to byte[] * * @param s * long * @return byte[] * */ public static byte[] longToByteArray(long s) { byte[] targets = new byte[2]; for (int i = 0; i < 8; i++) { int offset = (targets.length - 1 - i) * 8; targets[i] = (byte) ((s >>> offset) & 0xff); } return targets; } /**32浣峣nt杞琤yte[]*/ public static byte[] int2byte(int res) { byte[] targets = new byte[4]; targets[0] = (byte) (res & 0xff);// 鏈�浣庝綅 targets[1] = (byte) ((res >> 8) & 0xff);// 娆′綆浣� targets[2] = (byte) ((res >> 16) & 0xff);// 娆¢珮浣� targets[3] = (byte) (res >> 24);// 鏈�楂樹綅,鏃犵鍙峰彸绉汇�� return targets; } /** * 灏嗛暱搴︿负2鐨刡yte鏁扮粍杞崲涓�16浣峣nt * * @param res * byte[] * @return int * */ public static int byte2int(byte[] res) { // res = InversionByte(res); // 涓�涓猙yte鏁版嵁宸︾Щ24浣嶅彉鎴�0x??000000锛屽啀鍙崇Щ8浣嶅彉鎴�0x00??0000 int targets = (res[0] & 0xff) | ((res[1] << 8) & 0xff00); // | 琛ㄧず瀹変綅鎴� return targets; } }
View Code
注意:
char c= mySerial.read(); RecCache=RecCache +(String)c; 遇到byte=0 时会发生错误 如果需要按byte接收数据,请采用 /article/4890325.html 的方式
相关文章推荐
- 计算机网络——运输层
- WINDOWS网络编程接口
- 卷积神经网络参数说明
- T/TCP TCP事物协议的学习
- 几种通讯协议的比较RMI > Httpinvoker >= Hessian >> Burlap >> web service (转)
- 卷积神经网络CNN
- 导入javaweb项目后,java代码httprequest、@Override报错
- 编译httpd 和 php
- centos时间不同步网络问题
- linux c 下的 tcp的recv函数和udp的recvfrom函数 参数对比
- 网络请求和缓存
- 在RDO自动安装的OpenStack Liberty单机中搭建能够内外网互访的网络(三)-配置规则和创建密钥对
- HTTPRequest 网络请求之get请求和post请求
- 主机字节序+网络字节序
- Tomcat中配置https访问
- org.apache.http.Header这个类找不到的情况
- TCP/IP详解之:ARP协议 和 RARP协议
- 在RDO自动安装的OpenStack Liberty单机中搭建能够内外网互访的网络(二)-安装系统和OpenStack
- HTTP 长连接和短连接
- Sublime Text 官方网站 http://www.sublimetext.com