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.Begi n );
sr.Read (buffer, 0, 17);
next_node = encoding.GetStr ing(buffer);
Console.WriteLi ne(next_node.Tr im() + "__");
*************** *************** *************** **********
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 1580
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.Begi n ); sr.Read (buffer, 0, 17); next_node = encoding.GetStr ing(buffer);
Console.WriteLi ne(next_node.Tr im() + "__"); *************** *************** *************** **********
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(buffe r, 0, bytesRead);
--
Jon Skeet - <sk***@pobox.co m> 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(buffe r, 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 = System.Text.Encoding.UTF8.GetBytes(StringData);
Now, how can I convert ConvertedString to "This is a string" string again?
|
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 the data converted from the byte array into a string
, is not what i expect.
example:
- the data returned from the socket is (byte array)
"Usuario Sin Atribuciones Para Esta Función"
|
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 first one.
|
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 be read and the
string representation of the byte array has to be converted back to
the original byte array and converted back to the original string -
confused yet?
in psuedo
|
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 when I tried
the program on the Win98 system it will be running on, I get the
following error:
Cast from string "2076719" to type 'Long' is not valid
I am not sure why I only get this error on the Win98 system or how to go
about correcting...
| |
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 Encoding.Convert, but that needs byte arrays.
Thanks,
/Ger
|
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, here's the code:
======================================
Dim textConverter As New ASCIIEncoding
Dim sParam As String = "This is my cool param"
Dim bytParam() As Byte
'load the byte array here...
|
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++){
printf("%02x",bs);
}
}
|
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: 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: 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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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...
| |