Encoding.ASCII.GetString question

C

cok

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 ['A', 'B', 'C', 0,0,0,0,...])

but the retult is a long string contain string("ABC") and many '0',

How can I only get string "ABC" of I needed?

or May I shouldn't use Encoding.ASCII.GetString ?

thanks for any advice.


--
_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
_/
_/ Nie Longhai , coder -_-|||
_/ Shanghai Xomi Instruments Co., Ltd.
_/ URL: http://www.xomi.cn
_/ Shanghai C&U Industrial Park
_/ Feng Cheng Town, Feng Xian District
_/ Shanghai, 201411
_/ Phone:86-21-57513966-807
_/ Fax:86-21-57513636
_/ Mobile:13162055440
_/ Email:[email protected] ,[email protected]
_/
_/ Profession & Focus
_/ High precision semiconductor metrology system vendor.
_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
 
R

rich.quackenbush

Are you getting this string from an imported (windows dll) function or
something? The GetString function isn't designed to look at null
terminated strings.

One way to sort this would be to search through the byte array for the
first value which = null. Then, just convert the previous part of the
array to a string.

-Rich
 
J

james.curran

One way to sort this would be to search through the byte array for the
first value which = null. Then, just convert the previous part of the
array to a string.

Search through the byte array for the first value which = (byte) 0,
which is different from null.
 
C

cok

Hi, james
thanks for your help :)

Encoding.ASCII.GetString(byteArray, 0, Array.IndexOf(byteArray, (byte)
0));

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 ['A', 'B', 'C', 0,0,0,0,...])

but the retult is a long string contain string("ABC") and many '0',
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top