Hex() in C#

G

Guest

Hi,

Does C# have an equivalent function of Hex() -> "Returns a string
representing the hexadecimal value of an integer" in VB?

thanks,
 
G

Guest

Thanks Marc, how about "VarPtr()"?
I have created this function. Is it work as VarPtr() in VB?

private static int VarPtr(object o)
{
System.Runtime.InteropServices.GCHandle GC;
GC = System.Runtime.InteropServices.GCHandle.Alloc(o,
System.Runtime.InteropServices.GCHandleType.Pinned);

int ret = GC.AddrOfPinnedObject().ToInt32();

GC.Free();
return ret;
}
 
M

Marc Gravell

Damned if I know... looks like some code I found on google, but my gut tells
me that it might not work as expected after you have Free()'d the handle...
this may unpin the object rendering the address useless? I'm talking
gut-feel here - this isn't something I've ever done...

If this method is provided by the VB.Net migration dlls, you might want to
reference these dlls (from C# - not a problem) to use them directly? Else
load in reflector and see what they do...

Marc
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

Why you convert it to int32 ?

IMO you should keep it as IntPtr, it's more descriptive and less error
prone.

I feel the same regarding the Free call, you are releasing it so maybe you
lost the referenced memory.
 
G

Guest

Hi Ignacio Machin ,

I'm not sure about this function because I found it in Google.
I tried to add the reference to Microsoft.VisualBasic but I didn't see
VarPtr() function.

Could you explain more about how to use IntPtr?

thanks,
ano
 
C

Chris Dunaway

ano said:
I'm not sure about this function because I found it in Google.
I tried to add the reference to Microsoft.VisualBasic but I didn't see
VarPtr() function.

What are you doing that requires VarPtr? Perhaps there is another way
to accomplish what you want.
 
M

Mattias Sjögren

Damned if I know... looks like some code I found on google, but my gut tells
me that it might not work as expected after you have Free()'d the handle...
this may unpin the object rendering the address useless? I'm talking
gut-feel here - this isn't something I've ever done...

You're right, the code is definitely broken and the "pointer" it
returns is useless.


Mattias
 

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