CopyMemory Replacement??

hpdvs2

New member
Joined
Dec 31, 2002
Messages
3
Location
Bloomington, IL
In VB6 I used the API CopyMemory to do byte for byte copies between Integers and Strings. In VB6 an int was 2 bytes, and a long was 4 bytes.

A great deal of my Code Modeling is based off of the ability to do this. I need to do a byte for byte copy between Strings and Integers. Particularly Im intrested in Int64, which is 8 bytes of data.

Please do not respond to this if your answer doesnt always require that the string by the same byte length as the Integer. Thank you.
 
Are you saying that you have strings that are 8 bytes are less and you want to copy the string into an Int64 variable?

Can you not convert the string to a char array and then shove the char (as a byte) into your Int64 variable?

If you *must* use CopyMemory, you can still use the API - look for RtlMoveMemory and alias it as CopyMemory. Just declare the first and second arguments as the .NET types (string, Int64).

-ner
 
Update...

CopyMemory Does not work. I had attempted to use it by switching Any with a combination of int64/String.

In Vb6 that might have worked, but The actual Data is hidden behind the objects. When it is Integers or Single Characters, Copy Memory works perfectly, but that does not matter. each of those where only one line of code away from the same thing.

But Strings it seems, that It directly accesses the Pointer. I cannot access the String Data itself. I set the destination and source of CopyMEm to string, then it makes both strings point to the source. Which means that If I try to copy 4bytes of the string, (4 byte ptr) that I will have all the bytes in the second string, even if there are 2 megs worth.

I need to find out how to get direct access to the string, the real DATA store of the string. Strings are so hidden behind objects now that it makes these things impossible. Please prove that last statement wrong.
 
Last edited by a moderator:
Nerseus, How do you propose that I "Shove" each byte into the int64? Is there a way that you can build up an int64 easily from Bytes in VB Code? Please let my know. (PS, Multiplying the second byte by 256, then the second by 65k, etc... is a huge amount of processing compaired to a memory copy.
 
Heres a C# code snippet (sorry, I dont know VB.NET very well but Im sure youll get the idea):

Code:
Int64 iValue = 0;
double power = 0.0;
string s = "abc123";
			
// Make sure s is 8 chars or less.
if (s.Length>8) throw new Exception("String too large...");		
foreach(char c in s.ToCharArray())
{
	iValue += (Int64)(((byte)c) * Math.Pow(256.0, power));
	power++;
}

System.Diagnostics.Debug.WriteLine(String.Format("Int Value of {0} is {1}, hex={2}", s, iValue, iValue.ToString("x")));

You may be able to use the above to convert the string into a Char array (a method off the string object) and pass the char array to CopyMemory.

Good luck

-ner
 
Back
Top