tags:

views:

104

answers:

4
+1  Q: 

Help: c++ to c#

Hi, in c++:

byte des16[16];
....
byte *d = des16+8;

in c#?

byte des16[16];
    [0] 207 'Ï' unsigned char
    [1] 216 'Ø' unsigned char
    [2] 108 'l' unsigned char
    [3] 93 ']'  unsigned char
    [4] 249 'ù' unsigned char
    [5] 249 'ù' unsigned char
    [6] 100 'd' unsigned char
    [7] 0   unsigned char
    [8] 76 'L'  unsigned char
    [9] 50 '2'  unsigned char
    [10]    104 'h' unsigned char
    [11]    118 'v' unsigned char
    [12]    104 'h' unsigned char
    [13]    191 '¿' unsigned char
    [14]    171 '«' unsigned char
    [15]    199 'Ç' unsigned char

after
 byte *d = des16+8;
d = "L2hvh¿«Ç†¿æ^ òÎL2hvh¿«Ç"
+8  A: 

C# (generally speaking) has no pointers. Maybe the following is what you are after:

byte[] des16 = new byte[16];
byte byteAtIndex8 = des16[8];

This code gives you the element at index 8.

If I read your code correctly, you are trying to get the address of the element at index 8. This is - generally speaking - not possible with C# (unless you use unsafe code).

John
Thanks guys but still not it.
edv4ld0
A: 
byte[] des16 = new byte[16];
....
byte d = des16[8];

Unless you use unsafe code you cannot retrieve a pointer.

Steven
Thanks guys but still not it.
edv4ld0
+2  A: 

I think this would be more appropriate (though it depends on how d is used):

byte[] des16 = new byte[16];
IEnumerable<byte> d = des16.Skip(8);

Using pure managed code, you cannot use pointers to locations. Since d takes a pointer to the 8th element of the array, the closest analog would be creating an enumeration of des16 skipping the first 8 items. If you are just iterating through the items, this would be the best choice.

I should also mention that Skip() is one of many extension methods available for arrays (and other IEnumerables) in .Net 3.5 (VS2008/VS2010) and up which I could only assume you were using. You wouldn't be able to use it if you are using .Net 2.0 (VS2003/VS2005).

If d is used to access the offset elements in des16 like an array, it could be converted to an array as well.

byte[] d = des16.Skip(8).ToArray();

Note this creates a separate instance of an array which contains the items in des16 excluding the first 8.

Otherwise it's not completely clear what the best use would be without seeing how it is used.

[edit]

It appears you are working with null-terminated strings in a buffer in .Net 2.0 possibly (if Skip() isn't available). If you want the string representation, you can convert it to a native string object.

byte[] des16 = new byte[16];
char[] chararr = Array.ConvertAll(des16, delegate(byte b) { return (char)b; }); //convert to an array of characters
string str = new String(chararr, 8, chararr-8); //create the string
Jeff M
True, but this will require some explanation to a C++-go-er.
Dykam
Point taken. I should elaborate.
Jeff M
Thanks guys but still not it. 'System.Array' does not contain a definition for 'Skip'.
edv4ld0
A: 

Jeff M Thanks for opening my eyes.

c++:

byte des16[16]; 
byte *d = des16+8; 

c#:

byte[] des16 = new byte[16];
byte[] b = new byte[8];
System.Array.Copy(des16, 8, b, 0, 8);
edv4ld0