java bytebuffer flip_java string转byte

java bytebuffer flip_java string转byteimportjava.nio.ByteBuffer;importjava.nio.ByteOrder;publicclassbytebuffertest{publicstaticvoidmain(String[]args){//CreateaByteBufferusingabytearraybyte[]bytes=newbyte[10];Byt

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE稳定放心使用

import java.nio.ByteBuffer;  
import java.nio.ByteOrder;  
public class bytebuffertest {  
  
    public static void main(String[] args)  
    {  
        // Create a ByteBuffer using a byte array   
        byte[] bytes = new byte[10];  
        ByteBuffer buf = ByteBuffer.wrap(bytes);  
  
        // Create a non-direct ByteBuffer with a 10 byte capacity   
        // The underlying storage is a byte array.   
        buf = ByteBuffer.allocate(10);  
  
        // Create a direct (memory-mapped) ByteBuffer with a 10 byte capacity.   
        buf = ByteBuffer.allocateDirect(10);  
  
          
        // Create an empty ByteBuffer with a 10 byte capacity   
        ByteBuffer bbuf = ByteBuffer.allocate(10);  
  
        // Get the ByteBuffer's capacity   
        int capacity = bbuf.capacity(); // 10   
        System.out.println(capacity);  
  
        // Use the absolute get().   
        // This method does not affect the position.   
        byte b = bbuf.get(5); // position=0   
  
        // Set the position   
        bbuf.position(5);  
  
        // Use the relative get()   
        b = bbuf.get();  
  
        // Get the new position   
        int pos = bbuf.position(); // 6   
  
        // Get remaining byte count   
        int rem = bbuf.remaining(); // 4   
  
        // Set the limit   
        bbuf.limit(7); // remaining=1   
  
        // This convenience method sets the position to 0   
        bbuf.rewind(); // remaining=7   
          
        //Converting Between a ByteBuffer an a Byte Array    
  
        // Create a ByteBuffer from a byte array   
        byte[] bytes1 = new byte[10];  
        ByteBuffer buf1 = ByteBuffer.wrap(bytes);  
  
        // Retrieve bytes between the position and limit   
        // (see Putting Bytes into a ByteBuffer)   
        bytes1 = new byte[buf.remaining()];  
        buf1.get(bytes1, 0, bytes1.length);  
  
        // Retrieve all bytes in the buffer   
        buf1.clear();  
        bytes1 = new byte[buf1.capacity()];  
        buf1.get(bytes1, 0, bytes1.length);  
          
        // Use the absolute put().   
        // This method does not affect the position.   
        bbuf.put((byte)0xFF); // position=0   
  
        // Set the position   
        bbuf.position(5);  
  
        // Use the relative put()   
        bbuf.put((byte)0xFF);  
  
        // Get the new position   
        int pos1 = bbuf.position(); // 6   
          
          
        //Setting the Byte Ordering for a ByteBuffer    
        // Get default byte ordering   
        ByteOrder order = buf.order(); // ByteOrder.BIG_ENDIAN   
  
        // Put a multibyte value   
        buf.putShort(0, (short)123);  
        buf.get(0); // 0   
        buf.get(1); // 123   
  
        // Set to little endian   
        buf.order(ByteOrder.LITTLE_ENDIAN);  
  
        // Put a multibyte value   
        buf.putShort(0, (short)123);  
        buf.get(0); // 123   
        buf.get(1); // 0   
  
  
  
    }  
} 

这是一篇转载的博客我还没有来得及进行验证,放在这里只是为了以后自己方便查看,如有错误的地方还请以评论的方式告知,我会进行改正。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/186742.html原文链接:https://javaforall.net

(0)
上一篇 2022年10月2日 下午2:36
下一篇 2022年10月2日 下午2:46


相关推荐

  • jquery ajax步骤,jquery ajax(ajax请求的五个步骤jQuery)

    jquery ajax步骤,jquery ajax(ajax请求的五个步骤jQuery)jqueryajaxAJAX是与服务器交流数据的艺术,它在不重载全部页面的情况下,完成了对部分网页的更新。jQueryAJAX实例请点击下面的按钮,经过jQueryAJAX改变这段文本。获得外部的内容亲身试一试什么是AJAX?AJAX=异步JavaScript和XML(AsynchronousJavaScriptandXML)。简短地说,在不重载整个网页的情况下,AJAX经过后台加载数据,并在网页…

    2022年5月17日
    39
  • 格雷码与二进制的转换[通俗易懂]

    格雷码与二进制的转换[通俗易懂]一、什么是格雷码?格雷码,又叫循环二进制码或反射二进制码,格雷码是我们在工程中常会遇到的一种编码方式,它的基本的特点就是任意两个相邻的代码只有一位二进制数不同,这点在下面会详细讲解到。格雷码的基本特点就是任意两个相邻的代码只有一位二进制数不同,这点很重要。常用的二进制数与格雷码间的转换关系如下表:二、二进制格雷码与自然二进制码的互换1、二进制码转换成二进制格雷码  二进制码转换成二进制格雷码,

    2022年10月17日
    4
  • java 事务回滚注解_Java Spring 事务回滚详解

    java 事务回滚注解_Java Spring 事务回滚详解这篇文章主要介绍了 javaSpring 事务回滚的相关资料 需要的朋友可以参考下 spring 事务回滚 1 遇到的问题当我们一个方法里面有多个数据库保存操作的时候 中间的数据库操作发生的错误 伪代码如下 publicmethod Dao1 save Person1 Dao1 save Person2 Dao1 save Person2 假如这句发生了错误 前面的两个对象会被保存到数

    2026年3月26日
    1
  • 目标跟踪之Lukas-Kanade光流法

    目标跟踪之Lukas-Kanade光流法光流是图像亮度的运动信息描述。光流法计算最初是由Horn和Schunck于1981年提出的,创造性地将二维速度场与灰度相联系,引入光流约束方程,得到光流计算的基本算法.光流计算基于物体移动的光学特性提出了2个假设:①运动物体的灰度在很短的间隔时间内保持不变;②给定邻域内的速度向量场变化是缓慢的。算法原理假设图像上一个像素点(x,y),在t时刻的亮度为E(x+Δx,y+Δy,t+

    2022年7月23日
    14
  • oracle 导入dmp文件的正确姿势

    oracle 导入dmp文件的正确姿势文章目录前言导入 dmp 文件的正确形式出现的问题和解决方式 ORACLE 导入遇到 ORACLE 错误 959 解决方法 IMP 00017 由于 ORACLE 错误 6550 解决办法关于表空间的如果我们只想导入 dmp 文件中的某张表怎么办 PL SQLdeveloper 0 注册码前言最近需要导入一个 dmp 文件到 oracle 数据库 了解到不少的技巧 导入 dmp 文件的正确形式假设要导入的数据库是

    2026年3月18日
    1
  • AI之Agent:OpenClaw(Clawdbot/Moltbot)的简介、安装和使用方法、案例应用之详细攻略

    AI之Agent:OpenClaw(Clawdbot/Moltbot)的简介、安装和使用方法、案例应用之详细攻略

    2026年3月15日
    4

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号