Converting Datatypes

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
 
I

Ian Semmel

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.
 
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 said:
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.
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
 
K

Kevin Spencer

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 said:
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.
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
 
A

AMP

Aint helping me....
I'm trying to do everything without pointers.
Kevin said:
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 said:
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.
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
 
W

Willy Denoyette [MVP]

| 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.
 
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; /* 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
 
W

Willy Denoyette [MVP]

|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; /* 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.
 
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
 
W

Willy Denoyette [MVP]

The C# code is exactly the equivalent of the C code, mind to explain what
problem you have with it?


Willy.

| 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:
| > | > |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; /* 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.
|
 
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),

The C# code is exactly the equivalent of the C code, mind to explain what
problem you have with it?


Willy.

| 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:
| > | > |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; /* 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.
|
 
W

Willy Denoyette [MVP]

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

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