开发者社区> 问答> 正文

C++转java代码问题

对java不太熟悉,下面的代码怎么转javad的
C++:

    fs.open("xxx", ios::binary | ios::in);
    fs.seekg(0x14);
    fs.read((char*)&WAV.channel, sizeof(WAV.channel));

还有java怎么表示没符号数

展开
收起
蛮大人123 2016-02-28 11:15:49 2936 0
1 条回答
写回答
取消 提交回答
  • 我说我不帅他们就打我,还说我虚伪

    如果没猜错的话题主是想要对wav文件进行读取操作:

    package com.chinasoft.cn;
    
    import java.io.BufferedInputStream;
    import java.io.FileInputStream;
    import java.io.IOException;
    
    @SuppressWarnings("unused")
    public class WaveFileReader {
        private String filename = null;
        private int[][] data = null;
    
        private int len = 0;
        private String chunkdescriptor = null;
        private long chunksize = 0;
        private String waveflag = null;
        private String fmtsubchunk = null;
        private long subchunk1size = 0;
        private int audioformat = 0;
        private int numchannels = 0;
        private long samplerate = 0;
        private long byterate = 0;
        private int blockalign = 0;
        private int bitspersample = 0;
        private String datasubchunk = null;
        private long subchunk2size = 0;
        private FileInputStream fis = null;
        private BufferedInputStream bis = null;
        private boolean issuccess = false;
    
        public WaveFileReader(String filename) {
    
            this.initReader(filename);
        }
    
        // 判断是否创建wav读取器成功
        public boolean isSuccess() {
            return issuccess;
        }
    
        // 获取每个采样的编码长度,8bit或者16bit
        public int getBitPerSample() {
            return this.bitspersample;
        }
    
        // 获取采样率
        public long getSampleRate() {
            return this.samplerate;
        }
    
        // 获取声道个数,1代表单声道 2代表立体声
        public int getNumChannels() {
            return this.numchannels;
        }
    
        // 获取数据长度,也就是一共采样多少个
        public int getDataLen() {
            return this.len;
        }
    
        // 获取数据
        // 数据是一个二维数组,[n][m]代表第n个声道的第m个采样值
        public int[][] getData() {
            return this.data;
        }
    
        private void initReader(String filename) {
            this.filename = filename;
    
            try {
                fis = new FileInputStream(this.filename);
                bis = new BufferedInputStream(fis);
    
                this.chunkdescriptor = readString(WaveConstants.LENCHUNKDESCRIPTOR);
                if (!chunkdescriptor.endsWith("RIFF"))
                    throw new IllegalArgumentException("RIFF miss, " + filename
                            + " is not a wave file.");
    
                this.chunksize = readLong();
                this.waveflag = readString(WaveConstants.LENWAVEFLAG);
                if (!waveflag.endsWith("WAVE"))
                    throw new IllegalArgumentException("WAVE miss, " + filename
                            + " is not a wave file.");
    
                this.fmtsubchunk = readString(WaveConstants.LENFMTSUBCHUNK);
                if (!fmtsubchunk.endsWith("fmt "))
                    throw new IllegalArgumentException("fmt miss, " + filename
                            + " is not a wave file.");
                this.subchunk1size = readLong();
                this.audioformat = readInt();
                this.numchannels = readInt();
                this.samplerate = readLong();
                this.byterate = readLong();
                this.blockalign = readInt();
                this.bitspersample = readInt();
                this.datasubchunk = readString(WaveConstants.LENDATASUBCHUNK);
                if (!datasubchunk.endsWith("data"))
                    throw new IllegalArgumentException("data miss, " + filename
                            + " is not a wave file.");
                this.subchunk2size = readLong();
    
                this.len = (int) (this.subchunk2size / (this.bitspersample / 8) / this.numchannels);
    
                this.data = new int[this.numchannels][this.len];
    
                // 读取数据
                for (int i = 0; i < this.len; ++i) {
                    for (int n = 0; n < this.numchannels; ++n) {
                        if (this.bitspersample == 8) {
                            this.data[n][i] = bis.read();
                        } else if (this.bitspersample == 16) {
                            this.data[n][i] = this.readInt();
                        }
                    }
                }
    
                issuccess = true;
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                try {
                    if (bis != null)
                        bis.close();
                    if (fis != null)
                        fis.close();
                } catch (Exception e1) {
                    e1.printStackTrace();
                }
            }
        }
    
        private String readString(int len) {
            byte[] buf = new byte[len];
            try {
                if (bis.read(buf) != len)
                    throw new IOException("no more data!!!");
            } catch (IOException e) {
                e.printStackTrace();
            }
            return new String(buf);
        }
    
        private int readInt() {
            byte[] buf = new byte[2];
            int res = 0;
            try {
                if (bis.read(buf) != 2)
                    throw new IOException("no more data!!!");
                res = (buf[0] & 0x000000FF) | (((int) buf[1]) << 8);
            } catch (IOException e) {
                e.printStackTrace();
            }
            return res;
        }
    
        private long readLong() {
            long res = 0;
            try {
                long[] l = new long[4];
                for (int i = 0; i < 4; ++i) {
                    l[i] = bis.read();
                    if (l[i] == -1) {
                        throw new IOException("no more data!!!");
                    }
                }
                res = l[0] | (l[1] << 8) | (l[2] << 16) | (l[3] << 24);
            } catch (IOException e) {
                e.printStackTrace();
            }
            return res;
        }
    
        private byte[] readBytes(int len) {
            byte[] buf = new byte[len];
            try {
                if (bis.read(buf) != len)
                    throw new IOException("no more data!!!");
            } catch (IOException e) {
                e.printStackTrace();
            }
            return buf;
        }
    
        public static int[] readSingleChannel(String filename) {
            if (filename == null || filename.length() == 0) {
                return null;
            }
            try {
                WaveFileReader reader = new WaveFileReader(filename);
                int[] res = reader.getData()[0];
                return res;
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }
    }

    第二个问题,java虽然没有无符号数,但是不管有无符号,对于二进制数据来说,在内存里存储的值是一样的,只不过打印出来时显示的值不一样,同时在做逻辑对比时有符号和无符号,虽然存储的值一样,但是“大小”可能不同。
    顺便说一下,如果你感觉java确实不熟,可以尝试一下JNI。

    2019-07-17 18:49:27
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
Spring Cloud Alibaba - 重新定义 Java Cloud-Native 立即下载
The Reactive Cloud Native Arch 立即下载
JAVA开发手册1.5.0 立即下载