✅ How to terminate reading a set of string chars when a null character is detected.
I have a video file custom made by my company which from the start of the file and a few bytes after that, has a set of strings (a name in my case which contains the name of the person that has encoded the video). I have attached a example image of how a name typically looks from a hex editor.
What I am doing is making an app that would get the name from these bytes and display it in a textbox. I am running into some problems here in doing so.
Using StreamReader to read data from a file other than a text file is generally not recommended and so I decided to use binary reader. the problem with binary reader is that you have to specify the amount of chars to read when using the .ReadChars() method. Since names cannot always be the same fixed length, you would have to end it on seeing a null character and here in this image, the encoder name's set of bytes that I have highlighted ends with a null character.
Here is the current code that I am using:
How do I handle this problem ? is there a way by which I can end reading as soon as the null character is read in binary reader?
14 Replies
❯ Method: System.IO.BinaryReader.ReadString()
Reads a string from the current stream. The string is prefixed with the length, encoded as an integer seven bits at a time.
React with ❌ to remove this embed.
bogus
make an extension method i guess
umm..where exactly is it specifying to terminate at a null character ?
(c = br.ReadChar()) != default
oh
replace
default
with '\0'
if you want
or (char)0
Do I have to implement it as you have shown ? I thought I can input a normal code lines as I am not familiar with using methods like that.
it would be preferrable to implement it like that
there's nothing more to do than copy and paste it (preferrably in its own file)
and then just use
br.ReadStringNullTerminated()
do I have to use a separate namespace for this ?
I get an error stating that target-typed object creation is not available in C# 7.3.
so don't use it...
StringBuilder sb = new StringBuilder();
put it in any namespace you feel likeshould I include the ? after public static string. I get an error if I put the ? there.
you don't get an error
you get a warning
since you're using framework, no, don't include it
and upgrade your framework
Well I managed to implement it without a method and its all good now
Thanks for the help as usual 🙂
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.
Closed!