473,378 Members | 1,577 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Encoding.ASCII.GetString()

C#:
I have a buffer that is populated with char data from a DLL:
byte [] ret = new byte[1024];
I want to convert to a C# string:
string msg = Encoding.ASCII.GetString(ret, 0, ret.Length);

The problem is I never know in advance what size the string will be. When I
call Encoding.ASCII.GetString I need to specify the size. If I set it to
ret.Length as above then in the debugger I can see msg is the full 1024
bytes (0 padded at the end regardless of the actual string's size).
How can I look at a byte[] and determine the size of the string in it (I
know I could just loop and look for the first occurance of 0 -- is tha tthe
best way?)

Thank you!

Mar 1 '06 #1
3 6013
Hello, devgrt!

d> The problem is I never know in advance what size the string will be.
d> When I call Encoding.ASCII.GetString I need to specify the size. If I
d> set it to ret.Length as above then in the debugger I can see msg is the
d> full 1024 bytes (0 padded at the end regardless of the actual string's
d> size). How can I look at a byte[] and determine the size of the string
d> in it (I know I could just loop and look for the first occurance of 0 --
d> is tha tthe best way?)

You can use Encoding.ASCII.GetCharCount();
However, why do you need to know the size of the string int the byte[]?

--
Regards, Vadym Stetsyak
www: http://vadmyst.blogspot.com
Mar 1 '06 #2
Thank you for reply!
The reason I need to know the length is that I need to specify it in:
string msg = Encoding.ASCII.GetString();
if I say string msg = Encoding.ASCII.GetString(ret, 0, ret.Length); then if
I look at my string msg in debug it is ret.Length and is zero-padded to the
end. If I do msg.Length I always get the ret.Length as opposed to the actual
size of teh string in msg. msg is still useable since it is zero-padded so
MessageBox.Show(msg) works OK but I dont want to waste the space by having
msg larger than it needs to be.

"Vadym Stetsyak" <va*****@ukr.net> wrote in message
news:OB**************@TK2MSFTNGP09.phx.gbl...
Hello, devgrt!

d> The problem is I never know in advance what size the string will be.
d> When I call Encoding.ASCII.GetString I need to specify the size. If I
d> set it to ret.Length as above then in the debugger I can see msg is the
d> full 1024 bytes (0 padded at the end regardless of the actual string's
d> size). How can I look at a byte[] and determine the size of the string
d> in it (I know I could just loop and look for the first occurance of
0 --
d> is tha tthe best way?)

You can use Encoding.ASCII.GetCharCount();
However, why do you need to know the size of the string int the byte[]?

--
Regards, Vadym Stetsyak
www: http://vadmyst.blogspot.com

Mar 1 '06 #3
You didn't get it right...

in Encoding.ASCII.GetString(....) you specify the length of the buffer and not the length of the string...

then if
I look at my string msg in debug it is ret.Length and is zero-padded to the
end. If I do msg.Length I always get the ret.Length


That is true for ASCII encoding ( every char is equal to 1 byte ), but if you will consider Unicode string ( 1 char is equal to 2 bytes ) then this will break your asumptions...

Internally Encoding.GetString() checks how much symbols of specified encoding are there in the buffer, and creates string that has the same number of symbols ( no waste here ).

You can use Encoding.....GetString() and fear nothing :8-)

--
Regards, Vadym Stetsyak
www: http://vadmyst.blogspot.com
Mar 1 '06 #4

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

Similar topics

4
by: Viorel | last post by:
For me is a little bit mysterious how work encoding and decoding functions, what is underneath of their calling? Encoding1.GetBytes(string1); in particularly ASCII.GetBytes(string1) ...
10
by: Christopher H. Laco | last post by:
Long story longer. I need to get web user input into a backend system that a) only grocks single byte encoding, b) expectes the data transer to be 1 bytes = 1 character, and c) uses the HP Roman-6...
4
by: Martin Fletcher | last post by:
when I execute dim strTEMP_DATA as string strTEMP_DATA = Encoding.ASCII.GetString(rBuffer).ToString I alway get a string without the closing quote. I see this in the 'Autos' window, I also...
6
by: Danny | last post by:
I am working on a project in which a number of client applications will be posting xml documents as a byte array to an ASP.NET page on our web server. I am trying to simulate the process and run...
4
by: cok | last post by:
Hi, All I have a question about Encoding.ASCII.GetString. I want convert a byte array to a string, I use Encoding.ASCII.GetString(byteArray) (byteArray is something like ) but the retult...
4
by: A_StClaire_ | last post by:
hi, I am using the following code to download multiple file types from a server. .txt files transfer fine. however Word .doc files come through garbled and I don't know enough about encoding...
0
by: Janusz Nykiel | last post by:
I've stumbled upon unexpected behavior of the .NET 2.0 System.Xml.XmlWriter class when using it to write data to a binary stream (System.IO.Stream). If the amount of data is less than a certain...
9
by: =?Utf-8?B?RGFu?= | last post by:
I have the following code section that I thought would strip out all the non-ascii characters from a string after decoding it. Unfortunately the non-ascii characters are still in the string....
3
by: mazdotnet | last post by:
Hi guys, I'm integrating our systems into another system which returns a response code after doing a POST Byte responseData = objWebClient.UploadValues(postTo, objFormPost); string retVal =...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...

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.