Skip to content
MPGHThe Dark Arts
/
RegisterLog in
Forum
Community
What's NewLatest posts across the boardTrendingHottest threads right nowSubscribedThreads you follow
Discussion
GeneralIntroductionsEntertainmentDebate FortFlaming & Rage
Board
News & AnnouncementsMPGH TimesSuggestions & HelpGiveaways
More Sections
Art & Graphic DesignProgrammingHackingCryptocurrency
Hacks & Cheats
Games
ValorantCS2 / CS:GOCall of Duty / WarzoneFortniteApex LegendsEscape From Tarkov
+14 moreLeague of LegendsGTA VMinecraftRustROTMGBattlefieldTroveBattleOnCombat ArmsCrossFireBlackshotRuneScapeDayZDead by Daylight
Resources
Game Hacking TutorialsReverse EngineeringGeneral Game HackingAnti-CheatConsole Game Hacking
Tools
Game Hacking ToolsTrainers & CheatsHack/Release NewsNew
Submit a release →Share your cheat, tool, or config with the community.
AINEW
AI Tools
General & DiscussionPrompt EngineeringLLM JailbreaksHotAI Agents & AutomationLocal / Open Models
AI × Gaming
AI Aimbots & VisionML Anti-CheatGame Bots & Automation
Create
AI Coding / Vibe CodingAI Art & MediaAI Voice & TTS
The AI frontier →Where game hacking meets modern machine learning. Jump in.
Marketplace
Buy & Sell
SellingBuyingTradingUser Services
Trust & Safety
Middleman LoungeMarketplace TalkVouch Copy Profiles
Money
Cryptocurrency TalkCurrency ExchangeWork & Job Offers
Start selling →List accounts, services, and goods. Use the middleman to trade safe.
MPGH The Dark Arts

A community for offensive security research, reverse engineering, and AI.

Community

ForumMarketplaceSearch

Account

RegisterLog in

Legal

Privacy PolicyForum RulesHelp & FAQ
© 2026 MPGH · All rights reserved.Built by the community, for the community. For educational purposes onlyContent is shared for security research and education — we don't condone illegal use. You're responsible for complying with applicable laws. Use at your own risk.
Home › Forum › Programming › C# Programming › Questions about streams

Thumbs upQuestions about streams

Posts 1–10 of 10 · Page 1 of 1
Laslod
Laslod
Questions about streams
Hello everyone! So recently i've been learning about streams and I have a couple of questions.

1. BinaryWriter

When I do this :

Code:
FileInfo file = new FileInfo(@"C:\Users\User\Documents\C# Stream Tests\binarywriter.dat");
            using (BinaryWriter writer = new BinaryWriter(file.OpenWrite()))
            {
                string message = "message";
                string message2 = "message2";
                
                writer.Write(message);
                writer.Write(message2);
            }
I get this in my .dat file: messagemessage2¢+ŠE

If I do this :

Code:
using (BinaryReader reader = new BinaryReader(file.OpenRead()))
            {
                MessageBox.Show(reader.ReadInt32().ToString());
                MessageBox.Show(reader.ReadString());
            }
I get a the number : 1936026887 and then it crashes and tells me "unable to read beyond the end of stream."

Now, my understanding is that int takes 4 bytes. So i'm guessing what happens here is that I write 2 strings and it stores it as bytes, then when it reads it since it's an int it'll read 4 bytes. I'm guessing each string was 2 bytes each so it read the 2 strings as one int when I told it to therefore leaving no room for reading another string. What I don't get is, when I open the file it shows "messagemessage2¢+ŠE". What's going on there? Is it "encoding" my string when I write it? If so, what does it encode it with? Unicode? But how can it read 1936026887 when I try reading an in32? It says "messagemessage". How can "messagemessage" be converted into an int? And What does ¢+ŠE mean?


2. BinaryFormatter

So I can do this:

