PC Review


Reply
Thread Tools Rate Thread

C Style strings to .NET strings.

 
 
=?Utf-8?B?RGF2aWQ=?=
Guest
Posts: n/a
 
      29th Jul 2005
I have a byte array that contains a whole bunch of information in it. It
gets generated from an embedded device and sent to me over ethernet.

Among the elements of information in that byte array is a C-style string.
In other words, there are 16 bytes. Each byte contains an ASCII character,
or 0. The string is null terminated.

I want to load those characters into a .NET string object. Is there a
single call that can do that?

I thought that ASCIIEncoding.ASCII.GetString(array, startindex, 16) would do
it for me. However, if the byte array contains the ascii character 'b',
followed by 15 zeros, what I want is the string "b", a string with length 1.
What I get is a string "b/0/0/0/0/0/0/0/0/0/0/0/0/0/0/0", a string with
length 16, in which the first character is the unicode 'b', and the others
are the unicode null character.

After failing to find a function that would do it, I just wrote a function
that took such a string and stripped out the first null and anything after
it, and that worked. However, it seemed to me that this would be a common
enough operation that there is probably a function call that did it in one
call, which would result in better code. Is there such a function?
 
Reply With Quote
 
 
 
 
Lloyd Dupont
Guest
Posts: n/a
 
      29th Jul 2005
I'm not usre about the performance (although can't be that bad) but there is
such a method in the
System.Runtime.InteropServices.Marshal class
from the top of my hea dI would say

Marshal.ToAnsiString(ptr)

--
There are 10 kinds of people in this world. Those who understand binary and
those who don't.
"David" <(E-Mail Removed)> wrote in message
news:73A7AA9A-092F-4A35-9875-(E-Mail Removed)...
>I have a byte array that contains a whole bunch of information in it. It
> gets generated from an embedded device and sent to me over ethernet.
>
> Among the elements of information in that byte array is a C-style string.
> In other words, there are 16 bytes. Each byte contains an ASCII
> character,
> or 0. The string is null terminated.
>
> I want to load those characters into a .NET string object. Is there a
> single call that can do that?
>
> I thought that ASCIIEncoding.ASCII.GetString(array, startindex, 16) would
> do
> it for me. However, if the byte array contains the ascii character 'b',
> followed by 15 zeros, what I want is the string "b", a string with length
> 1.
> What I get is a string "b/0/0/0/0/0/0/0/0/0/0/0/0/0/0/0", a string with
> length 16, in which the first character is the unicode 'b', and the others
> are the unicode null character.
>
> After failing to find a function that would do it, I just wrote a function
> that took such a string and stripped out the first null and anything after
> it, and that worked. However, it seemed to me that this would be a common
> enough operation that there is probably a function call that did it in one
> call, which would result in better code. Is there such a function?



 
Reply With Quote
 
Chris
Guest
Posts: n/a
 
      29th Jul 2005
David wrote:
> I have a byte array that contains a whole bunch of information in it. It
> gets generated from an embedded device and sent to me over ethernet.
>
> Among the elements of information in that byte array is a C-style string.
> In other words, there are 16 bytes. Each byte contains an ASCII character,
> or 0. The string is null terminated.
>
> I want to load those characters into a .NET string object. Is there a
> single call that can do that?
>
> I thought that ASCIIEncoding.ASCII.GetString(array, startindex, 16) would do
> it for me. However, if the byte array contains the ascii character 'b',
> followed by 15 zeros, what I want is the string "b", a string with length 1.
> What I get is a string "b/0/0/0/0/0/0/0/0/0/0/0/0/0/0/0", a string with
> length 16, in which the first character is the unicode 'b', and the others
> are the unicode null character.
>
> After failing to find a function that would do it, I just wrote a function
> that took such a string and stripped out the first null and anything after
> it, and that worked. However, it seemed to me that this would be a common
> enough operation that there is probably a function call that did it in one
> call, which would result in better code. Is there such a function?


