PC Review


Reply
Thread Tools Rate Thread

Byte array to string

 
 
Shayne H
Guest
Posts: n/a
 
      5th Oct 2003
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




 
Reply With Quote
 
 
 
 
Tim
Guest
Posts: n/a
 
      5th Oct 2003
Shayne,

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


Tim


 
Reply With Quote
 
Shayne H
Guest
Posts: n/a
 
      5th Oct 2003
Thanks Tim, will do.

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

"Tim" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Shayne,
>
> Why not just use System.Text.Encoding.GetString(byte()) ?
>
>
> Tim
>
>



 
Reply With Quote
 
Tom Shelton
Guest
Posts: n/a
 
      5th Oct 2003
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
 
Reply With Quote
 
Shayne H
Guest
Posts: n/a
 
      5th Oct 2003
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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



 
Reply With Quote
 
Herfried K. Wagner [MVP]
Guest
Posts: n/a
 
      5th Oct 2003
"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>
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Byte array to string =?Utf-8?B?cGt1bWFy?= Microsoft C# .NET 3 2nd Aug 2005 06:54 AM
array byte to string Hrvoje Voda Microsoft C# .NET 1 20th Jun 2005 11:50 AM
Re: Byte Array to Printable String to Byte Array Jon Skeet [C# MVP] Microsoft Dot NET 0 4th Aug 2004 01:53 PM
String from a byte array John Dann Microsoft VB .NET 2 11th Jul 2004 10:31 AM
string >> byte array >> string representation >> byte array >> string !! David Bargna Microsoft VB .NET 4 6th Oct 2003 04:02 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 04:46 PM.