Code:
Car car1 = new Car() { LitersFuel = 10 };
            Car car2 = new Car() { LitersFuel = 20 };
            Car car3;
            FileInfo file1 = new FileInfo(@"C:\Users\User\Documents\C# Stream Tests\CarClass1.dat");
            FileInfo file2 = new FileInfo(@"C:\Users\User\Documents\C# Stream Tests\CarClass2.dat");
            FileInfo file3 = new FileInfo(@"C:\Users\User\Documents\C# Stream Tests\CarClass3.dat");
            BinaryFormatter formatter = new BinaryFormatter();
            using (FileStream stream1 = file1.OpenWrite())
            using(FileStream stream2 = file2.OpenWrite())
            {
                formatter.Serialize(stream1, car1);
                formatter.Serialize(stream2, car2);
            }
When I go into the CarClass1.dat file , all I see is jibberish. This is what's in the CarClass1.dat file : 
" ÿÿÿÿ  OWindowsFormsApplication1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null WindowsFormsApplication1.Car LitersFuel "
That doesn't look like bytes AT ALL.

So now I do this :

Code:
            Byte[] bytes1 = File.ReadAllBytes(file1.FullName);
            byte[] bytes2 = File.ReadAllBytes(file2.FullName);

for (int i = 0; i < bytes1.Length; i++)
            {
                if (bytes1[i] != bytes2[i])
                    MessageBox.Show("Byte #" + i + " : " + bytes1[i] + " V.S " + bytes2[i]);
            }
            
             // Showed Byte #157 : 10 V.S 20

            Byte[] bytes3 = bytes1;
            bytes3[157] = 30;
            File.WriteAllBytes(file3.FullName, bytes3);
            using (FileStream stream3 = file3.OpenRead())
            {
                car3 = formatter.Deserialize(stream3) as Car;
            }
            MessageBox.Show(car3.LitersFuel.ToString());

Basically what I did is check where there was a difference between the first and second CarClass files, and where there was I just changed the value to 30 so when I deserialized it to car3 it's litersfuel field was then 30.
I have 2 questions; 1. The value 10 was stored in one byte, what if the value was larger than 255 ? Couldn't a byte not handle that? Why does it store one value in one byte then? 2. If the CarClass1.dat didn't look like a byte AT ALL, how could I store it in a bytes array, change one byte, store the newly changed byte array in a file then deserialize it into a car3 class just fine? I'm really confused by all of this so hopefully someone could come by and enlighten me.
#1 · 13y ago
abuckau907
abuckau907
1. " I'm guessing each string was 2 bytes each so it read the 2 strings as one int when I told it to therefore leaving no room for reading another string."
Hmm. Old-school way, a "c string" (array of bytes) used 1 byte PER character of the string. ie "message" = 7 bytes ( + 1 byte to store "\0", ie "empty" which is used for code that loops over strings: 0 signifies the end). So, best case scenario "messagemessage2" = 15 bytes. I think the string type in .net is, umm..Utf-16 it's called (?) - which uses, 16 bits (2 bytes) per character.
.ReadInt() should read 4 bytes from it ..I'm guessing if you look up the ascii values for "me" that's the number you posted.
-Maybe the error is happening because .ReadString( ) is expecting a "\0" at the end? ie. tries to keep reading.

"I get this in my .dat file: messagemessage2¢+ŠE" hmm..are you opening that with notepade or..? Most have an option to "set the encoding" - try setting it to utf-16? Haven't done much with unicode tho. Sometimes it's 3 bytes, sometimes it's 4 lol, I haven't had to deal with it yet : )
Maybe? All I got for now : p
#2 · edited 13y ago · 13y ago
Kuro Tenshi
Kuro Tenshi
Hmm have you ever tried to use the bytes you get from the binaryreader(idk if it is even possible with the binary reader but k) and use Encoding. Btw are there any /0's in your text that you retrieve? because this is a end statement. Also have you considered reading the HEX.

Srry this might not be much of help but based on what you want with binaryWriter (i prefer stream/memory stream abit more as you can just convert ascii to either 128 or 256 value in per byte) ... btw i hate the fact that the split used in this example is a 'Club' whilst in the notepad it looked like a '#' (I also notice a spade when i used \r\n)
. Owyeah the encoding for this UTF8 if you hover above the BinaryWriter.

Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System. IO;
using System.Windows.Forms;

namespace ConsoleApplication1
{
    class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main(string[] args)
        {
            string s1 = "text1";
            string s2 = "text2";

            SaveFileDialog sfd1 = new SaveFileDialog();
            sfd1.Filter = "Textformat|*.rtf";
            sfd1.FilterIndex = 0;
            sfd1.DefaultExt = "rtf";

