Problem with casting

A

AMP

Hello,
First, Thanks for ALL your help.
Second, another question:
I have some c code i am turning into c#:(truncated)


WORD calcChecksum(BYTE data[], WORD length)
/* Calculates a checksum of "data".
*/
{
WORD* i_data;
WORD checksum= 0;
BYTE i= 0;

i_data= (WORD*)data;

TURNS INTO:

ushort calcChecksum(byte[] data, ushort length)
/* Calculates a checksum of "data".
*/
{
ushort i_data;
ushort checksum= 0;
byte i= 0;

i_data= (ushort)data;


I am getting an error that :
Cannot convert type 'byte[]' to 'ushort'

If it can be done in c I'm sure I'm just missing something.
Any help would be appreciated.
Thanks
Mike
 
N

Nicholas Paldino [.NET/C# MVP]

Mike,

It's not as easy as it seems.

In the C code, i_data is a pointer to a WORD variable type. In "safe"
code in .NET, this doesn't exist. If you used unsafe code, you could easily
cast the pointer of the array of bytes to a pointer to an unsigned short.

I would go about this a different way. What I would do is cycle through
your bytes (you are cycling through to create a checksum, right?) two at a
time.

For each iteration of the loop, you would pass the reference to the byte
array to the ToUInt16 method on the BitConverter class and indicate the
index in the byte array that you want to extract the unsigned short from.
Then, you can perform your operation on the unsigned short that is returned.

Hope this helps.
 
A

AMP

Nicholas,
Dont think I'm asking you to write the code for me (Maybe this time).I
think I would understand it alot better if you could rewrite it for me.
I have several of these to do, so if I get the method down and step
through it.(without spending forever), I would be greatful.
Thia is the entire function:

ushort calcChecksum(byte[] data, ushort length)
/* Calculates a checksum of "data".
*/
{
ushort i_data;
ushort checksum= 0;
byte i= 0;

i_data= (ushort)(data);

for (i= 0; i < length/2; i++)
{
checksum^= i_data; /* xor-ing */
}
return(checksum ^ 0xffff); /* inverting */
}


Thanks
Mike
Mike,

It's not as easy as it seems.

In the C code, i_data is a pointer to a WORD variable type. In "safe"
code in .NET, this doesn't exist. If you used unsafe code, you could easily
cast the pointer of the array of bytes to a pointer to an unsigned short.

I would go about this a different way. What I would do is cycle through
your bytes (you are cycling through to create a checksum, right?) two at a
time.

For each iteration of the loop, you would pass the reference to the byte
array to the ToUInt16 method on the BitConverter class and indicate the
index in the byte array that you want to extract the unsigned short from.
Then, you can perform your operation on the unsigned short that is returned.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

AMP said:
Hello,
First, Thanks for ALL your help.
Second, another question:
I have some c code i am turning into c#:(truncated)


WORD calcChecksum(BYTE data[], WORD length)
/* Calculates a checksum of "data".
*/
{
WORD* i_data;
WORD checksum= 0;
BYTE i= 0;

i_data= (WORD*)data;

TURNS INTO:

ushort calcChecksum(byte[] data, ushort length)
/* Calculates a checksum of "data".
*/
{
ushort i_data;
ushort checksum= 0;
byte i= 0;

i_data= (ushort)data;


I am getting an error that :
Cannot convert type 'byte[]' to 'ushort'

If it can be done in c I'm sure I'm just missing something.
Any help would be appreciated.
Thanks
Mike
 
N

Nicholas Paldino [.NET/C# MVP]

it would be more helpful if you posted the C code.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

AMP said:
Nicholas,
Dont think I'm asking you to write the code for me (Maybe this time).I
think I would understand it alot better if you could rewrite it for me.
I have several of these to do, so if I get the method down and step
through it.(without spending forever), I would be greatful.
Thia is the entire function:

ushort calcChecksum(byte[] data, ushort length)
/* Calculates a checksum of "data".
*/
{
ushort i_data;
ushort checksum= 0;
byte i= 0;

i_data= (ushort)(data);

for (i= 0; i < length/2; i++)
{
checksum^= i_data; /* xor-ing */
}
return(checksum ^ 0xffff); /* inverting */
}


Thanks
Mike
Mike,

It's not as easy as it seems.

In the C code, i_data is a pointer to a WORD variable type. In
"safe"
code in .NET, this doesn't exist. If you used unsafe code, you could
easily
cast the pointer of the array of bytes to a pointer to an unsigned short.

I would go about this a different way. What I would do is cycle
through
your bytes (you are cycling through to create a checksum, right?) two at
a
time.

For each iteration of the loop, you would pass the reference to the
byte
array to the ToUInt16 method on the BitConverter class and indicate the
index in the byte array that you want to extract the unsigned short from.
Then, you can perform your operation on the unsigned short that is
returned.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

AMP said:
Hello,
First, Thanks for ALL your help.
Second, another question:
I have some c code i am turning into c#:(truncated)


WORD calcChecksum(BYTE data[], WORD length)
/* Calculates a checksum of "data".
*/
{
WORD* i_data;
WORD checksum= 0;
BYTE i= 0;

i_data= (WORD*)data;

TURNS INTO:

ushort calcChecksum(byte[] data, ushort length)
/* Calculates a checksum of "data".
*/
{
ushort i_data;
ushort checksum= 0;
byte i= 0;

i_data= (ushort)data;


I am getting an error that :
Cannot convert type 'byte[]' to 'ushort'

If it can be done in c I'm sure I'm just missing something.
Any help would be appreciated.
Thanks
Mike
 
A

AMP

WORD calcChecksum(BYTE data[], WORD length)
/* Calculates a checksum of "data".
*/
{
WORD* i_data;
WORD checksum= 0;
BYTE i= 0;

i_data= (WORD*)data;

for (i= 0; i < length/2; i++)
{
checksum^= i_data; /* xor-ing */
}
return(checksum ^ 0xffff); /* inverting */
}

THANKS!!


it would be more helpful if you posted the C code.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

AMP said:
Nicholas,
Dont think I'm asking you to write the code for me (Maybe this time).I
think I would understand it alot better if you could rewrite it for me.
I have several of these to do, so if I get the method down and step
through it.(without spending forever), I would be greatful.
Thia is the entire function:

ushort calcChecksum(byte[] data, ushort length)
/* Calculates a checksum of "data".
*/
{
ushort i_data;
ushort checksum= 0;
byte i= 0;

i_data= (ushort)(data);

for (i= 0; i < length/2; i++)
{
checksum^= i_data; /* xor-ing */
}
return(checksum ^ 0xffff); /* inverting */
}


Thanks
Mike
Mike,

It's not as easy as it seems.

In the C code, i_data is a pointer to a WORD variable type. In
"safe"
code in .NET, this doesn't exist. If you used unsafe code, you could
easily
cast the pointer of the array of bytes to a pointer to an unsigned short.

I would go about this a different way. What I would do is cycle
through
your bytes (you are cycling through to create a checksum, right?) two at
a
time.

For each iteration of the loop, you would pass the reference to the
byte
array to the ToUInt16 method on the BitConverter class and indicate the
index in the byte array that you want to extract the unsigned short from.
Then, you can perform your operation on the unsigned short that is
returned.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Hello,
First, Thanks for ALL your help.
Second, another question:
I have some c code i am turning into c#:(truncated)


WORD calcChecksum(BYTE data[], WORD length)
/* Calculates a checksum of "data".
*/
{
WORD* i_data;
WORD checksum= 0;
BYTE i= 0;

i_data= (WORD*)data;

TURNS INTO:

ushort calcChecksum(byte[] data, ushort length)
/* Calculates a checksum of "data".
*/
{
ushort i_data;
ushort checksum= 0;
byte i= 0;

i_data= (ushort)data;


I am getting an error that :
Cannot convert type 'byte[]' to 'ushort'

If it can be done in c I'm sure I'm just missing something.
Any help would be appreciated.
Thanks
Mike
 
N

Nicholas Paldino [.NET/C# MVP]

public ushort CalcChecksum(byte[] data, ushort length)
{
// The return value.
ushort retVal = 0;

// Cycle through the bytes.
for (ushort index = 0; index < length / 2; ++index)
{
// Get the current unsigned short.
ushort current = BitConverter.ToUInt16(data, index * 2);

// Alter the checksum.
retVal ^= current;
}

// Return the checksum.
return (ushort) (retVal ^ 0xffff);
}


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

AMP said:
WORD calcChecksum(BYTE data[], WORD length)
/* Calculates a checksum of "data".
*/
{
WORD* i_data;
WORD checksum= 0;
BYTE i= 0;

i_data= (WORD*)data;

for (i= 0; i < length/2; i++)
{
checksum^= i_data; /* xor-ing */
}
return(checksum ^ 0xffff); /* inverting */
}

THANKS!!


it would be more helpful if you posted the C code.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

AMP said:
Nicholas,
Dont think I'm asking you to write the code for me (Maybe this time).I
think I would understand it alot better if you could rewrite it for me.
I have several of these to do, so if I get the method down and step
through it.(without spending forever), I would be greatful.
Thia is the entire function:

ushort calcChecksum(byte[] data, ushort length)
/* Calculates a checksum of "data".
*/
{
ushort i_data;
ushort checksum= 0;
byte i= 0;

i_data= (ushort)(data);

for (i= 0; i < length/2; i++)
{
checksum^= i_data; /* xor-ing */
}
return(checksum ^ 0xffff); /* inverting */
}


Thanks
Mike

Nicholas Paldino [.NET/C# MVP] wrote:
Mike,

It's not as easy as it seems.

In the C code, i_data is a pointer to a WORD variable type. In
"safe"
code in .NET, this doesn't exist. If you used unsafe code, you could
easily
cast the pointer of the array of bytes to a pointer to an unsigned
short.

I would go about this a different way. What I would do is cycle
through
your bytes (you are cycling through to create a checksum, right?) two
at
a
time.

For each iteration of the loop, you would pass the reference to
the
byte
array to the ToUInt16 method on the BitConverter class and indicate
the
index in the byte array that you want to extract the unsigned short
from.
Then, you can perform your operation on the unsigned short that is
returned.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Hello,
First, Thanks for ALL your help.
Second, another question:
I have some c code i am turning into c#:(truncated)


WORD calcChecksum(BYTE data[], WORD length)
/* Calculates a checksum of "data".
*/
{
WORD* i_data;
WORD checksum= 0;
BYTE i= 0;

i_data= (WORD*)data;

TURNS INTO:

ushort calcChecksum(byte[] data, ushort length)
/* Calculates a checksum of "data".
*/
{
ushort i_data;
ushort checksum= 0;
byte i= 0;

i_data= (ushort)data;


I am getting an error that :
Cannot convert type 'byte[]' to 'ushort'

If it can be done in c I'm sure I'm just missing something.
Any help would be appreciated.
Thanks
Mike

 
A

AMP

Thank you so much!!
Mike said:
public ushort CalcChecksum(byte[] data, ushort length)
{
// The return value.
ushort retVal = 0;

// Cycle through the bytes.
for (ushort index = 0; index < length / 2; ++index)
{
// Get the current unsigned short.
ushort current = BitConverter.ToUInt16(data, index * 2);

// Alter the checksum.
retVal ^= current;
}

// Return the checksum.
return (ushort) (retVal ^ 0xffff);
}


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

AMP said:
WORD calcChecksum(BYTE data[], WORD length)
/* Calculates a checksum of "data".
*/
{
WORD* i_data;
WORD checksum= 0;
BYTE i= 0;

i_data= (WORD*)data;

for (i= 0; i < length/2; i++)
{
checksum^= i_data; /* xor-ing */
}
return(checksum ^ 0xffff); /* inverting */
}

THANKS!!


it would be more helpful if you posted the C code.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Nicholas,
Dont think I'm asking you to write the code for me (Maybe this time).I
think I would understand it alot better if you could rewrite it for me.
I have several of these to do, so if I get the method down and step
through it.(without spending forever), I would be greatful.
Thia is the entire function:

ushort calcChecksum(byte[] data, ushort length)
/* Calculates a checksum of "data".
*/
{
ushort i_data;
ushort checksum= 0;
byte i= 0;

i_data= (ushort)(data);

for (i= 0; i < length/2; i++)
{
checksum^= i_data; /* xor-ing */
}
return(checksum ^ 0xffff); /* inverting */
}


Thanks
Mike

Nicholas Paldino [.NET/C# MVP] wrote:
Mike,

It's not as easy as it seems.

In the C code, i_data is a pointer to a WORD variable type. In
"safe"
code in .NET, this doesn't exist. If you used unsafe code, you could
easily
cast the pointer of the array of bytes to a pointer to an unsigned
short.

I would go about this a different way. What I would do is cycle
through
your bytes (you are cycling through to create a checksum, right?) two
at
a
time.

For each iteration of the loop, you would pass the reference to
the
byte
array to the ToUInt16 method on the BitConverter class and indicate
the
index in the byte array that you want to extract the unsigned short
from.
Then, you can perform your operation on the unsigned short that is
returned.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Hello,
First, Thanks for ALL your help.
Second, another question:
I have some c code i am turning into c#:(truncated)


WORD calcChecksum(BYTE data[], WORD length)
/* Calculates a checksum of "data".
*/
{
WORD* i_data;
WORD checksum= 0;
BYTE i= 0;

i_data= (WORD*)data;

TURNS INTO:

ushort calcChecksum(byte[] data, ushort length)
/* Calculates a checksum of "data".
*/
{
ushort i_data;
ushort checksum= 0;
byte i= 0;

i_data= (ushort)data;


I am getting an error that :
Cannot convert type 'byte[]' to 'ushort'

If it can be done in c I'm sure I'm just missing something.
Any help would be appreciated.
Thanks
Mike

 

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