By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,422 Members | 1,615 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,422 IT Pros & Developers. It's quick & easy.

Converting Datatypes

P: n/a
AMP
Hello,
I have in c:
WORD calcChecksum(BYTE data[], WORD length)
{
WORD* i_data;
WORD checksum= 0;
BYTE i= 0;

i_data= (WORD*)data;

data.. which is data[0] comes in at 128 and gets changed to 6272 by the
statement above.

in c# i did this

ushort i_data;
ushort checksum = 0;
byte i = 0;

i_data = (ushort)data[0];

but i_data dos not change here.

Help!
And Thanks

Sep 14 '06 #1
Share this Question
Share on Google+
12 Replies


P: n/a
C# does not have pointers. You are putting the value of data[0] into i_data.

You will have to rethink whatever you are doing.

AMP wrote:
Hello,
I have in c:
WORD calcChecksum(BYTE data[], WORD length)
{
WORD* i_data;
WORD checksum= 0;
BYTE i= 0;

i_data= (WORD*)data;

data.. which is data[0] comes in at 128 and gets changed to 6272 by the
statement above.

in c# i did this

ushort i_data;
ushort checksum = 0;
byte i = 0;

i_data = (ushort)data[0];

but i_data dos not change here.

Help!
And Thanks
Sep 14 '06 #2

P: n/a
AMP
It should not matter i_data=(WORD*)data is converting the value in
data(which is a byte value of 128 to a WORD(ushort) with a value of
6272...every time.
I dont know how, but i dont think the pointer is relative.

Ian Semmel wrote:
C# does not have pointers. You are putting the value of data[0] into i_data.

You will have to rethink whatever you are doing.

AMP wrote:
Hello,
I have in c:
WORD calcChecksum(BYTE data[], WORD length)
{
WORD* i_data;
WORD checksum= 0;
BYTE i= 0;

i_data= (WORD*)data;

data.. which is data[0] comes in at 128 and gets changed to 6272 by the
statement above.

in c# i did this

ushort i_data;
ushort checksum = 0;
byte i = 0;

i_data = (ushort)data[0];

but i_data dos not change here.

Help!
And Thanks
Sep 14 '06 #3

P: n/a
Actually, C# *does* have pointers. They are in an unsafe context, but they
are managed pointers.

--
HTH,

Kevin Spencer
Microsoft MVP
Chicken Salad Surgery

What You Seek Is What You Get.

"Ian Semmel" <is***********@NOKUNKrocketcomp.com.auwrote in message
news:eg**************@TK2MSFTNGP02.phx.gbl...
C# does not have pointers. You are putting the value of data[0] into
i_data.

You will have to rethink whatever you are doing.

AMP wrote:
>Hello,
I have in c:
WORD calcChecksum(BYTE data[], WORD length)
{
WORD* i_data;
WORD checksum= 0;
BYTE i= 0;

i_data= (WORD*)data;

data.. which is data[0] comes in at 128 and gets changed to 6272 by the
statement above.

in c# i did this

ushort i_data;
ushort checksum = 0;
byte i = 0;

i_data = (ushort)data[0];

but i_data dos not change here.

Help!
And Thanks

Sep 14 '06 #4

P: n/a
AMP
Aint helping me....
I'm trying to do everything without pointers.
Kevin Spencer wrote:
Actually, C# *does* have pointers. They are in an unsafe context, but they
are managed pointers.

--
HTH,

Kevin Spencer
Microsoft MVP
Chicken Salad Surgery

What You Seek Is What You Get.

"Ian Semmel" <is***********@NOKUNKrocketcomp.com.auwrote in message
news:eg**************@TK2MSFTNGP02.phx.gbl...
C# does not have pointers. You are putting the value of data[0] into
i_data.

You will have to rethink whatever you are doing.

AMP wrote:
Hello,
I have in c:
WORD calcChecksum(BYTE data[], WORD length)
{
WORD* i_data;
WORD checksum= 0;
BYTE i= 0;

i_data= (WORD*)data;

data.. which is data[0] comes in at 128 and gets changed to 6272 by the
statement above.

in c# i did this

ushort i_data;
ushort checksum = 0;
byte i = 0;

i_data = (ushort)data[0];

but i_data dos not change here.

Help!
And Thanks
Sep 14 '06 #5

P: n/a

"AMP" <am******@gmail.comwrote in message
news:11*********************@m73g2000cwd.googlegro ups.com...
| Hello,
| I have in c:
| WORD calcChecksum(BYTE data[], WORD length)
| {
| WORD* i_data;
| WORD checksum= 0;
| BYTE i= 0;
|
| i_data= (WORD*)data;
|
| data.. which is data[0] comes in at 128 and gets changed to 6272 by the
| statement above.
|

Your C function takes a BYTE (an unsigned char) that is 8 bits, but you
treat it as a WORD (16 bits)

| in c# i did this
|
| ushort i_data;
| ushort checksum = 0;
| byte i = 0;
|
| i_data = (ushort)data[0];
|
| but i_data dos not change here.
|
| Help!
| And Thanks
|

Not sure what you are trying here, are you calling a C function from C# or
are you implementing the above C function in C#?
Please, post a complete sample that illustrates the issue.
Willy.

Sep 14 '06 #6

P: n/a
AMP
I was, and am still thinking that:
i_data= (WORD*)data;

converts it into a ushort.

Here is the c:

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 */
}
return (ushort)(checksum ^ 0xffff);

}
As I run this through Visual Studio, that particular statement( i_data=
(WORD*)data;) turns i_data to 6272. But data is 128.

One more thing that may be inportant:
There is 2 typedefs
typedef unsigned char BYTE;
typedef unsigned short WORD;

I dont know how inportant they are. Is there a difference between a
byte and an unsigned char?
MANY THANKS
Mike


Willy Denoyette [MVP] wrote:
"AMP" <am******@gmail.comwrote in message
news:11*********************@m73g2000cwd.googlegro ups.com...
| Hello,
| I have in c:
| WORD calcChecksum(BYTE data[], WORD length)
| {
| WORD* i_data;
| WORD checksum= 0;
| BYTE i= 0;
|
| i_data= (WORD*)data;
|
| data.. which is data[0] comes in at 128 and gets changed to 6272 by the
| statement above.
|

Your C function takes a BYTE (an unsigned char) that is 8 bits, but you
treat it as a WORD (16 bits)

| in c# i did this
|
| ushort i_data;
| ushort checksum = 0;
| byte i = 0;
|
| i_data = (ushort)data[0];
|
| but i_data dos not change here.
|
| Help!
| And Thanks
|

Not sure what you are trying here, are you calling a C function from C# or
are you implementing the above C function in C#?
Please, post a complete sample that illustrates the issue.
Willy.
Sep 14 '06 #7

P: n/a

"AMP" <am******@gmail.comwrote in message
news:11**********************@h48g2000cwc.googlegr oups.com...
|I was, and am still thinking that:
| i_data= (WORD*)data;
|
| converts it into a ushort.
|
Sure but with the cast you pretend that you are pointing to a WORD, that
means that you take both data[0] AND data[1] and vove it to an WORD. That
means if data[0] = 128 (0x80) and data[1] = 24 (0x18). the result i_data
hold 0x8018 after the move (that's 6272 on intel achitectures).

| Here is the c:
|
| 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 */
| }
| return (ushort)(checksum ^ 0xffff);
|
|
|
| }
|
|
| As I run this through Visual Studio, that particular statement( i_data=
| (WORD*)data;) turns i_data to 6272. But data is 128.
|

That's correct (assumed data[1] = 0x18, what did you expect?

| One more thing that may be inportant:
| There is 2 typedefs
| typedef unsigned char BYTE;
| typedef unsigned short WORD;
|
| I dont know how inportant they are. Is there a difference between a
| byte and an unsigned char?

No. the types says threat BYTE as an unsigned char in code.
But, where is the C# question in this posting, I would suggest you to post
to a C language NG like microsoft.public.dotnet.languages.vc.

Willy.

Sep 14 '06 #8

P: n/a
AMP
Willy,
I am trying to turn the afformentioned code into c#.
Nicolas gave me some c# code that was supposed to do it, but it breakes
down at i_data= (WORD*)data. The code i was given is:

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);

}

If you can see the problem I'd appreciate it.
Thanks
Mike
Willy Denoyette [MVP] wrote:
"AMP" <am******@gmail.comwrote in message
news:11**********************@h48g2000cwc.googlegr oups.com...
|I was, and am still thinking that:
| i_data= (WORD*)data;
|
| converts it into a ushort.
|
Sure but with the cast you pretend that you are pointing to a WORD, that
means that you take both data[0] AND data[1] and vove it to an WORD. That
means if data[0] = 128 (0x80) and data[1] = 24 (0x18). the result i_data
hold 0x8018 after the move (that's 6272 on intel achitectures).

| Here is the c:
|
| 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 */
| }
| return (ushort)(checksum ^ 0xffff);
|
|
|
| }
|
|
| As I run this through Visual Studio, that particular statement( i_data=
| (WORD*)data;) turns i_data to 6272. But data is 128.
|

That's correct (assumed data[1] = 0x18, what did you expect?

| One more thing that may be inportant:
| There is 2 typedefs
| typedef unsigned char BYTE;
| typedef unsigned short WORD;
|
| I dont know how inportant they are. Is there a difference between a
| byte and an unsigned char?

No. the types says threat BYTE as an unsigned char in code.
But, where is the C# question in this posting, I would suggest you to post
to a C language NG like microsoft.public.dotnet.languages.vc.

Willy.
Sep 14 '06 #9

P: n/a
The C# code is exactly the equivalent of the C code, mind to explain what
problem you have with it?
Willy.

"AMP" <am******@gmail.comwrote in message
news:11**********************@h48g2000cwc.googlegr oups.com...
| Willy,
| I am trying to turn the afformentioned code into c#.
| Nicolas gave me some c# code that was supposed to do it, but it breakes
| down at i_data= (WORD*)data. The code i was given is:
|
| 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);
|
|
|
| }
|
| If you can see the problem I'd appreciate it.
| Thanks
| Mike
|
|
| Willy Denoyette [MVP] wrote:
| "AMP" <am******@gmail.comwrote in message
| news:11**********************@h48g2000cwc.googlegr oups.com...
| |I was, and am still thinking that:
| | i_data= (WORD*)data;
| |
| | converts it into a ushort.
| |
| Sure but with the cast you pretend that you are pointing to a WORD, that
| means that you take both data[0] AND data[1] and vove it to an WORD.
That
| means if data[0] = 128 (0x80) and data[1] = 24 (0x18). the result i_data
| hold 0x8018 after the move (that's 6272 on intel achitectures).
| >
| | Here is the c:
| |
| | 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 */
| | }
| | return (ushort)(checksum ^ 0xffff);
| |
| |
| |
| | }
| |
| |
| | As I run this through Visual Studio, that particular statement(
i_data=
| | (WORD*)data;) turns i_data to 6272. But data is 128.
| |
| >
| That's correct (assumed data[1] = 0x18, what did you expect?
| >
| | One more thing that may be inportant:
| | There is 2 typedefs
| | typedef unsigned char BYTE;
| | typedef unsigned short WORD;
| |
| | I dont know how inportant they are. Is there a difference between a
| | byte and an unsigned char?
| >
| No. the types says threat BYTE as an unsigned char in code.
| But, where is the C# question in this posting, I would suggest you to
post
| to a C language NG like microsoft.public.dotnet.languages.vc.
| >
| Willy.
|
Sep 15 '06 #10

