Android TCP传输自定义格式数据帧(1)

public static byte[] intToBytes(int value, ByteOrder mode) {

byte[] src = new byte[4];

if(mode == ByteOrder.LITTLE_ENDIAN){

src[3] = (byte) ((value >> 24) & 0xFF);

src[2] = (byte) ((value >> 16) & 0xFF);

src[1] = (byte) ((value >> 8) & 0xFF);

src[0] = (byte) (value & 0xFF);

}else{

src[0] = (byte) ((value >> 24) & 0xFF);

src[1] = (byte) ((value >> 16) & 0xFF);

src[2] = (byte) ((value >> 8) & 0xFF);

src[3] = (byte) (value & 0xFF);

}

return src;

}

public static int bytesToInt(byte[] src, int offset, ByteOrder mode) {

int value;

if(mode == ByteOrder.LITTLE_ENDIAN){

value = (int) ((src[offset] & 0xFF) | ((src[offset + 1] & 0xFF) << 8)

| ((src[offset + 2] & 0xFF) << 16) | ((src[offset + 3] & 0xFF) << 24));

}else{

value = (int) (((src[offset] & 0xFF) << 24)

| ((src[offset + 1] & 0xFF) << 16)

| ((src[offset + 2] & 0xFF) << 8) | (src[offset + 3] & 0xFF));

}

return value;

}

3.连接byte[]

public static byte[] concatAll(byte[] first, byte[]… rest) {

int totalLength = first.length;

for (byte[] array : rest) {

totalLength += array.length;

}

byte[] result = Arrays.copyOf(first, totalLength);

int offset = first.length;

for (byte[] array : rest) {

System.arraycopy(array, 0, result, offset, array.length);

offset += array.length;

}

return result;

}

4.十六进制String > byte[]

public static byte[] hexStringToByte(String hex) {

hex = hex.toUpperCase();

int len = (hex.length() / 2);

byte[] result = new byte[len];

char[] achar = hex.toCharArray();

for (int i = 0; i < len; i++) {

int pos = i * 2;

result[i] = (byte) (toByte(achar[pos]) << 4 | toByte(achar[pos + 1]));

}

return result;

}

5.byte[] > 十六进制String

资源分享

  • 最新大厂面试专题

这个题库内容是比较多的,除了一些流行的热门技术面试题,如Kotlin,数据库,Java虚拟机面试题,数组,Framework ,混合跨平台开发,等

  • 对应导图的Android高级工程师进阶系统学习视频
    最近热门的,NDK,热修复,MVVM,源码等一系列系统学习视频都有!

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化学习资料的朋友,可以戳这里获取

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

统化学习资料的朋友,可以戳这里获取](https://bbs.csdn.net/topics/618156601)**

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

  • 20
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值