468,251 Members | 1,390 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,251 developers. It's quick & easy.

Help converting Buffer of Bytes to string

I note that you can null teminate a string by adding controlchar.null.

Is there a way of adding a null to a Buffer of Bytes and converting it to a
string.

I have packets coming in from a serial ports as bytes and some of these
represent strings. (Like the Packed BCD date/time stamp etc).

At present I read through each Byte and convert to char and append to a
string.

In C, you can simply place a null at any position in he buffer and copy the
entire buffer to a string variable. Can this be dome in VB .NET ?

Thanks
Nov 21 '05 #1
5 3787
David,

"David" <da***@orbitcoms.com> schrieb:
Is there a way of adding a null to a Buffer of Bytes and converting it to
a
string.

I have packets coming in from a serial ports as bytes and some of these
represent strings. (Like the Packed BCD date/time stamp etc).

At present I read through each Byte and convert to char and append to a
string.
Take a look at the 'System.Text.Encoding.{encoding name}.GetString' methods.
These methods can be used to convert data stored in a byte array to a string
by using a specific encoding.
In C, you can simply place a null at any position in he buffer and copy
the
entire buffer to a string variable. Can this be dome in VB .NET ?


Notice that in .NET strings are /not/ null-terminated. In addition to that,
strings are immutable, which means that you cannot change a string's length
without creating a new string. If you want a string to be null-terminated,
simply append 'ControlChars.NullChar' to the string. When using p/invoke,
null-termination is not necessary because it's done by the marshaller
automatically.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Nov 21 '05 #2
Thanks for that.

I do not require a null terminted string. This was a hangover from C that I
thought was required.

If I can simply select a start position and number of characters in the
bytes array to convert to a string, this would be excellent.

"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
David,

"David" <da***@orbitcoms.com> schrieb:
Is there a way of adding a null to a Buffer of Bytes and converting it to a
string.

I have packets coming in from a serial ports as bytes and some of these
represent strings. (Like the Packed BCD date/time stamp etc).

At present I read through each Byte and convert to char and append to a
string.
Take a look at the 'System.Text.Encoding.{encoding name}.GetString'

methods. These methods can be used to convert data stored in a byte array to a string by using a specific encoding.
In C, you can simply place a null at any position in he buffer and copy
the
entire buffer to a string variable. Can this be dome in VB .NET ?
Notice that in .NET strings are /not/ null-terminated. In addition to

that, strings are immutable, which means that you cannot change a string's length without creating a new string. If you want a string to be null-terminated, simply append 'ControlChars.NullChar' to the string. When using p/invoke,
null-termination is not necessary because it's done by the marshaller
automatically.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Nov 21 '05 #3
David,

"David" <da***@orbitcoms.com> schrieb:
If I can simply select a start position and number of characters in the
bytes array to convert to a string, this would be excellent.


There is a 'GetString(Byte(), Integer, Integer)' overload that does what you
want to archieve.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Nov 21 '05 #4
I have a similar problem. I am calling a C API on a PocketPC using VB.NET.
The function declaration is

Declare Function newVo Lib "Test.dll" Alias "newVo" _
(ByRef hVo As IntPtr, ByVal hInstance As integer, ByVal
ByVal vo() As Byte, ByVal Rate As UInt32, ByVal coding
As String) As Integer

I call it like this.

Dim vo() As Byte
vo = Encoding.ASCII.GetBytes("Test")
ret = api.ttsNewVo(hVo, hInstance.ToInt32, vo, 16000, "L")

In have a log file enabled and it should list "Test.vde", but there is a
binary 04 and 08 character following "Test" and before the ".vde". Do I need
to NULL terminate the ASCII byte array or where are these extra characters
coming from?
What is the best way of adding a null to a Buffer of Bytes.

Thanks

"David" wrote:
I note that you can null teminate a string by adding controlchar.null.

Is there a way of adding a null to a Buffer of Bytes and converting it to a
string.

I have packets coming in from a serial ports as bytes and some of these
represent strings. (Like the Packed BCD date/time stamp etc).

At present I read through each Byte and convert to char and append to a
string.

In C, you can simply place a null at any position in he buffer and copy the
entire buffer to a string variable. Can this be dome in VB .NET ?

Thanks

Nov 21 '05 #5
You can Null terminate a byte array by

vo = Encoding.ASCII.GetBytes("Test")
ReDim Preserve vo(vo.Length)
"smaggi" wrote:
I have a similar problem. I am calling a C API on a PocketPC using VB.NET.
The function declaration is

Declare Function newVo Lib "Test.dll" Alias "newVo" _
(ByRef hVo As IntPtr, ByVal hInstance As integer, ByVal
ByVal vo() As Byte, ByVal Rate As UInt32, ByVal coding
As String) As Integer

I call it like this.

Dim vo() As Byte
vo = Encoding.ASCII.GetBytes("Test")
ret = api.ttsNewVo(hVo, hInstance.ToInt32, vo, 16000, "L")

In have a log file enabled and it should list "Test.vde", but there is a
binary 04 and 08 character following "Test" and before the ".vde". Do I need
to NULL terminate the ASCII byte array or where are these extra characters
coming from?
What is the best way of adding a null to a Buffer of Bytes.

Thanks

"David" wrote:
I note that you can null teminate a string by adding controlchar.null.

Is there a way of adding a null to a Buffer of Bytes and converting it to a
string.

I have packets coming in from a serial ports as bytes and some of these
represent strings. (Like the Packed BCD date/time stamp etc).

At present I read through each Byte and convert to char and append to a
string.

In C, you can simply place a null at any position in he buffer and copy the
entire buffer to a string variable. Can this be dome in VB .NET ?

Thanks

Nov 21 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Hal Vaughan | last post: by
3 posts views Thread by Pete Davis | last post: by
1 post views Thread by Darrel | last post: by
reply views Thread by NPC403 | last post: by
reply views Thread by kermitthefrogpy | last post: by
reply views Thread by zattat | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.