P: n/a
AMP
I dont get the same final answer.
The Line:
ushort current = BitConverter.ToUInt16(data, index*2);
doesnt take into accoune transforming data[3] and data[4] because
index*2 can never be odd?
Beleave me, you guys have been a tremendous help and I do appreciate
it,but I am stuck.
FYI length is 8(this uses length/2)
data = 128,24,4,4,0,0,.........0),
Willy Denoyette [MVP] wrote:
The C# code is exactly the equivalent of the C code, mind to explain what
problem you have with it?
Willy.

"AMP" <am******@gmail.comwrote in message
news:11**********************@h48g2000cwc.googlegr oups.com...
| Willy,
| I am trying to turn the afformentioned code into c#.
| Nicolas gave me some c# code that was supposed to do it, but it breakes
| down at i_data= (WORD*)data. The code i was given is:
|
| 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);
|
|
|
| }
|
| If you can see the problem I'd appreciate it.
| Thanks
| Mike
|
|
| Willy Denoyette [MVP] wrote:
| "AMP" <am******@gmail.comwrote in message
| news:11**********************@h48g2000cwc.googlegr oups.com...
| |I was, and am still thinking that:
| | i_data= (WORD*)data;
| |
| | converts it into a ushort.
| |
| Sure but with the cast you pretend that you are pointing to a WORD, that
| means that you take both data[0] AND data[1] and vove it to an WORD.
That
| means if data[0] = 128 (0x80) and data[1] = 24 (0x18). the result i_data
| hold 0x8018 after the move (that's 6272 on intel achitectures).
| >
| | Here is the c:
| |
| | 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 */
| | }
| | return (ushort)(checksum ^ 0xffff);
| |
| |
| |
| | }
| |
| |
| | As I run this through Visual Studio, that particular statement(
i_data=
| | (WORD*)data;) turns i_data to 6272. But data is 128.
| |
| >
| That's correct (assumed data[1] = 0x18, what did you expect?
| >
| | One more thing that may be inportant:
| | There is 2 typedefs
| | typedef unsigned char BYTE;
| | typedef unsigned short WORD;
| |
| | I dont know how inportant they are. Is there a difference between a
| | byte and an unsigned char?
| >
| No. the types says threat BYTE as an unsigned char in code.
| But, where is the C# question in this posting, I would suggest you to
post
| to a C language NG like microsoft.public.dotnet.languages.vc.
| >
| Willy.
|
Sep 15 '06 #11

