What's new

Reverse Bytes

  • Thread starter Xeren
  • Start date
  • Views 645
Xeren

Xeren

♦♦♦ God Complex ♦♦♦
Legendary Veteran Programmer Modder
Messages
5,668
Reaction score
2,107
Points
795
Sin$
0
I want to read an Int32 from bytes(00E7), but evertime I do it comes as 5000(something like that). I need to reverse it so it will read 231. I've tried Array.Reverse and other methods, but no result.
 
I

iCodeJunk

Newbie
Messages
18
Reaction score
4
Points
45
Sin$
0
Well you're actually reading an int16 if you're reading from only 2 bytes. I'm not sure why you are having problems though, this works fine for me:
coJd.png
 
Xeren

Xeren

♦♦♦ God Complex ♦♦♦
Legendary Veteran Programmer Modder
Messages
5,668
Reaction score
2,107
Points
795
Sin$
0
For the Int16 part, I was reading Big Endian, which makes it from 00000000 to 0000. Also, I'll try it out.
 
C

CraigMoore

Newbie
Messages
10
Reaction score
0
Points
35
Sin$
7
Have you tried reading the bytes in a different order, such as changing the Endian from Big to Small?
 
BuC-ShoTz

BuC-ShoTz

Enthusiast
Messages
181
Reaction score
72
Points
85
Sin$
0
Code:
Int16 myint = IPAddress.HostToNetworkOrder(binaryReader.ReadInt16());
or use this class

Code:
using System;
using System.IO;
namespace PS3_Packager
{
class BigEndianReader : BinaryReader
{
private byte[] a16 = new byte[2];
private byte[] a32 = new byte[4];
private byte[] a64 = new byte[8];
public BigEndianReader(Stream stream) : base(stream) { }
public override int ReadInt32()
{
a32 = base.ReadBytes(4);
Array.Reverse(a32);
return BitConverter.ToInt32(a32, 0);
}
public Int16 ReadInt16()
{
a16 = base.ReadBytes(2);
Array.Reverse(a16);
return BitConverter.ToInt16(a16, 0);
}
public Int64 ReadInt64()
{
a64 = base.ReadBytes(8);
Array.Reverse(a64);
return BitConverter.ToInt64(a64, 0);
}
public UInt32 ReadUInt32()
{
a32 = base.ReadBytes(4);
Array.Reverse(a32);
return BitConverter.ToUInt32(a32, 0);
}
}
}
 
BuC-ShoTz

BuC-ShoTz

Enthusiast
Messages
181
Reaction score
72
Points
85
Sin$
0
the class i posted above just use it like a BinaryReader
Code:
BigEndianReader reader = new BigEndianReader(new FileStream(pkgFile, FileMode.Open));
 
Top Bottom
Login
Register