In the following piece of code:
************************************************** *****
byte[] buffer = new byte[20];
string next_node=null, ret=null;
ASCIIEncoding encoding = new ASCIIEncoding();
BufferedStream sr = new BufferedStream (new FileStream (nodeptr, FileMode.Open ));
sr.Seek(6, SeekOrigin.Begin );
sr.Read (buffer, 0, 17);
next_node = encoding.GetString(buffer);
Console.WriteLine(next_node.Trim() + "__");
************************************************** *****
I read 17 bytes from a text file as from the 7th byte onwards.
Why is it that the final WriteLine statement shows what appears to be
characters with ASCII code 0 in next_node before the "__" ?
A work-around is to set the size of buffer to exactly the number of bytes
I happen to know I need to read. What when I don't and overallocate space
in buffer, or when Read() says fewer bytes than requested were read and I
only want to convert those to a string?
Rico. 2 1565
Rico <ra*****@yahoo.com> wrote: In the following piece of code: ************************************************** ***** byte[] buffer = new byte[20]; string next_node=null, ret=null; ASCIIEncoding encoding = new ASCIIEncoding();
This line is unnecessary - you can just use Encoding.ASCII to avoid
creating a new encoding each time.
BufferedStream sr = new BufferedStream (new FileStream (nodeptr, FileMode.Open ));
sr.Seek(6, SeekOrigin.Begin ); sr.Read (buffer, 0, 17); next_node = encoding.GetString(buffer);
Console.WriteLine(next_node.Trim() + "__"); ************************************************** *****
I read 17 bytes from a text file as from the 7th byte onwards. Why is it that the final WriteLine statement shows what appears to be characters with ASCII code 0 in next_node before the "__" ?
Because you've presented it with a buffer of 20 bytes and asked it to
convert the whole of it to a string - it's done exactly what you asked
it to.
A work-around is to set the size of buffer to exactly the number of bytes I happen to know I need to read. What when I don't and overallocate space in buffer, or when Read() says fewer bytes than requested were read and I only want to convert those to a string?
You should use the return value of Read to know how much you've
actually read, and then use
Encoding.ASCII.GetString(buffer, 0, bytesRead);
--
Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
On Wed, 04 Aug 2004 08:52:16 +0100, Jon Skeet[ C# MVP] wrote: You should use the return value of Read to know how much you've actually read, and then use Encoding.ASCII.GetString(buffer, 0, bytesRead);
I know this group is quite busy but it still seems there's space and
bandwidth for a 'Thank You Jon'.
Rico. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: FrzzMan |
last post by:
How to convert back a string that have converted to byte using
System.Text.Encoding.UTF8.GetBytes()
string StringData = "This is a string";
byte ConvertedString =...
|
by: Ricardo Quintanilla |
last post by:
i have a code that sends data to a socket listening over as400 platform,
the socket responds to me as a "byte array".
then i need to convert the "byte array" into a string.
the problem is that...
|
by: Benoit |
last post by:
Hi, what is the most performing way to convert a byte stream to a string?
Byte() to be converted to String.
Thanks, iBen.
Sorry if it ia a double post in this newsgroup, I cannot set my...
|
by: David Bargna |
last post by:
Hi
I have a problem, I have a string which needs to be converted to a
byte array, then have the string representation of this array stored
in an AD attribute. This string attribute then has to...
|
by: rsine |
last post by:
I have developed a program that sends a command through the serial port
to our business system and then reads from the buffer looking for a
number. Everything worked great on my WinXP system, but...
|
by: Ger |
last post by:
I have not been able to find a simple, straight forward Unicode to ASCII
string conversion function in VB.Net.
Is that because such a function does not exists or do I overlook it?
I found...
|
by: moondaddy |
last post by:
I need to convert a byte array to a string and pass it as a parameter in a
URL and then convert it back to the original byte array. However, its
getting scrambled in the conversion. In short,...
|
by: Andrea |
last post by:
Hi,
suppose that I have a string that is an hexadecimal number, in order
to print this string I have to do:
void print_hex(unsigned char *bs, unsigned int n){
int i;
for (i=0;i<n;i++){...
|
by: =?Utf-8?B?ZGg=?= |
last post by:
If there's bunch of hex numbers: 0xA6, 0xD9, 0x00, 0xAA, 0x00, and 0x62, how
to construct a string ("string" in C#) with those hex numbers?
Thanks!
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
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,...
|
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...
|
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,...
|
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...
| |