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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • js中join方法

    js中join方法js中的join方法join方法用于把数组中的所有元素放入一个字符串。元素是通过指定的分隔符进行分隔的。大白话:join方法可以用符不同的分隔符来构建这个字串。join方法值接受一个参数,即用作分隔符的字符串,然后返回所有数组项的字符串。vararr=["red","yellow","blue"];vararray=[];下面开始调用join方法1a…

    2022年4月29日
    82
  • matlab直方图绘制[通俗易懂]

    matlab直方图绘制[通俗易懂]x=[55,63,69,70,75,78,82,84,85,88,90,96,100];y=[1,2,1,6,4,7,2,1,3,2,4,2,1];bar(x,y)>>a=[54.5,64.5,74.5,84.5,94.5];>>b=[1,3,17,8,7];>>bar(a,b)barh():可以显示一个水平的条形图barh(a,b)bar3或bar3h:显示三维bar3(a,b)…

    2022年10月18日
    3
  • dom4j 解析 xml

    dom4j 解析 xml

    2022年6月21日
    31
  • 树莓派简单教程(一)「建议收藏」

    树莓派简单教程(一)「建议收藏」一、什么是树莓派基于ARM的微型电脑主板,其系统基于linux二、树莓派能做什么2.1挖矿【树莓派:居然拿我干这事?———6分钟使用树莓派,编译CPU挖矿程序测试算力。-哔哩哔哩】https://b23.tv/tvx06lM…

    2022年10月14日
    3
  • ios消息推送

    ios消息推送

    2021年8月19日
    64
  • Java Session获取/销毁

    Java Session获取/销毁Session会话机制,它是保存在服务器里面的,就是当我们进入一个浏览器,浏览器会自动给我们创建一个session保存用户的信息,当关闭浏览器的时候,session就销毁了,Session在web项目中是经常用到的。下面是session的用法。Session在web项目中的登录是基本都用上的,那么就以账号登录为例:代码只是展示了最里面判断登录成功并保存数据到session的//获取ses…

    2022年7月12日
    27

发表回复

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

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