473,385 Members | 1,846 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Problem with casting

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

Sep 12 '06 #1
6 1413
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]
- mv*@spam.guard.caspershouse.com

"AMP" <am******@gmail.comwrote in message
news:11*********************@i42g2000cwa.googlegro ups.com...
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

Sep 12 '06 #2
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[i]; /* 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]
- mv*@spam.guard.caspershouse.com

"AMP" <am******@gmail.comwrote in message
news:11*********************@i42g2000cwa.googlegro ups.com...
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
Sep 12 '06 #3
it would be more helpful if you posted the C code.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"AMP" <am******@gmail.comwrote in message
news:11**********************@i3g2000cwc.googlegro ups.com...
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[i]; /* 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]
- mv*@spam.guard.caspershouse.com

"AMP" <am******@gmail.comwrote in message
news:11*********************@i42g2000cwa.googlegr oups.com...
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

Sep 12 '06 #4
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[i]; /* xor-ing */
}
return(checksum ^ 0xffff); /* inverting */
}

THANKS!!

Nicholas Paldino [.NET/C# MVP] wrote:
it would be more helpful if you posted the C code.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"AMP" <am******@gmail.comwrote in message
news:11**********************@i3g2000cwc.googlegro ups.com...
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[i]; /* 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]
- mv*@spam.guard.caspershouse.com

"AMP" <am******@gmail.comwrote in message
news:11*********************@i42g2000cwa.googlegro ups.com...
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
Sep 12 '06 #5
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]
- mv*@spam.guard.caspershouse.com

"AMP" <am******@gmail.comwrote in message
news:11*********************@e63g2000cwd.googlegro ups.com...
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[i]; /* xor-ing */
}
return(checksum ^ 0xffff); /* inverting */
}

THANKS!!

Nicholas Paldino [.NET/C# MVP] wrote:
>it would be more helpful if you posted the C code.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"AMP" <am******@gmail.comwrote in message
news:11**********************@i3g2000cwc.googlegr oups.com...
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[i]; /* 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]
- mv*@spam.guard.caspershouse.com

"AMP" <am******@gmail.comwrote in message
news:11*********************@i42g2000cwa.googlegr oups.com...
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


Sep 12 '06 #6
AMP
Thank you so much!!
Mike
Nicholas Paldino [.NET/C# MVP] wrote:
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]
- mv*@spam.guard.caspershouse.com

"AMP" <am******@gmail.comwrote in message
news:11*********************@e63g2000cwd.googlegro ups.com...
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[i]; /* xor-ing */
}
return(checksum ^ 0xffff); /* inverting */
}

THANKS!!

Nicholas Paldino [.NET/C# MVP] wrote:
it would be more helpful if you posted the C code.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"AMP" <am******@gmail.comwrote in message
news:11**********************@i3g2000cwc.googlegro ups.com...
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[i]; /* 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]
- mv*@spam.guard.caspershouse.com

"AMP" <am******@gmail.comwrote in message
news:11*********************@i42g2000cwa.googlegro ups.com...
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
Sep 12 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: ghostdog | last post by:
hi, i got this opengl/c++ code: <code> void render(CMesh *mesh){ ... float *pVertices; int *pIndices;
17
by: Jon Slaughter | last post by:
I'm having a little trouble understanding what the slicing problem is. In B.S.'s C++ PL3rdEd he says "Becayse the Employee copy functions do not know anything about Managers, only the Employee...
10
by: Ben | last post by:
Hi, I am a newbie with C and am trying to get a simple linked list working for my program. The structure of each linked list stores the char *data and *next referencing to the next link. The...
14
by: Jason Sewall | last post by:
Hello, I'm using the intel C++ 8.0 compiler on windows and I'm working on some software that uses the following lines of code: gf->invdim = 1.0/(double)dim; printf("invdim: %f\n",...
6
by: Carlo Marchesoni | last post by:
I have an ASP.NET/C# solution, where I can perfectly cast something I stored in the session object to a class of mine (BackEnd), as this: ->be = (BackEnd)Session;<- But if I try to do the same:...
2
by: ajikoe | last post by:
Hi, I tried to follow the example in swig homepage. I found error which I don't understand. I use bcc32, I already include directory where my python.h exist in bcc32.cfg. /* File : example.c...
39
by: Martin Jørgensen | last post by:
Hi, I'm relatively new with C-programming and even though I've read about pointers and arrays many times, it's a topic that is a little confusing to me - at least at this moment: ---- 1)...
11
by: Vinod | last post by:
Hi, I am working in the project where VC6 code is ported to VC8 (VC++ .Net 2005) I got a problem when I cast a double value to unsigned int. Problem is I couldn’t get the proper value after...
7
by: Ajeet | last post by:
hi I am having some difficulty in casting using generics. These are the classes. public interface IProvider<PROF> where PROF : IProviderProfile { //Some properties/methods }
4
by: Wally Barnes | last post by:
Can someone help a poor C++ programmer that learned the language before there was a standard lib .. etc ? Basically I have two classes that look something like below: template <class T>...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.