Register for your free account! | Forgot your password?

Go Back   elitepvpers > Coders Den > Coding Releases > Coding Snippets
You last visited: Today at 21:33

  • Please register to post and access all features, it's quick, easy and FREE!

Advertisement



[C#] Write Hex to Binary File & Compare binary & hex

Discussion on [C#] Write Hex to Binary File & Compare binary & hex within the Coding Snippets forum part of the Coding Releases category.

Reply
 
Old   #1
 
Epanias's Avatar
 
elite*gold: 0
Join Date: Sep 2011
Posts: 41
Received Thanks: 18
Post [C#] Write Hex to Binary File & Compare binary & hex

For those who like to make a binary hack (write hexadecimal to a specified adress in a binary file) and use a checkbox - checker if the hack has already been enabled or not, here are two usefull functions:

This code snippets require the IO namespace.

Code:
using System.IO;
To use this functions, you'll need 3 other ones that were not written by me, but unfortunatelly I couldn't find the original author of them again, sorry.

Code:
public static byte[] ConvertToByteArray(string value)
        {
            byte[] bytes = null;
            if (String.IsNullOrEmpty(value))
                bytes = Empty;
            else
            {
                int string_length = value.Length;
                int character_index = (value.StartsWith("0x", StringComparison.Ordinal)) ? 2 : 0; // Does the string define leading HEX indicator '0x'. Adjust starting index accordingly.               
                int number_of_characters = string_length - character_index;

                bool add_leading_zero = false;
                if (0 != (number_of_characters % 2))
                {
                    add_leading_zero = true;

                    number_of_characters += 1;  // Leading '0' has been striped from the string presentation.
                }

                bytes = new byte[number_of_characters / 2]; // Initialize our byte array to hold the converted string.

                int write_index = 0;
                if (add_leading_zero)
                {
                    bytes[write_index++] = FromCharacterToByte(value[character_index], character_index);
                    character_index += 1;
                }

                for (int read_index = character_index; read_index < value.Length; read_index += 2)
                {
                    byte upper = FromCharacterToByte(value[read_index], read_index, 4);
                    byte lower = FromCharacterToByte(value[read_index + 1], read_index + 1);

                    bytes[write_index++] = (byte)(upper | lower);
                }
            }

            return bytes;
        }

        private static byte FromCharacterToByte(char character, int index, int shift = 0)
        {
            byte value = (byte)character;
            if (((0x40 < value) && (0x47 > value)) || ((0x60 < value) && (0x67 > value)))
            {
                if (0x40 == (0x40 & value))
                {
                    if (0x20 == (0x20 & value))
                        value = (byte)(((value + 0xA) - 0x61) << shift);
                    else
                        value = (byte)(((value + 0xA) - 0x41) << shift);
                }
            }
            else if ((0x29 < value) && (0x40 > value))
                value = (byte)((value - 0x30) << shift);
            else
                throw new InvalidOperationException(String.Format("Character '{0}' at index '{1}' is not valid alphanumeric character.", character, index));

            return value;
        }

        private static readonly byte[] Empty = new byte[0];
To write from a hexadecimal string to a binary file, you can use this function:

Code:
        private void WriteToFile(string Filename, string Text, int Position)
        {
            byte[] ContentToWrite;
            ContentToWrite = ConvertToByteArray(Text);

            BinaryWriter BinW = new BinaryWriter(File.OpenWrite(Filename));
            BinW.BaseStream.Position = Position;
            BinW.Write(ContentToWrite);
            BinW.Close();
        }
To check if the hack is already enabled, you can use this function (Text=hex):

Code:
        private bool ReadNCompare(string Filename, string Text, int Position)
        {
            byte[] Readtext = { };
            string Content = "";
            byte[] Converted = ConvertToByteArray(Text);
            string Trimmed = "";
            int Comparation = 0;

            BinaryReader binRead = new BinaryReader(File.OpenRead(Filename));

            binRead.BaseStream.Position = Position;
            Readtext = binRead.ReadBytes(Converted.Length);
            binRead.Close();

            Content = BitConverter.ToString(Readtext);
            Trimmed = Content.Replace("-", "");
            Comparation = Trimmed.IndexOf(Text);

            if (Comparation != -1)
                return true;
            else
                return false;
        }
I hope this helps you. I haven't seen codesnippets for that here yet, if there is already a thread for it, than I apologize.

Greetings
Epanias
Epanias is offline  
Reply


Similar Threads Similar Threads
Understand binary and how to write it
03/12/2013 - Coding Tutorials - 5 Replies
I have no idea where this should be posted actually rofl. I was a bit bored and though I'd do a quick tutorial on how to read and write binary, but as well how to write messages in it. First of all I'm sure you all know how binary works. It's just a bunch of 1's and 0's. Well no, it's not just a bunch of 0's and 1's. These numbers represents something which is called bits. Some people confuses bits with bytes, but they're not the same. A byte is actually 8 bits. So what are the 0's and...
Problem with a binary file
08/05/2012 - CO2 Private Server - 0 Replies
Hi The source I'm working with is loading mobs from a binary file and I'm having trouble editing the mobs info/stat/name/hp and all that I tried editing it but everytime the source wouldn't load it , It only loads the un-edited file the original one. Here's the file if anyone could help me or show me how to edit/decyrpt it that would be great Here is the file Thanks.
[Relese]Tq Binary translation 98% + all Binary Files
07/07/2009 - CO2 Private Server - 20 Replies
#Removed



All times are GMT +2. The time now is 21:33.


Powered by vBulletin®
Copyright ©2000 - 2023, Jelsoft Enterprises Ltd.
SEO by vBSEO ©2011, Crawlability, Inc.
This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Support | Contact Us | FAQ | Advertising | Privacy Policy | Terms of Service | Abuse
Copyright ©2023 elitepvpers All Rights Reserved.