I'm confused as a java dev trying his way into C#. I've read about the string type and it being immutable and such , not much different from java except that it doesn't seem to be an object like there but I'm getting weird behavior regardless. I have following toString method on a class
public override string ToString() { StringBuilder builder = new StringBuilder(); builder.Append("BlockType: "); builder.Append(BlockType + "\n"); //builder.Append(System.Text.ASCIIEncoding.ASCII.GetChars(Convert.FromBase64String("dHh0AA=="))); //builder.Append("\n"); builder.Append("BlockName: "); builder.Append(BlockName + "\n"); //builder.Append(System.Text.ASCIIEncoding.ASCII.GetChars(Convert.FromBase64String(this.BlockName))); //builder.Append("\n"); builder.Append("BlockLength: " + this.BlockLength + "\n"); builder.Append("pBlockData: " + this.pBlockData + "\n"); return builder.ToString(); } When I fill it with data. Taking in account that BlockType and BlockName will contain a Base64 String. I get following result
FileVersionNo: 0 nx: 1024 ny: 512 TileSize: 256 HorizScale: 10 Precis: 0,01 ExtHeaderLength: 35 nExtHeaderBlocks: 1 pExtHeaderBlocks: System.Collections.Generic.LinkedList`1[LibFhz.HfzExtHeaderBlock] BlockType: dHh0AA== BlockName: YXBwLW5hbWUAAAAAAAAAAA== BlockLength: 11 pBlockData: System.Byte[] Which is perfect exactly what I want, however when I try to get the ASCII value of those Base64 (or UTF-8, I tried both) I get the following result
FileVersionNo: 0 nx: 1024 ny: 512 TileSize: 256 HorizScale: 10 Precis: 0,01 ExtHeaderLength: 35 nExtHeaderBlocks: 1 pExtHeaderBlocks: System.Collections.Generic.LinkedList`1[LibFhz.HfzExtHeaderBlock] BlockType: txt The code just seems to stop, without error or stacktrace. I have no idea what is going on. I thought first that a \0 is missing so I've added it to the string, then I thought I need a \r\n ... again not the sollution, I started to google with people just wanting to know how to do a Bas64 to UTF-8 conversion ... but that part seems easy ... this code stop isn't.
Any insights or links to decent articles about string handling in .net would be appreciated
Convert.FromBase64String()is likely returning binary zero values, which are getting converted to ASCII NULL characters. I imagine that might mess up the output. What output are you expecting?