P: n/a

"AMP" <am******@gmail.comwrote in message
news:11**********************@i3g2000cwc.googlegro ups.com...
|I dont get the same final answer.
| The Line:
| ushort current = BitConverter.ToUInt16(data, index*2);
| doesnt take into accoune transforming data[3] and data[4] because
| index*2 can never be odd?
| Beleave me, you guys have been a tremendous help and I do appreciate
| it,but I am stuck.
| FYI length is 8(this uses length/2)
| data = 128,24,4,4,0,0,.........0),
|
|

But you don't need an odd index, array elements start at index 0. you treat
the array as holding shorts, so you need to pick the values at 0, 2, 4, 6
etc...
Following C# and C++ code (compiled into a DLL), illustrates that both
return the same value.
If you compile this code and run it from the command line you should get
58235 as return value for the given array.

// C# main
using System;
using System.Runtime.InteropServices;

class Program
{
[DllImport("checks.dll")]
extern static ushort calcChecksum(byte[] da, ushort length);
static void Main()
{
byte[] ba = {128, 24, 4, 4, 0, 0, 0, 0};
// call the C++ function
Console.WriteLine(calcChecksum(ba, (ushort)ba.Length));
// call the C# function
Console.WriteLine(CalcChecksum(ba, (ushort)ba.Length));
}
static 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);
}
}
// checks.cpp
// compile from the commandline: cl /O2 /LD checks.cpp
#define WORD unsigned short
#define BYTE unsigned char
extern "C" {
__declspec( dllexport ) WORD __stdcall calcChecksum(BYTE data[], WORD
length);
}
WORD __stdcall 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 */
}
}
Sep 15 '06 #12

