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


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


相关推荐

  • jupyter notebook和spyder区别_命令行打开spyder

    jupyter notebook和spyder区别_命令行打开spyderPycharm、jupyterlab、jupyternotebook、python的区别Pycharm是开发工具,开发人员修改bug特别方便jupyterlab是jupyternotebook的升级版,特别友好,里面的很多插件对于你做数据分析很是方便jupyternotebook是数据分析师经常用的工具,小白学习数据分析时练习numpy、pandas就在这里进行python这个软件我没有用过,我大学学习python装的是Anaconda里面的spyderspyder刚开始接触pyt

    2022年8月28日
  • 十大开源视频会议「建议收藏」

    十大开源视频会议「建议收藏」视频会议及流媒体十大开源项目 在视频会议领域,有许多可以值得参考的开源项目,这些开源项目有的是协议栈、有的是编码器或者是传输协议,由于视频会议系统是一个综合性的应用系统,里面包含功能较多,如能把这些开源项目选择性的加入我们的视频会议开发当中,我们的开发效率肯定会事半功倍,下面我们列举一下视频会议相关的十大开源项目,并对其重要性及优缺点做一个全面的评价与排名。1、 OpenH323项

    2022年7月12日
  • intellij idea 激活码2099【2022最新】

    (intellij idea 激活码2099)这是一篇idea技术相关文章,由全栈君为大家提供,主要知识点是关于2021JetBrains全家桶永久激活码的内容IntelliJ2021最新激活注册码,破解教程可免费永久激活,亲测有效,下面是详细链接哦~https://javaforall.net/100143.htmlCJM5ZJBPHS-eyJsa…

    2022年3月31日
  • Python 爬虫系列教程一爬取批量百度图片[通俗易懂]

    Python 爬虫系列教程一爬取批量百度图片[通俗易懂]很久之前就学习了Python的爬虫了,也用来做过一些项目(主要是一些课程项目),但时间比较紧,一直没有空把它写下来,这个暑假,我可能会逐渐更新Python爬虫的相关知识。项目1:实现批量爬取百度图片先简单的介绍下这个项目。当你需要下载大量图片的时候,或许你会去百度图片里一张张右键下载,但这样未免太麻烦了,有了这个工具,你直接运行下程序,输入你想要下载图片的关键字,然后输入你想要下载图片的数…

    2022年5月3日
  • java中Scanner用法

    java中Scanner用法1.导入包,idea可自动导入importjava.util.Scanner;2.创建一个扫描器对象,用于接收用户输入的数据Scannerscanner=newScanner(System.in);3.创建完成后的接收以及判断<1>用next方法接收System.out.println(“使用next方式接收:”);//判断用户有没有输入字符串if(scanner.hasNext()){Stringstr=

    2022年7月7日
  • tp框架使用心得(六)——分页查询

    tp框架使用心得(六)——分页查询

    2021年10月21日

发表回复

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

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