Home All Groups Group Topic Archive Search About

Help converting Buffer of Bytes to string

Author
27 Feb 2005 9:34 PM
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

Author
6 Apr 2005 12:35 PM
smaggi
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

Show quoteHide quote
"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
>
>
>
Author
6 Apr 2005 4:39 PM
smaggi
You can Null terminate a byte array by

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


Show quoteHide quote
"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
> >
> >
> >