Java 中没有无符号数,在读字节数组转换的时候需要注意溢出的情况

import java.io.*;
import java.util.*;

public class CWav_1 {

    public static int toInt(byte[] bs) {
        return ((bs[3] << 24) + (bs[2] << 16) + (bs[1] << 8) + (bs[0] << 0));
    }

    public static short toShort(byte[] bs) {
        return (short)((bs[1] << 8) + (bs[0] << 0));
    }


    public static byte[] readBytes(RandomAccessFile rd, int pos, int length) throws IOException {
        rd.seek(pos);
        byte result[] = new byte[length];
        for (int i = 0; i < length; i++) {
            result[i] = rd.readByte();
        }
        return result;
    }


    public static void main(String[] args) throws IOException {

        File file = new File(args[0]);
        RandomAccessFile rd = new RandomAccessFile(file,"r");
        
        System.out.println("wav size: " + toInt(readBytes(rd, 4, 4)));
        System.out.println("wav format: " + toShort(readBytes(rd, 20, 2)));
        System.out.println("channels count: " + toShort(readBytes(rd, 22, 2)));
        System.out.println("sample rate: " + toInt(readBytes(rd, 24, 4)));
        System.out.println("byte rate: " + toInt(readBytes(rd, 28, 4)));
        System.out.println("block align: " + toShort(readBytes(rd, 32, 2)));
        System.out.println("bits per sample: " + toShort(readBytes(rd, 34, 2)));
        
        rd.close();
    }
}

当读到文件内部是 0x803f 时结果出错了


后来改为:

import java.io.*;
import java.util.*;

public class CWav_2 {

    public static int toInt(byte[] bs) {
        int ds[] = new int[4];
        
        for (int i = 0; i < 4; i++) {
            ds[i] = ((bs[i]) >= 0 ? bs[i] : 256 + bs[i]); 
        }

        return ((ds[3] << 24) | (ds[2] << 16) | (ds[1] << 8) | (ds[0] << 0));
    }

    public static short toShort(byte[] bs) {
        return (short)((bs[1] << 8) | (bs[0] << 0));
    }


    public static byte[] readBytes(RandomAccessFile rd, int pos, int length) throws IOException {
        rd.seek(pos);
        byte result[] = new byte[length];
        for (int i = 0; i < length; i++) {
            result[i] = rd.readByte();
        }
        return result;
    }


    public static void main(String[] args) throws IOException {

        File file = new File(args[0]);
        RandomAccessFile rd = new RandomAccessFile(file,"r");
        
        System.out.println("wav size: " + toInt(readBytes(rd, 4, 4)));
        System.out.println("wav format: " + toShort(readBytes(rd, 20, 2)));
        System.out.println("channels count: " + toShort(readBytes(rd, 22, 2)));
        System.out.println("sample rate: " + toInt(readBytes(rd, 24, 4)));
        System.out.println("byte rate: " + toInt(readBytes(rd, 28, 4)));
        System.out.println("block align: " + toShort(readBytes(rd, 32, 2)));
        System.out.println("bits per sample: " + toShort(readBytes(rd, 34, 2)));
        
        rd.close();
    }
}
Java 中没有无符号数,在读字节数组转换的时候需要注意溢出的情况 , 后来这样做结果才正确。。。。

唉~要是java有unsigned 类型结果就没这么麻烦了.....
 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值