• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 274
  • Last Modified:

Long.toHexString() not coverting correctly

Hi,
I have this code.


import java.io.*;

public class Project_LaCroix_Al {
      private static long[] m = {1,256,256^2,256^3};
//initiate variables
      private String RIFF_H = "";
      private long RIFF_S;
      private String WAV_H = "";
      private String CK_ID = "";
      private long n_Chunk_Size;
      private int w_Format_Tag;
      private int n_Channels;
      private long n_Samples_Per_Sec;
      private long n_Avg_Bytes_Per_Sec;
      private int n_Block_Align;
      private int n_Bits_Per_Sample;
        private long n_Samples;
      private String data_Tag = "";
      private BufferedInputStream wav_Stream;
      private int n_Bytes = 0;
      private int bytes_Read = 0;
      private int[] File_H;
        private int H_Size=0;
        
//gets the number of channels      
      public int get_n_Channels() {
            return n_Channels;
      }
//returns head size
      public byte[] get_File_H() {
    byte[] head = new byte[H_Size];
    for (int i=0; i<H_Size; i++)
      head[i] = ((byte)File_H[i]);
            return head;
      }
//gets sample rate
      public long get_Sampling_Rate() {
            return n_Samples_Per_Sec;
      }
//gets bits per sample
      public int get_n_Bits_Per_Sample() {
            return n_Bits_Per_Sample;
      }
//gets nuber of samples
      public long get_n_Total_Samples() {
            return n_Samples;
      }
//gets bytes per sample
      public int get_Bytes_Per_Sample() {
            return n_Bits_Per_Sample/8;
      }
//closes the input stream
      public void close() {
            try {
                  wav_Stream.close();
            }
            catch (IOException e) {
                  System.err.println(e);
            }
      }
//reads input stream
      public Project_LaCroix_Al(InputStream in) {
            wav_Stream = new BufferedInputStream(in);
            try {
                  n_Bytes = wav_Stream.available();
            }
            catch (IOException e) {
                  System.err.println(e);
            }
            try {
                  File_H = new int[1024];

                  int i = 0;
                  bytes_Read = 0;
                  
                  //reads in the RIFF header
                  for (; i<4; ++i) {
                        File_H[i] = (int)wav_Stream.read();
                        RIFF_H += (char)File_H[i];
                        ++bytes_Read;
                  }

                  //reads in RIFF size
                  for (; i<8; ++i) {
                        File_H[i] = (int)wav_Stream.read();
                        RIFF_S += File_H[i]*m[i-4];
                        ++bytes_Read;
                  }

                  //reads in WAVE header
                  for (; i<12; ++i) {
                        File_H[i] = (int)wav_Stream.read();
                        WAV_H += (char)File_H[i];
                        ++bytes_Read;
                  }
                  
                  //reads in chunk ID
                  for (; i<16; ++i) {
                        File_H[i] = (int)wav_Stream.read();
                        CK_ID += (char)File_H[i];
                        ++bytes_Read;
                  }
                  //reads in chunk size
                  for (; i<20; ++i) {
                        File_H[i] = (int)wav_Stream.read();
                        n_Chunk_Size += File_H[i]*m[i-16];
                        ++bytes_Read;
                  }
                  //reads in tag
                  for (; i<22; ++i) {
                        File_H[i] = (int)wav_Stream.read();
                        w_Format_Tag += File_H[i]*m[i-20];
                        ++bytes_Read;
                  }
                  //reads in channels
                  for (; i<24; ++i) {
                        File_H[i] = (int)wav_Stream.read();
                        n_Channels += File_H[i]*m[i-22];
                        ++bytes_Read;
                  }
                  //reads in samples per second
                  for (; i<28; ++i) {
                        File_H[i] = (int)wav_Stream.read();
                        n_Samples_Per_Sec += File_H[i]*m[i-24];
                        ++bytes_Read;
                  }
                  //reads in ave bytes per second
                  for (; i<32; ++i) {
                        File_H[i] = (int)wav_Stream.read();
                        n_Avg_Bytes_Per_Sec += File_H[i]*m[i-28];
                        ++bytes_Read;
                  }
                  //reads in block alignment
                  for (; i<34; ++i) {
                        File_H[i] = (int)wav_Stream.read();
                        n_Block_Align += File_H[i]*m[i-32];
                        ++bytes_Read;
                  }
                  //reads in bits per sample
                  for (; i<36; ++i) {
                        File_H[i] = (int)wav_Stream.read();
                        n_Bits_Per_Sample += File_H[i]*m[i-34];
                        ++bytes_Read;
                  }

                  //checks to if the whole fmt chunk has been read
                  for(; i < n_Chunk_Size+20; ++i) {
                        File_H[i] = (int)wav_Stream.read();
                        ++bytes_Read;
                  }

                  //checks to see if the next chunk header is DATA
                  int di;
                  boolean Data_Chunk = false;
                  String Chunk_Name = "";
                  while(!Data_Chunk) {
                        Chunk_Name = "";
                        di = i+4;
                        for(; i<di; ++i) {
                              File_H[i] = (int)wav_Stream.read();
                              ++bytes_Read;
                              Chunk_Name += (char)File_H[i];
                        }
                        Data_Chunk = (0 == Chunk_Name.compareTo("data"));
                        
                        //System.out.print("ischunk: "+Data_Chunk+"\n");
                        if(! Data_Chunk) {
                          System.out.print("Ignoring chunk:"+ Chunk_Name +"\n");
                              int n_Chk_Sz = 0;
                              di = i+4;
                              for (; i<di; ++i) {
                                    File_H[i] = (int)wav_Stream.read();
                                    
                                    //System.out.print(File_H[i]+ "\n");
                                    n_Chk_Sz += File_H[i]*m[i-di+4];
                                    ++bytes_Read;
                              }
                              
                              //System.out.print("chunkSize: " + n_Chk_Sz + "\n");
                              di = i+n_Chk_Sz;
                              for (; i<di; ++i) {
                                    File_H[i] = (int)wav_Stream.read();
                                    ++bytes_Read;
                              }
                        }
                  }
//calculates the number of samples
      n_Samples = (RIFF_S - bytes_Read + 8)/(n_Channels * (n_Bits_Per_Sample/8));
      H_Size = bytes_Read;
        data_Tag = Chunk_Name;

            }
            catch (IOException e) {
                  System.err.println(e);
            }
//prints header info
            System.out.print("RIFF Header: " + '"' + RIFF_H + '"');
            System.out.print(" " + RIFF_S);
            System.out.println(" (File Size = " + n_Bytes + ")");
            System.out.println("WAVE string: " + '"' + WAV_H + '"');
            System.out.println("CK_ID: " + '"' + CK_ID + '"');
            System.out.println("n_Chunk_Size: " + n_Chunk_Size);
            System.out.println("w_Format_Tag: " + w_Format_Tag);
            System.out.println("n_Channels: " + n_Channels);
            System.out.println("n_Samples_Per_Sec: " + n_Samples_Per_Sec);
            System.out.println("n_Avg_Bytes_Per_Sec: " + n_Avg_Bytes_Per_Sec);
            System.out.println("n_Block_Align: " + n_Block_Align);
            System.out.println("n_Bits_Per_Sample: " + n_Bits_Per_Sample);
            System.out.println("data begin: " + '"' + data_Tag + '"');
            System.out.println("");
      }
//gets next byte
      public byte get_Next_WAV_Byte() {
            try {
                  ++bytes_Read;
                  return (byte)wav_Stream.read();
            }
            catch (IOException e) {
                  System.err.println(e);
            }
            return 0;
      }
//Checks to see if it is done reading the file
      public boolean Blocked() {
            if (bytes_Read >= n_Bytes)
                  close();
            return bytes_Read >= n_Bytes;
      }
//reads next sample amplitude
      public long getNextWAVAmplitude() {
            long amp = 0;
            try {
                  for (int i=0; i<n_Bits_Per_Sample/8; ++i)
                        amp += wav_Stream.read()*m[i];
            }
            catch (IOException e) {
                  System.err.println(e);
            }
            amp -= 128;
            return amp;
      }