Well if you think about it, what any function call is going to have to
do is basically the same loop you are doing. As long as your function
is effencient as possible, it'll be just as fast as any function call,
and maybe faster since you know what you are doing and any function you
call will probably be more generic.


PS. Any interop call would be slower than anything native..

Chris
 
Reply With Quote
 
Atul
Guest
Posts: n/a
 
      30th Jul 2005
Use the Marshal.PtrToStringAnsi function.

- Atul
Sky Software http://www.ssware.com/
Drop-In Windows Explorer-Like Shell Browsing UI for your apps.


"David" <(E-Mail Removed)> wrote in message
news:73A7AA9A-092F-4A35-9875-(E-Mail Removed)...
>I have a byte array that contains a whole bunch of information in it. It
> gets generated from an embedded device and sent to me over ethernet.
>
> Among the elements of information in that byte array is a C-style string.
> In other words, there are 16 bytes. Each byte contains an ASCII
> character,
> or 0. The string is null terminated.
>
> I want to load those characters into a .NET string object. Is there a
> single call that can do that?
>
> I thought that ASCIIEncoding.ASCII.GetString(array, startindex, 16) would
> do
> it for me. However, if the byte array contains the ascii character 'b',
> followed by 15 zeros, what I want is the string "b", a string with length
> 1.
> What I get is a string "b/0/0/0/0/0/0/0/0/0/0/0/0/0/0/0", a string with
> length 16, in which the first character is the unicode 'b', and the others
> are the unicode null character.
>
> After failing to find a function that would do it, I just wrote a function
> that took such a string and stripped out the first null and anything after
> it, and that worked. However, it seemed to me that this would be a common
> enough operation that there is probably a function call that did it in one
> call, which would result in better code. Is there such a function?



 
Reply With Quote
 
=?Utf-8?B?RGF2aWQ=?=
Guest
Posts: n/a
 
      1st Aug 2005
Thanks, everyone. (And special thanks to Lloyd and his sig. I liked it.)

"David" wrote:

> I have a byte array that contains a whole bunch of information in it. It
> gets generated from an embedded device and sent to me over ethernet.
>
> Among the elements of information in that byte array is a C-style string.
> In other words, there are 16 bytes. Each byte contains an ASCII character,
> or 0. The string is null terminated.
>
> I want to load those characters into a .NET string object. Is there a
> single call that can do that?
>
> I thought that ASCIIEncoding.ASCII.GetString(array, startindex, 16) would do
> it for me. However, if the byte array contains the ascii character 'b',
> followed by 15 zeros, what I want is the string "b", a string with length 1.
> What I get is a string "b/0/0/0/0/0/0/0/0/0/0/0/0/0/0/0", a string with
> length 16, in which the first character is the unicode 'b', and the others
> are the unicode null character.
>
> After failing to find a function that would do it, I just wrote a function
> that took such a string and stripped out the first null and anything after
> it, and that worked. However, it seemed to me that this would be a common
> enough operation that there is probably a function call that did it in one
> call, which would result in better code. Is there such a function?

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Is it simply that c# converts all strings coming in to double and strings going out to single? AAaron123 Microsoft C# .NET 3 23rd Feb 2009 01:08 PM
How to find number of pairs of strings from list of strings? greg_overholt Microsoft Excel Worksheet Functions 5 27th Jan 2006 10:42 PM
I NEED A CODE TO SEPARATE NUMERIC STRINGS, FROM STRINGS IN PARENTH =?Utf-8?B?WmV0b255?= Microsoft Access VBA Modules 8 5th Dec 2005 03:45 PM
Make Word displays strings of text, not strings of code =?Utf-8?B?WGVybw==?= Microsoft Word Document Management 2 9th Dec 2004 10:35 AM
Where are Windows dialog strings and title strings stored? Bob Hiller Microsoft Windows 2000 Developer 0 1st Oct 2003 09:02 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 07:31 PM.