[Help] Byte array to string, Then in XML (LINQ)

Posts 1–14 of 14 · Page 1 of 1
[Help] Byte array to string, Then in XML (LINQ)
I keep getting an error from this... some UNICODE ERROR!!!!!!!


[highlight="vb.net"]
Public Overrides Function ToString() As String
Dim file = My.Computer.FileSystem.ReadAllBytes("example.exe")
Dim fileR = System.Text.Encoding.ASCII.GetString(File)
Dim XML = <root>
<File><%= fileR %></File>
</root>
Return XML.ToString()
End Function
[/highlight]
By the way, you forgot to enclose your string with string deliminators.
Quote Originally Posted by freedompeace View Post
By the way, you forgot to enclose your string with string deliminators.
thanks but even if i do that XML.ToString() still gives me an error.
Quote Originally Posted by topblast View Post


thanks but even if i do that XML.ToString() still gives me an error.
As a coder, as someone who is supposed to know things about technology and programming, aren't you forgetting something here?

("I got an error.")
What's the end goal with this? Chances are there's a simpler method. Seems overkill to manually build an XML structure, then convert it back to string
Quote Originally Posted by freedompeace View Post


As a coder, as someone who is supposed to know things about technology and programming, aren't you forgetting something here?

("I got an error.")
I tried over and over and over to get this thing to work I keep falling on my ass. It keeps saying Invalid Char ' ', hex x1F


Quote Originally Posted by Jason View Post
What's the end goal with this? Chances are there's a simpler method. Seems overkill to manually build an XML structure, then convert it back to string
I am using XML because well they is more information in that, well really it is for my Blue Class To String. And it really is formed as an XML in file format but the size of it increases a LARGE AMOUNT and i got it down to this as the problem.
Quote Originally Posted by topblast View Post


I tried over and over and over to get this thing to work I keep falling on my ass. It keeps saying Invalid Char ' ', hex x1F




I am using XML because well they is more information in that, well really it is for my Blue Class To String. And it really is formed as an XML in file format but the size of it increases a LARGE AMOUNT and i got it down to this as the problem.
Meaning that one of the bytes in the file is invalid as an ASCII string, which is what you're trying to convert the bytes to. The ASCII string range is from 0 to 128 (if i remember correctly). Bytes are from 0 to 255.
Quote Originally Posted by topblast View Post


I tried over and over and over to get this thing to work I keep falling on my ass. It keeps saying Invalid Char ' ', hex x1F




I am using XML because well they is more information in that, well really it is for my Blue Class To String. And it really is formed as an XML in file format but the size of it increases a LARGE AMOUNT and i got it down to this as the problem.
What exactly does "Blue Class To String" comprise of? As I said before, chances are XML isn't even the best option. Without knowing exactly what you're trying to read from, I can't tell you a definitive answer.
Quote Originally Posted by Jason View Post


What exactly does "Blue Class To String" comprise of? As I said before, chances are XML isn't even the best option. Without knowing exactly what you're trying to read from, I can't tell you a definitive answer.

[highlight="vb.net"]
Public Overrides Function ToString() As String
'Me.Name String
'Me.Creator String
'Me.EXT string
'Me.File Byte()
Dim XML = <root>
<Name><%= Me.Name %></Name>
<Creator><%= Me.Creator %></Creator>
<Extension><%= Me.EXT %></Extension>
<File><%= String.Join(" ", Me.File) %></File>
</root>
Return XML.ToString()
End Function
[/highlight]
Quote Originally Posted by topblast View Post



[highlight="vb.net"]
Public Overrides Function ToString() As String
'Me.Name String
'Me.Creator String
'Me.EXT string
'Me.File Byte()
Dim XML = <root>
<Name><%= Me.Name %></Name>
<Creator><%= Me.Creator %></Creator>
<Extension><%= Me.EXT %></Extension>
<File><%= String.Join(" ", Me.File) %></File>
</root>
Return XML.ToString()
End Function
[/highlight]
No need to manually make an XElement then. You're converting it instantly to string which gives an exact replica of what you hardcoded to the user, so you may as well just hardcode it as string in the first place:

[highlight=vb.net]
Public Overrides Function ToString() As String
Dim thisName As String = String.Concat("<Name>", Me.Name, "</Name>")
Dim thisCreator As String = String.Concat("<Creator>", Me.Creator, "</Creator>")
Dim thisExtension As String = String.Concat("<Extension>", Me.EXT, "</Extension>")
Dim thisBytes As String = String.Concat("<File>", String.Join(" ", Array.ConvertAll(Me.File, Function(b As Byte) b.ToString)), "</File>")

Dim output As String = String.Join(ControlChars.Lf, New String() {"<root>", thisName, thisCreator, thisExtension, thisBytes, "</root>"})
Return output
End Function
[/highlight]
no but I also want it smaller.. I currently using the code i showed above and it works but I want to be able to convert to string and Back with no mistakes

PS the XML way is Much easier compared to what you have.
topblast, i honestly think you need to go back and read up on what XML is. Jason posted XML (not a non-"XML way"), and the problems with your code have been pointed out time and time again.
Your way was easier? You manually wrote out an XElement, converted it instantly to string (not Value, just ToString, which returns exactly what my method did, without any conversions.) There is really NO NEED for it to be that complicated lol. As for the bytes, you're implicitly converting them, chances are you should explicitly convert the array to a compatible data-type.
Quote Originally Posted by Jason View Post
Your way was easier? You manually wrote out an XElement, converted it instantly to string (not Value, just ToString, which returns exactly what my method did, without any conversions.) There is really NO NEED for it to be that complicated lol. As for the bytes, you're implicitly converting them, chances are you should explicitly convert the array to a compatible data-type.
I was away for a while. thanks you your help
Posts 1–14 of 14 · Page 1 of 1
This thread is closed for replies.

Tags for this Thread

None

Talk with us