473,396 Members | 1,683 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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 4144
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Hal Vaughan | last post by:
If I have a byte and I convert it to string (String sData = new String(byte bData), then convert it back (byte bData = sData.getBytes()), will all data be intact, or do Strings have problems with...
8
by: MattP | last post by:
Ok, with the help of some examples found on the web and some minor modifications on our own, we have a simple and working encrypt and decrypt solution. It runs as a service, watches for files with...
8
by: baustin75 | last post by:
Posted: Mon Oct 03, 2005 1:41 pm Post subject: cannot mail() in ie only when debugging in php designer 2005 -------------------------------------------------------------------------------- ...
3
by: Pete Davis | last post by:
I've never done this in C# so I don't know what the appropriate way of doing it is. I've got an array of bytes and I need to convert the array into "usable" data. For example, the first 4 bytes...
4
by: Juan | last post by:
Does any one know if there are reported bugs when concatenating strings? When debugging each variable has the correct value but when I try to concatenate them some values are missing (I can´t see...
1
by: Darrel | last post by:
I am using binary writer to write an array of bytes to disk. However, my data starts out as an array of sbytes. I am currently type casting each array element in a for loop. Is there a faster...
2
by: DBuss | last post by:
OK, I'm reading a multicast socket. It attaches fine, reads fine, all of that. The problem is that while some of the data I get is normal text (ASCII String), some of it is Binary Integer. ...
2
by: shahiz | last post by:
basically im having null pointer exception //read an inputstream is = new DataInputStream(new FileInputStream("test.mpg")); loadBytes(is); //pass it as a datasource for the player public...
5
by: yakir22 | last post by:
Hello experts, I am dealing now in porting our server from windows to linux. our client is running only on windows machine. to avoid the wchar_t size problem ( in windows its 2 bytes and linux is...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.