            if (sfd1.ShowDialog() == DialogResult.OK)
            {
                using (BinaryWriter binWriter = new BinaryWriter(sfd1.OpenFile()))
                {
                    binWriter.Write(s1);
                    binWriter.Write(s2);
                }
            }

            FileInfo fi = new FileInfo(sfd1.FileName);

            if (fi.Exists == true)
            {

                using (BinaryReader binReader = new BinaryReader(fi.OpenRead()))
                {
                    string s = Encoding.ASCII.GetString(binReader.ReadBytes((int)binReader.BaseStream.Length));
                    String[] sArray = s.Split(new String[] { "" }, StringSplitOptions.None);

                    Console.WriteLine(s);
                    Console.WriteLine();

                    foreach (string sArrayResult in sArray)
                    {
                        Console.WriteLine(sArrayResult);
                    }
                }
            }

            Console.ReadLine();
        }
    }
}
Here is the other thing i tried to replicate ... results aren't that well as i don't know how to serialize stuff really ( haven't had the need for this) used the stuff above to replicate. I probably have some issues with the nulls at the end of the serial.

result:
Code:
resulting Array row 1
text1
text2
↑537472696E672054657374
?☻ ☺   ????☺       ♀☻   JConsoleApplication1, Version=1.0.0.0, Culture=neutral,
PublicKeyToken=null

resulting Array row 2
☺   ↔ConsoleApplication1.classTest☺
testResult☺☻   ♠♥   ▬537472696E672054657374♂

?♥FEFE01000FFFFFFFF10000000C20004A436F6E736F6C654170706C69636174696F6E312C205665
7273696F6E3D312E302E302E302C2043756C747572653D6E65757472616C2C205075626C69634B65
79546F6B656E3D6E756C6C510001D436F6E736F6C654170706C69636174696F6E312E636C6173735
46573741000A74657374526573756C74120006300016353337343732363936453637323035343635
37333734B00000000000000000000000000000000000000000000000000000000000000000000000
0000
serialized bytes:
01000FFFFFFFF10000000C20004A436F6E736F6C654170706C69636174696F6E312C205665727369
6F6E3D312E302E302E302C2043756C747572653D6E65757472616C2C205075626C69634B6579546F
6B656E3D6E756C6C510001D436F6E736F6C654170706C69636174696F6E312E636C6173735465737
41000A74657374526573756C74120006300016353337343732363936453637323035343635373337
34B000000000000000000000000000000000000000000000000000000000000000000000000000
program class:
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System. IO;
using System.Windows.Forms;
using System.Runtime.Serialization.Formatters.Binary;

namespace ConsoleApplication1
{
    class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main(string[] args)
        {
            string s1 = "text1";
            string s2 = "text2";

            classTest ct = new classTest("String Test");

            SaveFileDialog sfd1 = new SaveFileDialog();
            sfd1.Filter = "Textformat|*.rtf";
            sfd1.FilterIndex = 0;
            sfd1.DefaultExt = "rtf";

            if (sfd1.ShowDialog() == DialogResult.OK)
            {
                using (BinaryWriter binWriter = new BinaryWriter(sfd1.OpenFile()))
                {
                    binWriter.Write(s1 + Environment.NewLine);
                    binWriter.Write(s2 + Environment.NewLine);
                    binWriter.Write(ct.ToString() + Environment.NewLine);

                    string result1 = String.Empty;
                    string result2 = String.Empty;
                    MemoryStream ms = new MemoryStream();
                    BinaryFormatter formatter = new BinaryFormatter();
                    formatter.Serialize(ms, ct);
                    Byte[] buffer1 = new Byte[(int)ms.Length];
                    buffer1 = ms.GetBuffer();

                    result1 = Encoding.ASCII.GetString(buffer1);
                    foreach(byte b in buffer1)
                    {
                        result2 += b.ToString("X");
                    }
                    binWriter.Write(result1 + Environment.NewLine);
                    binWriter.Write("FEFE" + result2);
                }
            }

            FileInfo fi = new FileInfo(sfd1.FileName);

            if (fi.Exists == true)
            {

                using (BinaryReader binReader = new BinaryReader(fi.OpenRead()))
                {
                    string s = Encoding.ASCII.GetString(binReader.ReadBytes((int)binReader.BaseStream.Length));
                    String[] sArray = s.Split(new String[] { "" }, StringSplitOptions.None);

                    int row = 1;

                    foreach (string sArrayResult in sArray)
                    {
                        Console.WriteLine();
                        Console.WriteLine("resulting Array row {0}", row);
                        Console.WriteLine(sArrayResult);

                        if (sArrayResul*****ntains("FEFE"))
                        {
                            String[] sArraySerialized = sArrayResult.Split(new String[] { "FEFE" }, StringSplitOptions.None);
                            string sSerialized = sArraySerialized[1];
                            Byte[] bArraySerialized = new Byte[sSerialized.Length / 2];

                            for (int i = 0, j = 0; i < sSerialized.Length; i += 2, j++)
                            {
                                bArraySerialized[j] = Byte.Parse(sSerialized.Substring(i, 2), System.Globalization.NumberStyles.HexNumber);
                            }

                            Console.WriteLine("serialized bytes:");
                            Console.WriteLine(sSerialized);

                            MemoryStream ms = new MemoryStream(bArraySerialized);
                            BinaryFormatter formatter = new BinaryFormatter();

                            try
                            {
                                classTest ct2 = formatter.Deserialize(ms) as classTest;
                                Console.WriteLine("ct2:");
                                Console.WriteLine(ct2.ToString());
                                Console.WriteLine("ms buffer:");
                                Console.WriteLine(Encoding.ASCII.GetString(ms.GetBuffer()));
                            }
                            catch(Exception ex)
                            {
                                MessageBox.Show(ex.Message);
                            }
                        }

                        row++;
                    }
                }
            }

            Console.ReadLine();
        }
    }
}
classTest class:
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    [Serializable]
    public class classTest
    {
        private string testResult;

        public classTest(string hexResult)
        {
            Byte[] byteArray = Encoding.ASCII.GetBytes(hexResult);
            testResult = String.Empty;

            foreach (byte b in byteArray)
            {
                testResult += b.ToString("X");
            }
        }

        public override string ToString()
        {
            return testResult;
        }
    }
}
#3 · edited 13y ago · 13y ago
♪~ ᕕ(ᐛ)ᕗ
♪~ ᕕ(ᐛ)ᕗ
An example of a BinaryWriter and Reader:
Code:
Stream fStream = new FileStream( .... );
BinaryWriter writer = new BinaryWriter(fStream);
BinaryReader reader = new BinaryReader(fStream);

