Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Java : Convert string to binary array?

Options
  • 03-04-2005 11:05pm
    #1
    Registered Users Posts: 2,013 ✭✭✭


    Hi,

    Working on a college project here. I have converting from a binary array(containing mp3 id3 tag information, artists,title,album etc) to a String working no problem, but how do I go about writing a String to a binary array?

    Hope that's enough info.

    Thanks :)


Comments

  • Registered Users Posts: 261 ✭✭HaVoC


    getBytes Should do it


  • Registered Users Posts: 2,013 ✭✭✭SirLemonhead


    Thanks! :)

    How do I write these bytes to a certain position in a binary file? (i need to write the id3 tag information in the last 128 bytes of the mp3 file)

    I've found the write method for DataOutputStream but this will only let me write the bytes to the start of the file, which isn't what I want.


  • Closed Accounts Posts: 1,502 ✭✭✭MrPinK


    Take a look at RandomAccessFile. It lets you read/write at any point in a file.
    RandomAccessFile raf = new RandomAccessFile("myfile.dat", "rw");
    raf.seek(raf.length() - 128);
    raf.writeBytes("Your String here");
    raf.close();
    


  • Closed Accounts Posts: 92 ✭✭tempest


    HaVoC wrote:
    getBytes Should do it

    Be careful though as this returns the Bytes in the platform default encoding. So on Windows in Ireland it will probably be ISO-8859-1 whereas if someone in the Estonia or the like were to run you program it would likely be in ISO-8859-2.

    Dangerous territory. :(
    As you don't seem to be having problems with reading the bytes into a String then the platform encoding appears to be ok, but you might find that it craps all over the place if someone else uses it.

    Be Warned....


  • Registered Users Posts: 2,013 ✭✭✭SirLemonhead


    Thanks guys. RandomAccessFile seems to be working somewhat ok.

    It's a college project so the lecturer will be the only one running it, so I don't think the encoding thing should be a problem.

    I've got one final little problem with the project..

    public void writeTagMethod()
    {
    
    	String title = new String();
    	String artist = new String();
    	String album = new String();
    	String year = new String();
    	String comment = new String();
    	String tag = new String();
    
    	tag = "TAG";
    	//int genre;
    
    	title = titleField.getText();
    	artist = artistField.getText();
    	album = artistField.getText();
    	year = yearField.getText();
    	comment = commentsField.getText();
    //	genre = genreCombo.getSelectedIndex();
    
    	String filenme = new String();
    
        filenme = dirName + "\\" + mp3list.getSelectedValue();
    
    	//byte b[] = new byte[128];
    
    	byte btag[] = new byte[3];
    	byte btitle[] = new byte[30];
    	byte bartist[] = new byte[30];
    	byte balbum[] = new byte[30];
    	byte byear[] = new byte[4];
    	byte bcomment[] = new byte[30];
    	byte bgenre[] = new byte[0];
    
    	btag = tag.getBytes();
    	btitle = title.getBytes();
    	bartist = artist.getBytes();
    	balbum = album.getBytes();
    	byear = year.getBytes();
    	bcomment = comment.getBytes();
    
    try
    {
    	File mp3 = new File(filenme);
    
    	RandomAccessFile raf = new RandomAccessFile(filenme, "rw");
    
    		raf.seek(raf.length() - 128);
    
    		raf.write(btag);
    		raf.write(btitle);
    		raf.write(bartist);
    		raf.write(balbum);
    		raf.write(byear);
    		raf.write(bcomment);
    
    		raf.close();
    }
    	catch ( IOException e )
    
    		{
    		  System.out.println( "Couldn't open the files: " + e );
    		}
    
    
    }
    

    Say I load in an mp3 file, Click read id3 tags (this works perfectly)

    Now, If i make a change, or even if I don't make any changes and click write id3 to write the original data, upon re-reading the updated tag the following happens:

    The artist name starts getting written onto the end of the title.
    The year appears in the artist field, along with various characters from the original artist name.

    Can anyone see any problems with how this data is being written to the file? it seems some data is somehow invading other fields in the id3 tag...


  • Advertisement
  • Registered Users Posts: 1,994 ✭✭✭lynchie


    If I remember properly, each element in the tag is a finite length. So you need to pad out each of your elements with spaces up to the length of the field. e.g. artist is 128 characters long, but if you only enter "U2" instead of "U2 ...." then it will corrupt the fields


  • Registered Users Posts: 2,013 ✭✭✭SirLemonhead


    I had thought of that, but didn't know how to go about doing it?

    Would you be able to tell me how please? Kinda running low on time :o


  • Closed Accounts Posts: 1,502 ✭✭✭MrPinK


    while your string length is less than 30, add a space to the end of it


  • Registered Users Posts: 2,013 ✭✭✭SirLemonhead


    Its working now :D

    Thanks a million for the help :)


Advertisement