I read bytes and display them in a text box with this:
BinaryReader br = new BinaryReader(File.OpenRead(path));
br.BaseStream.Position = 0x1D;
textBox1.Text = br.ReadInt32().ToString("X");
br.Dispose();
I need to read 4 bytes then XOR each one with the value 149 and print the results out to the text box. The text box only seems to display "System.Byte()"
I've tried this code, but there were many errors, such as not being able to convert a byte to an int:
BinaryReader br = new BinaryReader(File.OpenRead(path));
br.BaseStream.Position = 0x3E8;
byte[] buffer = br.ReadBytes(4);
int i = 149;
The output should look like this:
result = buffer[0] ^ 149;
result2 = buffer[1] ^ 149;
result3 = buffer[2] ^ 149;
result4 = buffer[3] ^ 149;
How can I achieve this?
Welcome to Stack Overflow, the land where dreams come true (if your dreams consist of answering programming questions.). Since this is your first question, I'll do my best to answer but in the future, try to follow community guidelines to basically show that
As a side note, check out dotnetfiddle.net when you want to make small programs to test out quickly. Pro tip: don't use excel to debug c# programs ;)
OK, I'm going to infer that your question is the following. Pretty liberal paraphrasing I know, but I'm trying to help you out here
[How can I read 4 bytes from a file at a given file offset, XOR each of those 4 bytes with 0x149, then display those on the screen?]
OK, first place to start is to get a FileStream
object I think. More reading: what is using
?
using (var input = File.OpenRead(path))
{
// somehow seek to file offset
// read 4 bytes, and
// XOR each byte with 0x149
// store results in a List<byte> (or something)
}
// display result
Alright, to seek a file, you need input.Seek(0x1D, SeekOrigin.Begin);
(Assuming 0x1D from OP is correct).
To read 4 bytes, do this (see ReadByte()
documentation)
for (var i=0;i<4;i++){
var byteThatIsNotRemembered = input.ReadByte();
}
Now you need to XOR those bytes with
for (var i=0;i<4;i++){
var byteThatIsNotRemembered = input.ReadByte() ^ 0x149;
}
Finally, save them to a list
// need to instantiate list somewhere near top
var byteList = new List<byte>();
// ... other code that we've written
for (var i=0;i<4;i++){
var byteThatIsRememberedNow = input.ReadByte() ^ 0x149;
// need to cast to byte because ^ operator creates ints
byteList.Add((byte) byteThatIsRememberedNow);
}
// you'll need to replace this with something for your text box...
// couldn't figure out from your question
for (var i=0;i<byteList.Length;i++){
Console.WriteLine(byteList[i]);
}
All together now...
// need to instantiate list somewhere near top
var byteList = new List<byte>();
using (var input = File.OpenRead(path))
{
input.Seek(0x1D, SeekOrigin.Begin);
for (var i=0;i<4;i++){
var byteThatIsRememberedNow = input.ReadByte() ^ 0x149;
byteList.Add((byte) byteThatIsRememberedNow);
}
}
// you'll need to replace this with something for your text box...
// couldn't figure out from your question
for (var i=0;i<byteList.Length;i++){
Console.WriteLine(byteList[i]);
}
Here's a similar dotnetfiddle where I use a string instead of a file to generate a stream.
Let me know if this was helpful