//Write to the file...
writer.Write(10);
writer.Write("Hello nigga!");
writer.Flush(); //Must be called.

//Read and display the file...
string[] lines = new string[] { reader.ReadInt32(),
                                        reader.ReadString() };
foreach(string l in lines) Console.WriteLine(l);
Console.Read();

Now a BinaryFormatter is used to serialize classes in array segments and places it on a stream. You should consult MSDN for further info. (This class [BinaryFormatter] will turn in really useful when you get more advanced)
#4 · 13y ago
Laslod
Laslod
Quote Originally Posted by ლ(ಠ_ಠლ) View Post



Now a BinaryFormatter is used to serialize classes in array segments and places it on a stream. You should consult MSDN for further info. (This class [BinaryFormatter] will turn in really useful when you get more advanced)
I checked msdn but it doesn't have much if not any worthwhile information. What I'm confused about is, does the binaryformatter store the classes in bytes? It says on msdn it serializes them in binary format. If they're in binary format, then how come I can read the file the binaryformatter outputed to, change some bytes around, store it in another file and it'll work just fine? Is there some conversion going on? If so how come the changed file works without converting the bytes into binary again?
#5 · 13y ago
♪~ ᕕ(ᐛ)ᕗ
♪~ ᕕ(ᐛ)ᕗ
Quote Originally Posted by Laslod View Post
I checked msdn but it doesn't have much if not any worthwhile information. What I'm confused about is, does the binaryformatter store the classes in bytes? It says on msdn it serializes them in binary format. If they're in binary format, then how come I can read the file the binaryformatter outputed to, change some bytes around, store it in another file and it'll work just fine? Is there some conversion going on? If so how come the changed file works without converting the bytes into binary again?
By "binary format" you don't really mean like a bunch of zeros and ones? If so you're wrong. The binary formatter serializes a class into a stream. You may then manipulate the class byte's within the stream or send them to another application using Sockets. It really depends on your needs.

