Help converting Buffer of Bytes to string

D

David

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
 
H

Herfried K. Wagner [MVP]

David,

David said:
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.
 
D

David

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.
 
H

Herfried K. Wagner [MVP]

David,

David said:
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.
 
G

Guest

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
 
G

Guest

You can Null terminate a byte array by

vo = Encoding.ASCII.GetBytes("Test")
ReDim Preserve vo(vo.Length)
 

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