C
C#3y ago
Surihia

✅ 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:
br.BaseStream.Position = EncoderNamePos;
var GetEncoderName = br.ReadChars(13);
var EncoderName = string.Join("", GetEncoderName);
br.BaseStream.Position = EncoderNamePos;
var GetEncoderName = br.ReadChars(13);
var EncoderName = string.Join("", GetEncoderName);
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
MODiX
MODiX3y ago
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.
ero
ero3y ago
WhatChamp bogus make an extension method i guess
static class BinaryReaderExt
{
public static string? ReadStringNullTerminated(this BinaryReader br)
{
StringBuilder sb = new();

char c;
while ((c = br.ReadChar()) != default)
{
sb.Append(c);
}

return sb.ToString();
}
}
static class BinaryReaderExt
{
public static string? ReadStringNullTerminated(this BinaryReader br)
{
StringBuilder sb = new();

char c;
while ((c = br.ReadChar()) != default)
{
sb.Append(c);
}

return sb.ToString();
}
}
Surihia
SurihiaOP3y ago
umm..where exactly is it specifying to terminate at a null character ?
ero
ero3y ago
(c = br.ReadChar()) != default
Surihia
SurihiaOP3y ago
oh
ero
ero3y ago
replace default with '\0' if you want or (char)0
Surihia
SurihiaOP3y ago
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.
ero
ero3y ago
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()
Surihia
SurihiaOP3y ago
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.
ero
ero3y ago
so don't use it... StringBuilder sb = new StringBuilder(); put it in any namespace you feel like
Surihia
SurihiaOP3y ago
should I include the ? after public static string. I get an error if I put the ? there.
ero
ero3y ago
you don't get an error you get a warning since you're using framework, no, don't include it and upgrade your framework
Surihia
SurihiaOP3y ago
Well I managed to implement it without a method and its all good now Thanks for the help as usual 🙂
Accord
Accord3y ago
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!
Want results from more Discord servers?
Add your server