P: n/a
AMP
I found the problem, and yes it was silly.
Thanks
Mike
Willy Denoyette [MVP] wrote:
"AMP" <am******@gmail.comwrote in message
news:11**********************@i3g2000cwc.googlegro ups.com...
|I dont get the same final answer.
| The Line:
| ushort current = BitConverter.ToUInt16(data, index*2);
| doesnt take into accoune transforming data[3] and data[4] because
| index*2 can never be odd?
| Beleave me, you guys have been a tremendous help and I do appreciate
| it,but I am stuck.
| FYI length is 8(this uses length/2)
| data = 128,24,4,4,0,0,.........0),
|
|

But you don't need an odd index, array elements start at index 0. you treat
the array as holding shorts, so you need to pick the values at 0, 2, 4, 6
etc...
Following C# and C++ code (compiled into a DLL), illustrates that both
return the same value.
If you compile this code and run it from the command line you should get
58235 as return value for the given array.

// C# main
using System;
using System.Runtime.InteropServices;

class Program
{
[DllImport("checks.dll")]
extern static ushort calcChecksum(byte[] da, ushort length);
static void Main()
{
byte[] ba = {128, 24, 4, 4, 0, 0, 0, 0};
// call the C++ function
Console.WriteLine(calcChecksum(ba, (ushort)ba.Length));
// call the C# function
Console.WriteLine(CalcChecksum(ba, (ushort)ba.Length));
}
static 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);
}
}
// checks.cpp
// compile from the commandline: cl /O2 /LD checks.cpp
#define WORD unsigned short
#define BYTE unsigned char
extern "C" {
__declspec( dllexport ) WORD __stdcall calcChecksum(BYTE data[], WORD
length);
}
WORD __stdcall 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 */
}
}
Sep 15 '06 #13

This discussion thread is closed

Replies have been disabled for this discussion.