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



lacroix_alAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mick BarryJava DeveloperCommented:
you need to handle them seperately:

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
lacroix_alAuthor Commented:
objects
is -128 = to -80?
al
lacroix_alAuthor Commented:
ok
it is
Thanks
Al
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.