当前位置: 首页 > news >正文

htm5网站网站建设知名公司

htm5网站,网站建设知名公司,开发公司设计管理部绩效考核,网站 做 app文章目录 前言一、无符号字节转为int1.前置知识2.无符号转int代码3.Java中字节转为int 二、字节缓冲流1.基础知识2.String与ByteBuffer转换 总结 前言 Java 中基本类型都是有符号数值,如果接收到了 C/C 处理的无符号数值字节流,将出现转码错误。 提示&a…

文章目录

  • 前言
  • 一、无符号字节转为int
    • 1.前置知识
    • 2.无符号转int代码
    • 3.Java中字节转为int
  • 二、字节缓冲流
    • 1.基础知识
    • 2.String与ByteBuffer转换
  • 总结


前言

Java 中基本类型都是有符号数值,如果接收到了 C/C++ 处理的无符号数值字节流,将出现转码错误。


提示:以下是本篇文章正文内容,下面案例可供参考

一、无符号字节转为int

1.前置知识

在线进制转换:https://tool.oschina.net/hexconvert
之前在解析 webscoket 传输得二进制数据时,因为二进制数据传输的是 uint32 无符号整数,需要把有符号的字节转为正常的;
uint32 代表无符号整数,只能存正整数,在内存中占4个字节,byte[4],0到4294967295,Java中 int 为32位有符号整数,占4字节,-2147483648 到 2147483648。

2.无符号转int代码

public static long bytes2int(byte[] buf){long anUnsignedInt = 0;int firstByte = 0;int sceondByte = 0;int thirdByte = 0;int fourthByte = 0;int index = 0;firstByte = (0x000000FF & ((int) buf[index+3]));sceondByte = (0x000000FF & ((int) buf[index+2]));thirdByte = (0x000000FF & ((int) buf[index+1]));fourthByte = (0x000000FF & ((int) buf[index]));anUnsignedInt = ((long) (firstByte << 24 | sceondByte << 16 | thirdByte << 8 | fourthByte)) & 0xFFFFFFFFL;return anUnsignedInt ;
}

3.Java中字节转为int

public static int byteArrayToInt(byte[] bytes) {int n = 0;for (int i = 0; i < 4; i++) {n += bytes[i] << i*8;}return n;
}

二、字节缓冲流

1.基础知识

  • 分配一个指定大小的缓冲区
// 1.分配一个指定大小的缓冲区
ByteBuffer buf = ByteBuffer.allocate(1024);
System.out.println(buf.position); 	//0
System.out.println(buf.limit); 		//1024
System.out.println(buf.capacity); 	//1024
System.out.println(buf.mark); 
  • 利用put()方法进行存储数据
// 2.利用put()方法进行存储数据
String str = "hello nio";
buf.put(str.getBytes());
System.out.println(buf.position); 	//9
System.out.println(buf.limit); 		//1024
System.out.println(buf.capacity); 	//1024
System.out.println(buf.mark); 
  • 切换读取数据的模式
// 3.切换读取数据的模式
buf.flip();
System.out.println(buf.position); 	//0
System.out.println(buf.limit); 		//1024
System.out.println(buf.capacity); 	//1024
System.out.println(buf.mark);
  • 利用get()方法读取数据
// 4.利用get()方法读取数据
byte[] dst = new byte[buf.limit()];
buf.get(dst);
System.out.println(new String(dst, 0, dst.lenth));System.out.println(buf.position); 	//9
System.out.println(buf.limit); 		//9
System.out.println(buf.capacity); 	//1024
System.out.println(buf.mark

2.String与ByteBuffer转换

import java.nio.ByteBuffer;  
import java.nio.CharBuffer;  
import java.nio.charset.Charset;  
import java.nio.charset.CharsetDecoder;  public class Test {  /** * String 转换 ByteBuffer * @param str * @return */  public static ByteBuffer getByteBuffer(String str) {  return ByteBuffer.wrap(str.getBytes());  }  /** * ByteBuffer 转换 String * @param buffer * @return */  public static String getString(ByteBuffer buffer) {  Charset charset = null;  CharsetDecoder decoder = null;  CharBuffer charBuffer = null;  try {  charset = Charset.forName("UTF-8");  decoder = charset.newDecoder();  // charBuffer = decoder.decode(buffer);//用这个的话,只能输出来一次结果,第二次显示为空  charBuffer = decoder.decode(buffer.asReadOnlyBuffer());  return charBuffer.toString();  }  catch (Exception ex) {  ex.printStackTrace();  return "";  }  }  
}

总结

生活, 一半是回忆, 一半是继续。 把所有的不快给昨天, 把所有的希望给明天, 把所有的努力给今天。

在这里插入图片描述

http://www.hkea.cn/news/121571/

相关文章:

  • 企业网站开发注意什么企业网站官网
  • 网站建设开发合同书关键词怎么找出来
  • 常州微信网站建设附子seo
  • 上海网站seo招聘十种营销方式
  • 农产品网络营销模式百度推广怎么优化
  • 公司网站维护如何做分录自己搭建一个网站
  • 做期货浏览哪些网站网络优化工程师前景如何
  • 垂直b2b电子商务网站有哪些google搜索排名优化
  • 建设中网站源码网络推广工具和方法
  • 厦门做点击付费网站培训教育
  • 常州网站建设案例网站制作建设公司
  • 外国人做家具的网站一站传媒seo优化
  • 佛山h5建站模板怎样优化网站
  • 第三方做公司网站谷歌搜索广告优化
  • 网站风格模板快速排名精灵
  • 做网站横幅 的网站推荐几个公司推广
  • html5国内网站建设客户管理软件
  • 网站建设报价单站长工具 seo查询
  • 日本电商网站贵州快速整站优化
  • 物业服务网站建设建立网站要多少钱一年
  • 中铁建设门户加长版廊坊百度提升优化
  • 最便宜的外贸网站建设电商平台运营方案
  • 做网站应该会什么问题网络营销软文范例500字
  • 摄影网课百度关键词优化查询
  • 打广告型的营销网站西安百度推广外包
  • 乌鲁木齐招聘网站建设一站式网络营销
  • 中小型网站建设服务淘宝数据分析工具
  • 梧州网站设计企业网站模板建站
  • 行政事业单位网站建设建议营销策划公司
  • 网络推广网站怎么做百度联盟广告点击一次收益