      public long[] getChunk(int size) {
            long[] chunk = new long[size];
            for (int i=0; i<size; ++i)
                  chunk[i] = getNextWAVAmplitude();
            return chunk;
      }

      public static void main(String[] args) {
            WAVReader a = null;
            try {
                  a = new WAVReader(new FileInputStream("C:/dsclip.wav"));
            }
            catch (FileNotFoundException e) {
                  System.err.println(e);
            }
//tracks the largest and smallest amplitude
      long maxx = Long.MIN_VALUE;
      long minx = Long.MAX_VALUE;
      int mini=0, maxi=0;

         for (int i=0; i<10009; i++){  
               long x = a.getNextWAVAmplitude();
               
 //to prove that the results are correct uncomment the following statement
//********* warning this will use a lot of memory  *************************              
     //System.out.println("amp= "+x+"@"+i);
                             
               if (x<minx) {
                 minx = x;
                 mini = i;
                           }
                           
               if (x>maxx) {
                 maxx = x;
                 maxi = i;
                           }
            }
                                                
//prints the minimum and maximum amplitude in int and Hex values                        
                        System.out.println("Min Amplitude as int= "+minx+"\t"+"@"+mini);
                        System.out.println("Max Amplitude as int= "+maxx+"\t"+"@"+maxi);
                        System.out.println();
                        System.out.println();
                        
                        System.out.println("Min Amplitude as Hex= "+Long.toHexString(minx)+"\t"+"@"+Integer.toHexString(mini));
                    System.out.println("Max Amplitude as Hex= "+Long.toHexString(maxx)+"\t"+"@"+Integer.toHexString(maxi));
                        System.out.println();
                        
}
}


It compiles fine.
I want to be able to print out the value of maxx and minx as a Hex.
I tried to use the Long.toHexString().
it returns the wrong value.
I think this is do to Long.toHexString() only returns the unsigned value.
exp:

Min Amplitude as int= -128      @2
Max Amplitude as int= 127       @6

Min Amplitude as Hex= ffffffffffffff80  @2
Max Amplitude as Hex= 7f        @6

how do I print -128 in Hex? or any other negitive value for that matter.

Thanks
Al



0
lacroix_al
Asked:
lacroix_al
  • 2
1 Solution
 
objectsCommented:
you need to handle them seperately:

if (x>=0)
{
   System.out.println(Long.toHexString(x));
}
else
{
   System.out.println("-"+Long.toHexString(-x));
}
0
 
lacroix_alAuthor Commented:
objects
is -128 = to -80?
al
0
 
lacroix_alAuthor Commented:
ok
it is
Thanks
Al
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now