| | | Welcome to XboxMB - Xbox Message Boards | | Home of the Ultimate Xbox 360 Modding Tool, Horizon. XboxMB.com is a community of Xbox 360 gamers and modders who share Tutorials, News, Reviews, and other resources. Xbox Message Boards is free to sign up and use, so what are you waiting for? Register Now! | | | | |  |
04-15-2011
|
#1 (permalink)
| | | BlueJ Join Date: Sep 2010 Location: New York
Posts: 1,343
Thanks: 1,031 | | | Add bytes to a file? Hi, I was wondering how I could add bytes to a file. For example I want to add let's say 16 bytes to the file at the offset 0x20. I don't want to write over existing ones, I need to add them to the file so it will be larger. Any help would be appreciated, thanks. | | | |  |
04-15-2011
|
#2 (permalink)
| Regular Member | | Join Date: Nov 2010 Location: Maryland
Posts: 1,915
Thanks: 1,191 | | | Re: Add bytes to a file? What kind of file?
be more elaborate | | | |  |
04-15-2011
|
#3 (permalink)
| | | BlueJ Join Date: Sep 2010 Location: New York
Posts: 1,343
Thanks: 1,031 | | | Re: Add bytes to a file? Quote:
Originally Posted by Retrix What kind of file?
be more elaborate | Any file.
This:
To this: | | | |  |
04-15-2011
|
#4 (permalink)
| | | x &= ~0; Join Date: Jul 2010 Location: New York
Posts: 6,680
Thanks: 16,730 | | | Re: Add bytes to a file? You can't really. You shouldn't have to either.
You have to make a new file or overwrite everything.
__________________ | | | |  |
04-15-2011
|
#5 (permalink)
| | | Join Date: Oct 2010 Location: UK
Posts: 1,071
Thanks: 1,464 | | | Re: Add bytes to a file? If the file isn't to big you could probably just get away with reading the file into the memory from the offset to EOF, writing the bytes you want to insert then writing the end of the file after that. Code: var iostream = File.Open("file.bin", FileMode.Open);
byte[] buffer = new byte[iostream.Length - 0x20];
iostream.Seek(0x20, SeekOrigin.Begin);
iostream.Read(buffer, 0, buffer.Length);
iostream.Seek(0x20, SeekOrigin.Begin);
iostream.Write(new byte[16], 0, 16); //bytes to insert
iostream.Write(buffer, 0, buffer.Length);
iostream.Close(); It's still kinda improper, always best to look for an alternative way. | | | |  |
04-17-2011
|
#6 (permalink)
| | | Join Date: Sep 2010 Location: USA
Posts: 488
Thanks: 594 | | | Re: Add bytes to a file? Code: /// <summary>
/// Inserts bytes into the given stream.
/// </summary>
/// <param name="Source">The stream to insert bytes into.</param>
/// <param name="Offset">Position to insert bytes into.</param>
/// <param name="Data">Bytes to insert.</param>
public static byte[] InsertBytes(Stream Source, int Offset, byte[] Data)
{
using (var io = new EndianIO(Source, EndianType.LittleEndian))
{
io.Open();
var firstArray = io.Reader.ReadBytes(Offset);
var secondArray = io.Reader.ReadBytes((int)io.Stream.Length - (int)io.Position);
var newArray = new byte[Source.Length + Data.Length];
Buffer.BlockCopy(firstArray, 0, newArray, 0, firstArray.Length);
Buffer.BlockCopy(Data, 0, newArray, firstArray.Length, Data.Length);
Buffer.BlockCopy(secondArray, 0, newArray, firstArray.Length + Data.Length, secondArray.Length);
return newArray;
}
}
/// <summary>
/// Inserts bytes into the given byte array.
/// </summary>
/// <param name="Source">The byte array to insert bytes into.</param>
/// <param name="Offset">Position to insert bytes into.</param>
/// <param name="Data">Bytes to insert.</param>
public static byte[] InsertBytes(byte[] Source, int Offset, byte[] Data)
{
using (var io = new EndianIO(Source, EndianType.LittleEndian))
{
io.Open();
var firstArray = io.Reader.ReadBytes(Offset);
var secondArray = io.Reader.ReadBytes((int)io.Stream.Length - (int)io.Position);
var newArray = new byte[Source.Length + Data.Length];
Buffer.BlockCopy(firstArray, 0, newArray, 0, firstArray.Length);
Buffer.BlockCopy(Data, 0, newArray, firstArray.Length, Data.Length);
Buffer.BlockCopy(secondArray, 0, newArray, firstArray.Length + Data.Length, secondArray.Length);
return newArray;
}
}
/// <summary>
/// Deletes bytes from the given stream.
/// </summary>
/// <param name="Source">The stream to delete bytes from.</param>
/// <param name="Offset">Position to delete bytes from.</param>
/// <param name="Size">How many bytes to delete.</param>
public static byte[] DeleteBytes(Stream Source, int Offset, int Size)
{
using (var io = new EndianIO(Source, EndianType.LittleEndian))
{
io.Open();
var firstArray = io.Reader.ReadBytes(Offset);
io.SetPosition(Size, SeekOrigin.Current);
var secondArray = io.Reader.ReadBytes((int)io.Stream.Length - (int)io.Position);
var newArray = new byte[Source.Length - Size];
Buffer.BlockCopy(firstArray, 0, newArray, 0, firstArray.Length);
Buffer.BlockCopy(secondArray, 0, newArray, firstArray.Length, secondArray.Length);
return newArray;
}
}
/// <summary>
/// Deletes bytes from the given byte array.
/// </summary>
/// <param name="Source">The byte array to delete bytes from.</param>
/// <param name="Offset">Position to delete bytes from.</param>
/// <param name="Size">How many bytes to delete.</param>
public static byte[] DeleteBytes(byte[] Source, int Offset, int Size)
{
using (var io = new EndianIO(Source, EndianType.LittleEndian))
{
io.Open();
var firstArray = io.Reader.ReadBytes(Offset);
io.SetPosition(Size, SeekOrigin.Current);
var secondArray = io.Reader.ReadBytes((int)io.Stream.Length - (int)io.Position);
var newArray = new byte[Source.Length - Size];
Buffer.BlockCopy(firstArray, 0, newArray, 0, firstArray.Length);
Buffer.BlockCopy(secondArray, 0, newArray, firstArray.Length, secondArray.Length);
return newArray;
}
} | | | | | | Thread Tools | | | | Display Modes | Linear Mode |
Posting Rules
| You may not post new threads You may not post replies You may not post attachments You may not edit your posts HTML code is Off | | | All times are GMT -5. The time now is 06:27 PM. | | | |