473,471 Members | 2,175 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Reading 8 bit chars off a stream

I am getting a packet of data from an instrument. The strings in the
data packet are null terminted. What is the best way to extract a
string from the data packet?

I am working in .Net 20.

Presently the code reads byte by byte through the packet adding the
value into a List<byte>. The a new byte[] is created based on the
count of the List<byte>. The code iterates though the List<byte> to
populate the byte[]. Finally the byte[] is passed to an instance of
Encoding to get the string.

Is there a better way which does not require looping thourgh the string
twice?

Feb 9 '06 #1
3 2121
Create a buffer to hold the bytes, that is, an array of bytes. Then you
don't need to use the List<byte> and you can flush the buffer whenever you
need to.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
We got a sick zebra a hat,
you ultimate tuna.
<ca******@gmail.com> wrote in message
news:11*********************@g14g2000cwa.googlegro ups.com...
I am getting a packet of data from an instrument. The strings in the
data packet are null terminted. What is the best way to extract a
string from the data packet?

I am working in .Net 20.

Presently the code reads byte by byte through the packet adding the
value into a List<byte>. The a new byte[] is created based on the
count of the List<byte>. The code iterates though the List<byte> to
populate the byte[]. Finally the byte[] is passed to an instance of
Encoding to get the string.

Is there a better way which does not require looping thourgh the string
twice?

Feb 9 '06 #2
I assume you are using the SerialPort class in the System.IO.Ports
namespace. If not, you might want to look at that.

You might want to consider a MemoryStream instead of a List<byte> to
store the bytes. A MemoryStream will store the bytes as well and allocate
new memory when you need it, but if the message is large, then you can call
the GetBuffer method on the stream to get the internal buffer, whereas with
List<byte> you have to create a new array, and that might be expensive.

Once you have the byte array, you can pass it to the GetString method on
the encoding in one shot, passing the array, as well as the start index and
number of characters to process. This will help in the case of using the
MemoryStream, since you can get the buffer directly and then tell the
encoder just how much to process.

It would be nice if you could use the StreamReader here to perform the
encoding for you, but unfortunately, it buffers the characters from the
stream internally, and if the device isn't going to offer up anything past
the message that it sent, the StreamReader will hang.

Hope this helps.

<ca******@gmail.com> wrote in message
news:11*********************@g14g2000cwa.googlegro ups.com...
I am getting a packet of data from an instrument. The strings in the
data packet are null terminted. What is the best way to extract a
string from the data packet?

I am working in .Net 20.

Presently the code reads byte by byte through the packet adding the
value into a List<byte>. The a new byte[] is created based on the
count of the List<byte>. The code iterates though the List<byte> to
populate the byte[]. Finally the byte[] is passed to an instance of
Encoding to get the string.

Is there a better way which does not require looping thourgh the string
twice?

Feb 9 '06 #3
System.Io.Ports, interesting and VERY useful. Currently the connection
is via the Socket Class, but serial connections will need to be
supported, also. Now I know where to go looking.

Your reply has provoked more questions regarding how the code is
working earlier on. Presently the code is using the
Socket.Receive(byte[]) method and passing that into a custom class
called CommsStreamer. CommsStreamer stores the byte[] in an internal
MemoryStream. Each byte is extracted from the memory stream via
ReadByte();

The main reason why each byte is being ripping off one byte at a time
is because the intrument is Big Endian. When extracting a WORD or
DWORD, the conversion is done in the CommsStreamer class back to our
world of Little Endian.

Is there a better way of doing things, when dealing with Big Endian
data?

Feb 9 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Webster | last post by:
Hello, I have to read a file with a single "command" per line. But how do I read to the end of the line?? I am using C style filepointers (not by choice :( ) Thanks for any help! --...
1
by: Joan | last post by:
I read RAW image data from a binary file stream. I have to read just bytes one by one and put them into a matrix of structs of unsigned chars. I have to use unsigned chars because their rangs go...
19
by: Lionel B | last post by:
Greetings, I need to read (unformatted text) from stdin up to EOF into a char buffer; of course I cannot allocate my buffer until I know how much text is available, and I do not know how much...
3
by: Alex Clark | last post by:
Hi All, I'm having some problems reading a network stream. I'm writing a lightweight POP client to handle a very specific task, but I keep unexpectedly reaching the end of the datastream when...
16
by: lovecreatesbeauty | last post by:
/* When should we worry about the unwanted chars in input stream? Can we predicate this kind of behavior and prevent it before debugging and testing? What's the guideline for dealing with it? ...
7
by: pedagani | last post by:
Dear comp.lang.c++, I'm trying to read a file with very long filename using ifstream. Although, the file exists the file open for read fails. Is there a restriction on the size? I'm using winXP...
8
by: Jay | last post by:
I'm trying to store a sequence of operations and values of different types into a single array. It's a sequence of command word bytes, and a sequence of one or more values (as determined by the...
6
by: efrenba | last post by:
Hi, I came from delphi world and now I'm doing my first steps in C++. I'm using C++builder because its ide is like delphi although I'm trying to avoid the vcl. I need to insert new features...
1
by: quddusaliquddus | last post by:
Hi :D, I am sending data to server via TCP IP Connection. I am using a continuous loop at the server end - that accepts new clients and while streams can be read, it reads data stream. ...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
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...
0
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 ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.