473,545 Members | 2,627 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Help converting Buffer of Bytes to string

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

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 4164
David,

"David" <da***@orbitcom s.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.En coding.{encodin g 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.N ullChar' 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******** ********@TK2MSF TNGP09.phx.gbl. ..
David,

"David" <da***@orbitcom s.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.En coding.{encodin g 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.N ullChar' 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***@orbitcom s.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(hV o, hInstance.ToInt 32, 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.nul l.

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(hV o, hInstance.ToInt 32, 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.nul l.

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
10281
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 bytes that are not printable characters? I've tested this and it seems to work fine, but I want to make sure there isn't some condition or situation...
8
2999
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 a specific extension in a specific directory. The files are uploaded by FTP to this directory. The service then does the following steps: 1)...
8
5455
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 -------------------------------------------------------------------------------- Hello, I have a very simple problem but cannot seem to figure it out. I have a very simple php script that sends a test email to myself. When I...
3
2330
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 need to be converted to an enum. The next 4 bytes to a 32-bit int. And so on. I'm basically populating a struct with the data. How does one do this...
4
2324
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 them in the debugger). After encoding the string (the sameone which complete value is not visible from the debugger) all the values can be seen but...
1
12392
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 method for converting an array of sbytes to an array of bytes? Thanks, Darrel
2
4350
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. The binary data is how they send numbers (they call it "Big Endian"). I only know at run time whether a byte is going to be text or binary (one of the...
2
11571
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 void loadBytes(InputStream is){ DataSource ds=new DataSource(is); player = Manager.createPlayer(ds);
5
16960
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 4 bytes ) we defined #ifdef WIN32 #define t_wchar_t wchar_t #else // LINUX #define t_wchar_t short
0
7496
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7685
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7452
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7784
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6014
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5354
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5071
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3485
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3467
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.