Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Encoding.Default.GetString

Status
Not open for further replies.

woyler

Programmer
Jun 20, 2001
678
US
Hi all,
When using the GetString method to decode bytes into a string, I am having an issue where the string is incomplete. The decode works fine and the return string can be used as a value into a textbox or messagebox, but not for reference to a file path or anything of that nature. I am reading the tags from MP3's and want to create a directory for each artist. I can get the artist name, but if I try to refer to the value to create the directory, it is incomplete. example;

Code:
Private b_Artist(30) As Byte

dim MP3File = New System.IO.FileStream(p_FilePath, IO.FileMode.Open, FileAccess.Read)

MP3File.Read(b_Artist, 0, b_Artist.Length - 1)
dim s_Artist AS String = Encoding.Default.GetString(b_Artist, 0, b_Artist.Length - 1)
If the artist is The Beatles the string returned is literally "The Beatles not "The Beatles". It is missing the end quotation marks. I can place the value into a textbox, but can not create a directory from the value.
Code:
IO.File.CreateDirectory("C:\" & s_Artist)
returns an illegal characters in path error.

What is my malfunction?
 
Use some Replace function to remove illegal chars from "s_Artist" then create a directory.

Zameer Abdulla
 
I thought the GetString method removed the empty bytes from the array. Guess not.
 
You can't create a file or folder with following chars

\/:*?"<>|

Remove/replace them before you create the dir
also you need to have a default name if s_Artist is null.

Zameer Abdulla
 
That is my point. None of those characters exists in the string. The issue is the fact that the empty bytes are at the end of the returned string. The only way around it I have found is to check the value of each byte in the array until I find the first empty byte. Then redim and preserve the array variable. Then it works. I dont think this is the best practice though. I thought the GetString method cleaned the array for you.
 
Did you simply try
Code:
IO.File.CreateDirectory("C:\" & s_Artist[COLOR=#ff0000][b].Trim()[/b][/color])
 
I have. As well as any other string manipulation I could think of. Even after a Trim, the Length of the string variable always equals that of the original array declaration. In this cae 30. so using the example above;
Code:
dim s_Artist AS String = Encoding.Default.GetString(b_Artist, 0, b_Artist.Length - 1)

s_artist.trim.length would equal 30
 
Then you try replacing the space with padLeft/padright functions of string before you add it to the array.
Code:
        Dim fldrName As String
        fldrName = Me.TextBox1.Text
        MessageBox.Show(fldrName.PadRight(30 - fldrName.Length, "_"))

Zameer Abdulla
 
ZmrAbdulla,
Thank you for the responses. But I think my point is being missed. The underlying question is how the decode method handles empty byte elements in an array. It is not coming through as a space. It is an empty sting, or ASCII NULL (ascii value is 0). you cannot do a replace on that value.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top