【问题标题】:How to read and write Bytes without skipping the zeros in Java如何在不跳过 Java 中的零的情况下读取和写入字节
【发布时间】:2019-12-18 07:24:23
【问题描述】:

有谁知道如何在不跳过零的情况下输入或输出字节 我正在尝试编写一个将整数数组导出为无符号短裤的程序。 我已经编写了编写和读取波形文件的代码,但它们的格式不正确。

阅读示例

        // dwChunkSize
        byteConvertedLong = extractBytes(4);
        dwFormatChunkSize = convertBytesToLong(byteConvertedLong);
        System.out.println("Format Chunk size: " + dwFormatChunkSize);
        // wFormatTag
        byteConvertedInt = extractBytes(2);
        System.out.println("Format Tag: " + convertBytesToInt(byteConvertedInt));

读取数据的函数:

    // convert byte to long
    public long convertBytesToLong(byte[] values) {
        byte[] spliceToArray = {0, 0, 0, 0, 
            values[0], values[1], values[2], values[3]};
        ByteBuffer debuffer = ByteBuffer.wrap(spliceToArray);
        long returnValue = (long)debuffer.getLong();
        return returnValue;
    }

    // convert byte to int
    public int convertBytesToInt(byte[] values) {
        byte[] spliceToArray = {0, 0, values[0], values[1]};
        ByteBuffer debuffer = ByteBuffer.wrap(spliceToArray);
        int returnValue = debuffer.getInt();
        return returnValue;
    }

    // extract bytes to DataOutputStream
    public byte[] extractBytes(int bytesToExtract)
            throws IOException {

        // define byte array
        byte[] extractedBytes = new byte[bytesToExtract];

        // extract bytes
        dis.read(extractedBytes, byteTracker, bytesToExtract);

        return extractedBytes;
    }

写例子

// dwChunkSize
        byteConvertedLong = convertLongToBytes(dwFormatChunkSize);
        appendBytes(byteConvertedLong, 4, 8);
        // wFormatTag
        byteConvertedInt = convertIntToByte(W_FORMAT_TAG);
        appendBytes(byteConvertedInt, 2, 4);

写作的功能;

// convert long to byte
    public byte[] convertLongToBytes(long value) {
        ByteBuffer buffer = ByteBuffer.allocate(8);
        buffer.putLong(value);
        return buffer.array();
    }

    // convert int to byte
    public byte[] convertIntToByte(int value) {
        ByteBuffer buffer = ByteBuffer.allocate(4);
        buffer.putInt(value);
        return buffer.array();
    }

    // append bytes to DataOutputStream
    public void appendBytes(byte[] bytesToAppend, int start, int end)
            throws IOException {
        for (int i = start; i < end; i++) {
            dos.writeByte(bytesToAppend[i]);
        }
    }

我必须使用 Long 和 int 变量分别读取和写入 int 和 shorts,以便将它们写为无符号数字。

我一直在遵循此站点上的说明 https://blogs.msdn.microsoft.com/dawate/2009/06/23/intro-to-audio-programming-part-2-demystifying-the-wav-format/ 以确保所有数据的格式正确

读取和写入的主要问题是,如果我将 1 读取为短 (0000000000000001),它将跳过零并从 1 (10000000000000000) 开始读取。 如果这不是我不知道是什么问题?

【问题讨论】:

  • DataInputStream 已经拥有您需要的所有方法,并且您的问题也被标记了。所以这就是答案。而且它不会跳过零。不清楚你真正要问的是什么。
  • 好吧,有些事情发生了,下面是我用write方法写的一个正弦波的读取数据: Group ID Header: RIFF File length: 88246 Riff type: WAVE Group ID Format: fmt Format Chunk大小:18 格式标签:1 通道:1 采样率:44100 每秒平均采样数:88200 块对齐:每采样 2 位:16 组 ID 数据:数据数据块大小:88200 数据流:0 976 1951 2924 等.... .
  • 当我尝试读取用 Audacity 制作的正弦波时会发生这种情况,组 ID 标题:RIFF 文件长度:2891448576 线程“main”中的异常 java.lang.NullPointerException Riff 类型:WAVE 组 ID 格式:fmt 格式块大小:268435456 格式标签:256 通道:256 采样率:1152122880 每秒平均采样数:2287468800 块对齐:每采样 512 位:268461153 在 sound.formatter.WavConverter.readWav(WavConverter.java:306) 组 ID数据:taネX 数据块大小:16777216 数据流:在 sound.generator.SoundGenerator.main(SoundGenerator.java:37)
  • 我认为可能是我使用了错误的 endien 格式。

标签: java datainputstream


【解决方案1】:

原来 Wave 文件是用小端写的,而我是用大端写的。我需要实现一个反转字节数组字节的函数。 我想出了这个。

// bigToLittleEndien method
    public byte[] bigToLittleEndien(byte[] oldArray) {

        // new array
        byte[] newArray = new byte[oldArray.length];

        // reverse the order of byes
        for (int i = 0, j = oldArray.length - 1; i < oldArray.length; i++, j--) {
            newArray[i] = oldArray[j];
        }

        // return the new bytes
        return newArray;
    }

我还有其他一些小问题,但我都解决了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-10-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-02
    • 1970-01-01
    • 2022-01-15
    相关资源
    最近更新 更多