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

那个网站做兼职靠谱深圳博惠seo

那个网站做兼职靠谱,深圳博惠seo,大连网站建设讯息,保山网站建设文章目录 前言一、无符号字节转为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/995570/

相关文章:

  • 广州北京网站建设公司网站外部优化的4大重点
  • 网站建设书优化大师是干什么的
  • 优秀的网站建设公司百度指数人群画像
  • wordpress企业中文模板太原seo哪家好
  • 广东网广东网站建设网站推广方案模板
  • 网站运营知识快手seo
  • 咖啡公司网站建设策划书微信营销方式
  • 柳江区城乡住房建设局网站上海seo优化服务公司
  • 西城企业网站建设企业网站怎么优化
  • 初学者做动态网站项目例子游戏特效培训机构排名
  • 汽车类网站搭建直链平台
  • 做网站遇到的困难总结网络营销软件代理
  • 做网站登录论坛外链代发
  • 东营专业网站建设公司排行青岛谷歌优化公司
  • 公众号和网站先做哪个口碑营销的形式
  • 长沙企业建网站费用关键词搜索推广排行榜
  • 怎么做网站端口代理沧州网络推广外包公司
  • php wordpress 目录seo课程培训机构
  • 常州网站建设方案优化引流app推广软件
  • 网络营销网站建设实训网络营销步骤
  • 网站都有后台吗百度竞价开户公司
  • 秭归网站建设网站seo优化心得
  • wordpress电影网站模板seo运营
  • 公司注册网上核名业务如何终止网站排名优化怎么做
  • 网站建设伍金手指下拉2网上推广平台
  • 沧州网站建设公司翼马爱情链接
  • 计算机学了出来干嘛免费优化推广网站的软件
  • 宁波网站建设优化湖南seo优化按天付费
  • 门户网站手机版google官网入口
  • 深圳市工程建设交易服务中心网站软文什么意思