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


相关推荐

  • 华为EC6108V9C/ E6108V9强刷固件及教程

    华为EC6108V9C/ E6108V9强刷固件及教程电信移动华为 EC6108V9C E6108V9 强刷固件刷机包及教程固件特点 1 调出原厂固件屏蔽的 wifi 开放原厂固件屏蔽的市场安装和 u 盘安装 apk 2 无开机广告 无系统更新 不在被强制升级 修改 dns 三网通用 3 大量精简内置的没用的软件 运行速度提升 30 以上 多出大量的存储空间 4 去除应用安装限制 实现自由安装软件 5 支持开机自启动 开机密码锁 儿童应用锁 应用隐藏 开机自动进入 HDMI 等各种花式功能 6 固件压缩包有刷机教程 解压获取 1 U 盘选择

    2025年6月14日
    1
  • 语义分割 实例分割 全景分割_语义分割转实例分割

    语义分割 实例分割 全景分割_语义分割转实例分割之前看过一篇使用分割思想进行目标检测,所以这里补习下一些分割相关的基础知识。这里重点说下语义分割、实力分割和全景分割的区别。1、semanticsegmentation(语义分割)通常意义上的目标分割指的就是语义分割,图像语义分割,简而言之就是对一张图片上的所有像素点进行分类语义分割(下图左)就是需要区分到图中每一点像素点,而不仅仅是矩形框框住了。但是同一物体的不同实例不需要单独分…

    2022年8月23日
    6
  • Qt中的QFile读写文件操作

    Qt中的QFile读写文件操作1.首先记录一下QString,QByteArray,char*之间的转换(1)QString->QByteArrayQStringbuf="123";QByteArraya=buf.toUtf8();//中文a=buf.toLocal8Bit();//本地编码(2)QByteArray->char*char*b=a.data…

    2022年6月13日
    26
  • pycharm21.02 激活码【2022.01最新】2022.03.08「建议收藏」

    (pycharm21.02 激活码)最近有小伙伴私信我,问我这边有没有免费的intellijIdea的激活码,然后我将全栈君台教程分享给他了。激活成功之后他一直表示感谢,哈哈~IntelliJ2021最新激活注册码,破解教程可免费永久激活,亲测有效,下面是详细链接哦~https://javaforall.net/100143.html40…

    2022年4月2日
    96
  • 自动生成测试用例_接口测试用例自动生成工具

    自动生成测试用例_接口测试用例自动生成工具前言写用例之前,我们应该熟悉API的详细信息。建议使用抓包工具Charles或AnyProxy进行抓包。har2case我们先来了解一下另一个项目har2case他的工作原理就是将当前主流的抓

    2022年7月28日
    10
  • @Scope(“prototype“)//多例模式[通俗易懂]

    @Scope(“prototype“)//多例模式[通俗易懂]@Scope(“prototype”)//多例模式

    2022年8月20日
    7

发表回复

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

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