- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
大家好!我一直在寻找一些关于 Java 序列化的旧帖子。我想将对象转换为 ByteArray。到目前为止我已经这样做了:
public class test
{
public static void main(String[] args) throws IOException
{
// 00111111 00111111 00111111 11110000 - in bytes: 63, 63, 63, 240
int x = 1061109744;
byte[] bytes = null;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutput out = null;
try {
out = new ObjectOutputStream(bos);
out.writeObject(x);
bytes = bos.toByteArray();
}
finally
{
out.close();
bos.close();
}
System.out.println("Lenght: " + bytes.length);
for(int i=0; i < bytes.length; i++)
{
System.out.println(bytes[i]);
}
}
}
显然它工作得很好,但是它在 ByteArray
中引入了很多“垃圾”。我真正感兴趣的值是与我的“int x = 1061109744;”相对应的最后 4 个字节。
为什么会发生这种情况?
是否可以避免“垃圾”值?
是否有可能超越“有符号”值? (我必须写入大于 128 的字节值)。
最佳答案
Is it possible to avoid the "junk" values?
是的。写出一个int
,而不是一个Object
。通过将 int
传递给 writeObject
,您可以将其提升(自动装箱)为 Integer
,这意味着序列化信息包含 header 信息:它是一个Integer
对象,而不是一个int
。您的 ByteArrayOutputStream 有 write(int)
将一个字节(int 的低 8 位)写入输出流的方法。这些是否已签名纯粹是解释问题,而不是流中的位。
关于java 将对象序列化为ByteArray,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16379338/
我有以下代码,适用于文本文件,但不适用于 pdf 文件。我的文件包含英语和希腊语字符。我尝试将 pdf 文件转换为 byteStream 并将 byteStream 转换为 String 格式,以便将
我有一些大的二进制文件,我需要在其中搜索特定的字节序列,例如: find_bytes = bytearray(base64.b16decode('a226fb42')) with open(filen
如何将 bytearray 值分配给面板背景图像。如果有人有想法或经验,请帮助我克服这个问题。简要经验: 我有面板控制,想加载从 webservice 获取的图像作为背景图像。所以我使用了 setst
我的 ByteArray 值为 avroBinaryValue ,架构名称值为 String schemaName ,上次修改日期值为 long 的 lastModifiedDate 。 byte[]
我试图在开始时将一个字节数组插入到另一个字节数组中。这是我正在努力完成的一个简单示例。 import struct a = bytearray(struct.pack(">i", 1)) b = by
我正在尝试将 long 和 bytearray 连接到另一个 bytearray。 我尝试过这样的: byte[] value1= new byte[16]; byte[] value2= new b
我有一个任务,我必须在数据库中读取和保存图像。 我正在使用 C#.net Webservice 和 SQlserver2008 数据库,并将图像保存为 varbinary 格式。 我想知道 如何从本地
我在创建 ByteArray var 时遇到了一些问题,其中它的元素也是 ByteArray,我不知道是不是可能先?以及如何? 最佳答案 ByteArray 顾名思义,就是一个字节数组。如果您想保留多
我有三个 bytearray,长度为 40000。我想将字节数组 1 的字节数组索引 0,1、bytearaay2 的索引 0,1 和 bytearray3 的索引合并到 40000 长度。 像这样:
我认为这是一个新手类型的问题,但我很理解这一点。 我可以找到很多关于如何将字符串转换为各种语言的字节数组的帖子。 我不明白的是逐个字符发生了什么。我知道屏幕上显示的每个字符都由一个数字表示,例如它的
锁定。这个问题及其答案是locked因为这个问题是题外话,但具有历史意义。它目前不接受新的答案或互动。 这是我的( Code Golf )挑战: 取两个字节数组并确定第二个数组是否是第一个数组的子字符
我在字节流方面做了很多工作,所以我理解这个概念——有些字节表示比其他字节更高的数值。 在阅读维基上的定义时,我变得非常困惑。 当我看到“Big-endian”或 BIG + END 这个词时,我期望最
我正在使用 Kafka 发送生产和消费消息。 生产很好,使用 制作人。 消费时,我使用下面的代码(取自示例)但我得到的每条记录只有 8 个字节(代码下方的示例输出)。 有没有一种方法可以让消费者简单地
我有很多时间创建缩略图,然后将它们转换为字节数组。我尝试了三种方法,所有 3 次都出现错误。 第一个是使用Bitmap.GetThumbnailImage,我过去用过,然后直接保存到Response.
如何将存储文件分配给 ByteArray? var file = await openPicker.PickSingleFileAsync(); 最佳答案 这里是一个使用文件选择器的示例,获取存储文件
我需要在Kotlin中创建一个包含混合类型的字节数组。 具体来说,我需要编写如下内容: 0, 1, 0x02, 1, "me@emailaddr.com" 使用Kotlin实现BLE的特性。 显然,我
当我运行这个: class SomeTest { fun howToMockByteArray() { val bytes = Mockito.mock(ByteArray::
D中ubyte[]的最大大小有限制吗? 在this线程 我遇到内存不足错误,无法理解是驱动程序问题还是 ubyte[] 大小限制? 最佳答案 D中任意数组的最大长度为size_t.max。但是,由于
如果只有一个写入器将字节附加到一个bytearray,并且多个读取器从中读取数据,那么是否有可能一个或多个读取器读取既不在扩展之前也不在扩展之后的数据? 例如,如果bytearray中的旧数据是012
请耐心听我说;我是系统管理员而不是开发人员。下面的代码对我来说效果很好。但是当我将其分成两个文件以便类位于一个文件中而逻辑位于另一个文件中时,我收到一条错误,指出 data[0] 是 str 并且不支
我是一名优秀的程序员,十分优秀!