?
Solved

Long.toHexString() not coverting correctly

Posted on 2006-04-23
3
Medium Priority
?
271 Views
Last Modified: 2010-03-31
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
Comment
Question by:lacroix_al
  • 2
3 Comments
 
LVL 92

Accepted Solution

by:
objects earned 2000 total points
ID: 16521260
you need to handle them seperately:

if (x>=0)
{
   System.out.println(Long.toHexString(x));
}
else
{
   System.out.println("-"+Long.toHexString(-x));
}
0
 

Author Comment

by:lacroix_al
ID: 16521482
objects
is -128 = to -80?
al
0
 

Author Comment

by:lacroix_al
ID: 16521485
ok
it is
Thanks
Al
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses
Course of the Month15 days, 18 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question