By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,573 Members | 1,924 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,573 IT Pros & Developers. It's quick & easy.

Byte array to string

P: n/a
I cannot get the following function to work. A System.NullReferenceError
exception occurs when CopyMemory is executed. However, during debug I found
that none of the parameters passed are Null.
Is there a .NET way of doing this without using the CopyMemory API function?

Function ByteArrayToString(ByVal byteArray() As Byte, ByVal start As
Integer, ByVal length As Integer)
Dim temp As New String(Chr(0), length+1)
CopyMemory(temp, byteArray(start), length)
Return temp.ToString()
End Function

--
Thanks for any help,
Shayne H


Nov 20 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Tim
Shayne,

Why not just use System.Text.Encoding.GetString(byte()) ?
Tim
Nov 20 '05 #2

P: n/a
Thanks Tim, will do.

I would be interested to know why my function had the exception.

"Tim" <ti********@yahoo.com> wrote in message
news:uo**************@TK2MSFTNGP10.phx.gbl...
Shayne,

Why not just use System.Text.Encoding.GetString(byte()) ?
Tim

Nov 20 '05 #3

P: n/a
On 2003-10-05, Shayne H <> wrote:
I cannot get the following function to work. A System.NullReferenceError
exception occurs when CopyMemory is executed. However, during debug I found
that none of the parameters passed are Null.
Is there a .NET way of doing this without using the CopyMemory API function?

Function ByteArrayToString(ByVal byteArray() As Byte, ByVal start As
Integer, ByVal length As Integer)
Dim temp As New String(Chr(0), length+1)
CopyMemory(temp, byteArray(start), length)
Return temp.ToString()
End Function

--
Thanks for any help,
Shayne H


As was pointed out - don't use copymem for this - use the appropriate
System.Text.Encoding class for the job. In fact, using CopyMemory in
..NET is a little dangerous since objects can be relocated on the heap.
If you have to use it, you will want to pin the objects before the call
- basically you tell the runtime not to relocate them until I say it
is ok :) (look at the System.Runtime.InteropServices.GCHandle
structure, for more information)

But, to answer your question about the nullreference exception - well
with out seeing your declaration of CopyMemory - I would bet that it is
wrong. If you could post it, it would be easier to tell :)

Tom Shelton
Nov 20 '05 #4

P: n/a
Thanks Tom,

I used an overloaded declaration

Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" ( _
ByVal destination As Object, _
ByVal source As Object, _
ByVal length As Integer)

Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" ( _
ByVal destination As String, _
ByVal source As Object, _
ByVal length As Integer)

"Tom Shelton" <to*@mtogden.com> wrote in message
news:en**************@TK2MSFTNGP12.phx.gbl...
On 2003-10-05, Shayne H <> wrote:
I cannot get the following function to work. A System.NullReferenceError
exception occurs when CopyMemory is executed. However, during debug I found that none of the parameters passed are Null.
Is there a .NET way of doing this without using the CopyMemory API function?
Function ByteArrayToString(ByVal byteArray() As Byte, ByVal start As
Integer, ByVal length As Integer)
Dim temp As New String(Chr(0), length+1)
CopyMemory(temp, byteArray(start), length)
Return temp.ToString()
End Function

--
Thanks for any help,
Shayne H


As was pointed out - don't use copymem for this - use the appropriate
System.Text.Encoding class for the job. In fact, using CopyMemory in
.NET is a little dangerous since objects can be relocated on the heap.
If you have to use it, you will want to pin the objects before the call
- basically you tell the runtime not to relocate them until I say it
is ok :) (look at the System.Runtime.InteropServices.GCHandle
structure, for more information)

But, to answer your question about the nullreference exception - well
with out seeing your declaration of CopyMemory - I would bet that it is
wrong. If you could post it, it would be easier to tell :)

Tom Shelton

Nov 20 '05 #5

P: n/a
"Shayne H" <shaynehATlycosSPAMGOTOHELLcoDOTuk> scripsit:
I cannot get the following function to work. A System.NullReferenceError
exception occurs when CopyMemory is executed. However, during debug I found
that none of the parameters passed are Null.
Is there a .NET way of doing this without using the CopyMemory API function?

Function ByteArrayToString(ByVal byteArray() As Byte, ByVal start As
Integer, ByVal length As Integer)
Dim temp As New String(Chr(0), length+1)
CopyMemory(temp, byteArray(start), length)
Return temp.ToString()
End Function


How do you declare 'CopyMemory'? Notice that strings are immutable and
cannot/should not be changed by unmanaged code. I would use
'System.Text.Encoding.*.GetString' methods.

--
Herfried K. Wagner
MVP VB Classic, VB.NET
<http://www.mvps.org/dotnet>
Nov 20 '05 #6

P: 1
Thanks dudes. Put me on the path to success! :cool:
Jun 29 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.