Can BinaryReader be forced to read a stream, say a TCP/IP stream or memory
stream or even file stream in big endian order or do I have to write
something custom to reverse the byte order? So, for example, I'd like a
Windows client PC (native little endian) to be able to read a network stream
coming in as big endian. I want to be able to read a short for example
without worrying about flipping the bytes.
Thank you,
Laszlo 14 19060
Hi Laszlo,
The BinaryReader uses UTF-8 encoding by default. You can specify your own encoding by using the overloaded constructor.
BinaryReader br = new BinaryReader(bi naryStream, System.Text.Enc oding.BigEndian Unicode);
Happy coding!
Morten Wennevik [C# MVP]
Thanks again. When I set the Encoding, does that work for non-text data
types. So, when I do a ReadUInt16, does it read them BigEndian? I'll give
it a try. Thank you.
"Morten Wennevik" <Mo************ @hotmail.com> wrote in message
news:opr8picjpg klbvpo@morten_x .edunord... Hi Laszlo,
The BinaryReader uses UTF-8 encoding by default. You can specify your own
encoding by using the overloaded constructor. BinaryReader br = new BinaryReader(bi naryStream,
System.Text.Enc oding.BigEndian Unicode); Happy coding! Morten Wennevik [C# MVP]
Morten Wennevik <Mo************ @hotmail.com> wrote: The BinaryReader uses UTF-8 encoding by default. You can specify your own encoding by using the overloaded constructor.
BinaryReader br = new BinaryReader(bi naryStream, System.Text.Enc oding.BigEndian Unicode);
I believe the OP is talking about the various other calls to
BinaryReader: ReadInt16 etc, not reading strings.
--
Jon Skeet - <sk***@pobox.co m> http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Laszlo Szijarto <ls*******@nosp am.mrdoc.cc> wrote: Can BinaryReader be forced to read a stream, say a TCP/IP stream or memory stream or even file stream in big endian order or do I have to write something custom to reverse the byte order? So, for example, I'd like a Windows client PC (native little endian) to be able to read a network stream coming in as big endian. I want to be able to read a short for example without worrying about flipping the bytes.
I don't believe there is any way of doing it with the current
BinaryReader. I've considered a few times writing a BitConverter and
BinaryReader where you can specify the endianness, but never got round
to it. Let me know if you'd particularly like it and I'll see what I
can do - no promises of delivery date though!
--
Jon Skeet - <sk***@pobox.co m> http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Thank you, John,
I really appreciate it. I've actually started writing my own
BinaryReaderBE. I can read basic data types. I've also extended it so that
it can read arrays of more than just bytes (so, short[], ushort[], int[],
uint[]). And, finally, I've added a function which would allow you to read
a struct (or class) out of a binary stream. You pass it a System.Type
object and it uses reflection to populate the various members, returning an
object. You have to cast the returned object to whatever type you want, but
it works really well. I've also implemented this for the Little Endian
Binary Reader in that same class. Pretty simple to do, and, given that
Network Byte Order teds to be Big Endian and that we here at NASA use Big
Endian for most things (we use a lot of Motorola CPUs), I'm surprised that
Microsoft doesn't have a Big Endian binary reader.
Inside the BinaryReaderBE, I have a private method which takes a byte[] and
flips it. Inside the ReadInt16, method, then, it returns a short
reconstructed from the reversed byte[] throught the BitConverter.
Thanks very much for the offer of help,
Laszlo
"Jon Skeet [C# MVP]" <sk***@pobox.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com... Laszlo Szijarto <ls*******@nosp am.mrdoc.cc> wrote: Can BinaryReader be forced to read a stream, say a TCP/IP stream or
memory stream or even file stream in big endian order or do I have to write something custom to reverse the byte order? So, for example, I'd like a Windows client PC (native little endian) to be able to read a network
stream coming in as big endian. I want to be able to read a short for example without worrying about flipping the bytes.
I don't believe there is any way of doing it with the current BinaryReader. I've considered a few times writing a BitConverter and BinaryReader where you can specify the endianness, but never got round to it. Let me know if you'd particularly like it and I'll see what I can do - no promises of delivery date though!
-- Jon Skeet - <sk***@pobox.co m> http://www.pobox.com/~skeet If replying to the group, please do not mail me too
Laszlo Szijarto <ls*******@nosp am.mrdoc.cc> wrote: I really appreciate it. I've actually started writing my own BinaryReaderBE. I can read basic data types. I've also extended it so that it can read arrays of more than just bytes (so, short[], ushort[], int[], uint[]). And, finally, I've added a function which would allow you to read a struct (or class) out of a binary stream. You pass it a System.Type object and it uses reflection to populate the various members, returning an object.
How does it know what order to populate the members in? The order it
comes back from the Type.GetPropert ies call (or whatever) isn't
specified, so you should be a bit wary.
You have to cast the returned object to whatever type you want, but it works really well. I've also implemented this for the Little Endian Binary Reader in that same class. Pretty simple to do, and, given that Network Byte Order teds to be Big Endian and that we here at NASA use Big Endian for most things (we use a lot of Motorola CPUs), I'm surprised that Microsoft doesn't have a Big Endian binary reader.
Yes, it is a bit short-sighted.
Inside the BinaryReaderBE, I have a private method which takes a byte[] and flips it. Inside the ReadInt16, method, then, it returns a short reconstructed from the reversed byte[] throught the BitConverter.
Right. I thought of using that, but reckoned it would probably be
easier (and faster) to do the conversions myself within my own
BinaryReader. I'll let you know if I find time to work on my
EndianBitConver ter and EndianBinaryRea der/Writer classes.
--
Jon Skeet - <sk***@pobox.co m> http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
"Jon Skeet [C# MVP]" <sk***@pobox.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com... Laszlo Szijarto <ls*******@nosp am.mrdoc.cc> wrote: I really appreciate it. I've actually started writing my own BinaryReaderBE. I can read basic data types. I've also extended it so
that it can read arrays of more than just bytes (so, short[], ushort[],
int[], uint[]). And, finally, I've added a function which would allow you to
read a struct (or class) out of a binary stream. You pass it a System.Type object and it uses reflection to populate the various members, returning
an object.
How does it know what order to populate the members in? The order it comes back from the Type.GetPropert ies call (or whatever) isn't specified, so you should be a bit wary.
I use reflection as in the code snippet below. I've used this approach
before. It seems to work well.
public object ReadStruct(Type type)
{
object objectStruct = Activator.Creat eInstance(type) ;
foreach (FieldInfo fieldInfo in type.GetFields( ))
{
switch(fieldInf o.FieldType.ToS tring())
{
case "System.Byt e":
fieldInfo.SetVa lue(objectStruc t, ReadByte());
break;
... [rest of supported data types]
}
}
return objectStruct;
}
You would call it as follows.
BinaryReaderBE binaryReaderBE = new BinaryReaderBE([stream object]);
[ClassName] class =
([ClassName])binaryReaderBE .ReadStruct(typ eof([ClassName]);
I've never had any trouble with stuff being out of order except if I've
changed my struct mid stream. Then, for some reason, I have to delete all
the stuff in the "bin" directories and THEN recompile. Otherwise, the order
always seems to come back as listed in the Class (or struct) Definition.
Laszlo Szijarto <ls*******@nosp am.mrdoc.cc> wrote: I've never had any trouble with stuff being out of order except if I've changed my struct mid stream. Then, for some reason, I have to delete all the stuff in the "bin" directories and THEN recompile. Otherwise, the order always seems to come back as listed in the Class (or struct) Definition.
Yes, it does *now* - but it's not guaranteed that it will in the
future. You're relying on the implementation of GetFields not changing,
and I suspect it's very likely that it will in the future.
--
Jon Skeet - <sk***@pobox.co m> http://www.pobox.com/~skeet
If replying to the group, please do not mail me too This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Rafal 'Raf256' Maj |
last post by:
Hi,
I need to parse a file. This means reading from it as from std::istream.
But - sometimes I also need to put-back some text I read before.
What type of string can I use for that? Something like:
void cLine::Load(std::istream &data) {
data.ignore(4); // ignore word "line"
char c; // ignore { and } chars
data >> c >> x1 >> y1 >> x2 >> y2 >> c;
|
by: ywchan |
last post by:
I have used a stream reader and sometimes it contains more than one rows of
text. How to read all the lines in a stream reader?
sr.Readline can only get line by line...and every times the total lines
varies...I wonder how to get all the lines at the same time. Thanks!
|
by: om |
last post by:
hi I an new to c++ and was wondering if anyone could advise me how to
read a stream coming from a http source thanks alot...
|
by: James Minns |
last post by:
Hi, I have the following problem with my VB code: accented characters are
being transformed into a cr-lf pair!
I am reading a sequence of bytes from a binary file, one part of which is a
text string.
Here is a code snippet:
Dim fs As New FileStream("c:\test.bin", FileMode.Open)
Dim br As New BinaryReader(fs)
Dim str As String
|
by: csharpula csharp |
last post by:
Hello,I am trying to read from a zip stream and one of the files is of a
0 size (0 bytes)- Exception is thrown as a result but there are some
other files after it in a stream which I want to keep reading and
writing to temp dir. How can I deal such exception.
I get error when in the end I want to erase the temp dir and it won't
let me because the 0 bytes file is being used by other process.
How to solve it?
Thank you!
| |
by: =?Utf-8?B?RGFu?= |
last post by:
I need to read an HTTP stream that is defined as excel format. How can I
access this via ADO.Net? This dataset pastes nicely into excel.
http://ts-lc.sc8.finance.lycos.com/1184344340757?User=?????&Pswd=?????&DataType=Excel97&Symbol=INDEX:NDX.X&Interval=10&Permission=1000&Ht=400&Wd=600&Display=0&Study=&Param1=&Param2=&Param3=&FontSize=10&LocaleID=0x0409
Thanks in advance
|
by: Jack |
last post by:
Hi,
I want to read a string a chars from a stream, and put it into a
string.
At the moment, I'm creating a buffer of a fixed size, and reading the
stream of text into it. It works, but I have to create a buffer of a
pre-defined length: (ConstBufferByteSize=10000000). How can I read a
stream into a buffer or string without knowing the number of chars the
stream will contain?
|
by: csharpula csharp |
last post by:
Hello,
I would like to know how can I read a file by this way:
Read from line X to Y and in other iteration from line Y to P and on and
on. How to implement it? Which method and how? Thank u!
*** Sent via Developersdex http://www.developersdex.com ***
|
by: hdbbdh |
last post by:
Hello everyone,
I try to read a stream and save it as byte array to add it later to zip file.
this is the code that I use
Dim totalbytes As Byte() = Nothing
Dim iBytesRead As Integer
Dim sChunks As FileStream = File.OpenRead("c:\myfile.wav")
Dim Totalread As Int64 = 0
ReDim totalbytes(sChunks.Length)
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
| |
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |