Current location: Hot Scripts Forums » Programming Languages » Everything Java » Integer to binary, weird output


Integer to binary, weird output

Reply
  #1 (permalink)  
Old 05-08-07, 05:11 PM
UnrealEd's Avatar
UnrealEd UnrealEd is offline
Community Liaison
 
Join Date: May 2005
Location: Antwerp, Belgium
Posts: 3,165
Thanks: 4
Thanked 25 Times in 25 Posts
Integer to binary, weird output

I was trying to convert a simple Integer to a binary string, using the static Integer.toBinaryString(int arg0) method.
When i run the following code, a String of length 32 is outputted, while a String of length 8 suffise.

Java Code:
  1. public class Test{
  2.   public static void main(String[] args){
  3.     // output= 11111111111111111111111111111111
  4.     System.out.println(Integer.toBinaryString(255));
  5.     // output= 11111111111111111111111110010000
  6.     System.out.println(Integer.toBinaryString(144));
  7.   }
  8. }
When manually converting the binary string back to an integer, the number simply doesn't match. When converting 255 to binary, the output should be:
Code:
1111 1111
  1 * 2^7
  1 * 2^6
  1 * 2^5
  1 * 2^4
  1 * 2^3
  1 * 2^2
  1 * 2^1
  1 * 2^0
+ --------
   255
Is this method returning a wrong result (i doubt that), or am i just wrong with my calculation or interpretation?

Thanx,
UnrealEd
__________________
"Good judgement comes from experience, and experience comes from bad judgement." - Fred Brooks

Reply With Quote
  #2 (permalink)  
Old 05-08-07, 07:17 PM
King Coder King Coder is offline
Community VIP
 
Join Date: Jan 2006
Posts: 703
Thanks: 0
Thanked 0 Times in 0 Posts
I'm getting the correct output with the code you provided:

Code:
11111111
10010000

Honestly, it looks like you might not have cleared previous runs in the output window. Can you make sure this isn't happening and try a different output window?
__________________
my site
Reply With Quote
  #3 (permalink)  
Old 05-09-07, 02:15 AM
UnrealEd's Avatar
UnrealEd UnrealEd is offline
Community Liaison
 
Join Date: May 2005
Location: Antwerp, Belgium
Posts: 3,165
Thanks: 4
Thanked 25 Times in 25 Posts
i've found it. This is the code i was using:
Java Code:
  1. protected void parseAudioInformation() throws IOException {
  2.         fileStream.seek(0);
  3.         byte[] ai = new byte[4];
  4.         fileStream.read(ai);
  5.         System.out.flush();
  6.         for (int i = 0; i < 4; i++) {
  7.             int t;
  8.             if ((int) ai[i] < 0) {
  9.                 t = (ai[i] + 256);
  10.             } else {
  11.                 t = ai[i];
  12.             }
  13.             System.out.println(t + " = " + Integer.toBinaryString(ai[i]));
  14.         }
  15.     }
It reads 4 bytes from an mp3 file. The code in my first post was just an example of the bytes i recieved. As bytes range from -127 to 127, and not 0 to 255, i was getting the binary string for "-1", and not "255". I just needed to change the input parameter of the function to "t" instead of "ai[i]"

It's working now i changed it,
Thanks
__________________
"Good judgement comes from experience, and experience comes from bad judgement." - Fred Brooks

Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
TreeView Control - Background Image? tim8w Windows .NET Programming 1 04-11-07 12:43 PM
Classified Ads skipper23 Perl 3 11-22-05 02:22 AM
ASP upload prob minority ASP 1 06-27-05 08:35 AM
binary in javascript bogomil JavaScript 1 02-09-05 11:02 AM
Classified Ads skipper23 Perl 2 12-30-03 03:43 AM


All times are GMT -5. The time now is 05:42 PM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.