Byte array to string

S

Shayne H

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
 
S

Shayne H

Thanks Tim, will do.

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

Tom Shelton

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

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
 
S

Shayne H

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

Herfried K. Wagner [MVP]

Shayne H said:
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.
 

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