栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何将int []转换为byte []

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何将int []转换为byte []

正如Brian所说,您需要确定需要什么样的转换。

您是否要将其保存为“正常”图像文件(jpg,png等)?

如果是这样,您可能应该使用Java Image I /
O
API。

如果要以“原始”格式保存,则必须指定写入字节的顺序,然后使用

IntBuffer
和NIO。

作为使用ByteBuffer / IntBuffer组合的示例:

import java.nio.*;import java.net.*;class Test{       public static void main(String [] args)        throws Exception // Just for simplicity!    {        int[] data = { 100, 200, 300, 400 };        ByteBuffer byteBuffer = ByteBuffer.allocate(data.length * 4);     IntBuffer intBuffer = byteBuffer.asIntBuffer();        intBuffer.put(data);        byte[] array = byteBuffer.array();        for (int i=0; i < array.length; i++)        { System.out.println(i + ": " + array[i]);        }    }}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/483265.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号