But remember it only serializes classes marked with the attribute "[Serializable]", and you can use those classes only in the same assembly, so if you want to use the same class in different applications you must put them in a class library and then add the library as a reference.
#6 · 13y ago
Laslod
Laslod
Quote Originally Posted by ლ(ಠ_ಠლ) View Post


By "binary format" you don't really mean like a bunch of zeros and ones? If so you're wrong. The binary formatter serializes a class into a stream. You may then manipulate the class byte's within the stream or send them to another application using Sockets. It really depends on your needs.

But remember it only serializes classes marked with the attribute "[Serializable]", and you can use those classes only in the same assembly, so if you want to use the same class in different applications you must put them in a class library and then add the library as a reference.
I know the function of the binary formatter and I know how to utilize it. It says on the msdn that it's in binary format. What I want to know is, WHY can I manipulate the bytes in the file I serialized to? Is it because binary format is bytes? Or what?
#7 · 13y ago
abuckau907
abuckau907
"Is it because binary format is bytes?"
um..not to sound sarcastic, but it's all just 1's and 0's. Once it's on the disk...it doesn't matter what "you call it"...you can read (ie. interpret) it any way you'd like. -Most devices (hdd and ram) make you read at least 1 byte at a time for technical (addressing) reasons. You're free to say those bits (or maybe just 1/several of them) should be 'interpreted' as whatever data type you like. As a bunch of INTs, SHORT, *anything else*...it's all just bytes/bits, you can "pretend" it's anything..
Or is this more a question of "how do objects get serialized?" ?

-------------------------------------
"WHY can I manipulate the bytes in the file I serialized to?"
Why wouldn't you be able to manipulate the file...? -same principle as above, it's all just bits, and you can 'pretend' it's whatever type of data you want. Not really sure how else to explain it.
#8 · edited 13y ago · 13y ago
♪~ ᕕ(ᐛ)ᕗ
♪~ ᕕ(ᐛ)ᕗ
Quote Originally Posted by Laslod View Post
I know the function of the binary formatter and I know how to utilize it. It says on the msdn that it's in binary format. What I want to know is, WHY can I manipulate the bytes in the file I serialized to? Is it because binary format is bytes? Or what?
I don't really know why you're asking me this. You can because: FUCK, YOU CAN!
#9 · 13y ago
Laslod
Laslod
Quote Originally Posted by abuckau907 View Post
"Is it because binary format is bytes?"
um..not to sound sarcastic, but it's all just 1's and 0's. Once it's on the disk...it doesn't matter what "you call it"...you can read (ie. interpret) it any way you'd like. -Most devices (hdd and ram) make you read at least 1 byte at a time for technical (addressing) reasons. You're free to say those bits (or maybe just 1/several of them) should be 'interpreted' as whatever data type you like. As a bunch of INTs, SHORT, *anything else*...it's all just bytes/bits, you can "pretend" it's anything..
Or is this more a question of "how do objects get serialized?" ?

-------------------------------------
"WHY can I manipulate the bytes in the file I serialized to?"
Why wouldn't you be able to manipulate the file...? -same principle as above, it's all just bits, and you can 'pretend' it's whatever type of data you want. Not really sure how else to explain it.
THANK YOU. The above bit answered my questions. I wanted to know WHY you could change BYTES around and it'd be totally fine even though the file was written in 1's and 0's.
#10 · 13y ago
Posts 1–10 of 10 · Page 1 of 1

Post a Reply

Similar Threads

  • question about zoomBy yocinfluence in WarRock - International Hacks
    4Last post 20y ago
  • Questions about making colored modelsBy zelda803 in WarRock - International Hacks
    2Last post 20y ago
  • Question About Invisible HackBy wafflele in WarRock - International Hacks
    14Last post 20y ago
  • A question about NFV2By vomer in WarRock - International Hacks
    19Last post 20y ago
  • Questions about Torrents.By SadisticGrin in Hardware & Software Support
    19Last post 19y ago

Tags for this Thread

None