从Socket接收数据时出现乱码字节偏移 [英] garbled byte offset when receiving data from Socket

查看:554
本文介绍了从Socket接收数据时出现乱码字节偏移的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个C#程序,它通过

套接字以字节数组发送数据。


VC ++应用服务器以下列格式接收数据....


typedef struct advice

{

header sHdr;

char code [ 33];

char pn [11];

char州[4];

char szType [4];

char szTravelDate [9];

char szAmount [13];

char szMethod [7];

char CATID [16] ;

长lStan;

...

}


奇怪的部分是每一个lStan失踪后的char数组3

字符。


如果我在VC ++中执行QUICKWATCH,我将lStan的值改为1128415488。

将其转换为字节数组,产生字节{0,65,66,67} -

是lStan之后字段的前3个值。(字段在lStan之后

有''ABCDEF'')


i CANT使用任何结构,ma因为我的结构定义来自XML Schema文档,因此我认为我的结构定义来自于一个XML Schema文档。


C#中的值的转换例程

int nData = int.Parse(strTempData);

//转换为字节数组; this.bPvtData =

BitConverter.GetBytes(nData);

将3652转换为字节数组= {68,14,0,0}

当它从

套接字出来时显然正在发生这种情况......或者VC ++没有解释

''的大小long''变量(我作为Int32传递)


请帮忙!

解决方案

< blockquote>一个long是一个Int64,根据文档:

类型范围大小.NET Framework类型

长a ?? 9,223,372,036,854,775,808到9,223,372,036,854,775,807签名64位

整数System.Int64

希望有所帮助:)

On Tue,2006年3月7日15:29:33 +0800,< Ro **** *****@gmail.com>写道:

我有一个C#程序通过
套接字以字节数组发送数据。


VC ++应用程序服务器接收数据以下格式....

typedef struct advice
{/ s>标题sHdr;
char代码[33];
char pn [11];
char State [4];
char szType [4];
char szTravelDate [9];
char szAmount [13];
char szMethod [7];
char CATID [16];
long lStan;
...


奇怪的部分是lStan丢失后的每个char数组3


如果我在VC ++中执行QUICKWATCH,我将lStan的值变为1128415488。
将其转换为字节数组,产生字节{0 ,65,66,67} - 这是lStan之后该领域的前3个值。(lStan之后的字段
有'ABCDEF'')

我不能使用任何结构,编组或以其他方式在C#端,
因为我的结构定义来自XML Schema文档。

C#中的值的转换例程

int nData = int.Parse(strTempData);
/ /转换为字节数组; this.bPvtData =
BitConverter.GetBytes(nData);

这将3652转换为字节数组= {68,14,0,0}

显然有些东西当它出现在插件中时......或者VC ++没有解释
''long'变量的大小(我作为Int32传递)

请帮忙!




感谢马克...但实际上我确实考虑过这个事实并且正在使用

a Int32在C#结束,


,因为我的VC ++ long(int)是一个32位整数。


Ro*********@gmail.com 写道:

我有一个C#程序,它通过
套接字以字节数组的形式发送数据。



VC ++应用服务器以下列格式接收数据.... br />
typedef struct advice
{
标题sHdr;
char代码[33];
char pn [ [11];
char州[4];
char szTravelDate [9];
char szAmount [13];
char szMethod [ 7];
char CATID [16];
long lStan;
...


奇怪的部分是lStan之后的每个字符数组缺少3个
字符。

如果我在VC ++中执行QUICKWATCH,我将lStan的值变为1128415488。
将其转换为字节数组,产生bytes {0,65,66,67} -
是lStan之后字段的前3个值。(lStan之后的字段
有''ABCDEF'')



这听起来像CATID那样导致了问题。你好吗

传输那个?


Jon


i have a C# program that is sending data in a byte array through a
socket.

the VC++ application server receives data in teh following format....

typedef struct advice
{
header sHdr;
char Code[33];
char pn[11];
char State[4];
char szType[4];
char szTravelDate[9];
char szAmount[13];
char szMethod[7];
char CATID[16];
long lStan;
...
}

the weird part is that EVERY char array after lStan is missing 3
characters.

if i do a QUICKWATCH in VC++, i get the value of lStan as "1128415488".
converting this to a byte array , yields the bytes {0,65,66,67} - which
is the first 3 values of the field after lStan.(the field after lStan
has ''ABCDEF'')

i CANT use any structures, marshalled or otherwise at the C# end ,
because my structure definitions come out of a XML Schema document.

conversion routine for the value in C#
int nData = int.Parse(strTempData);
//convert to Byte Array; this.bPvtData =
BitConverter.GetBytes(nData);
this converts 3652 into byte array = {68,14, 0 , 0}

something is evidently happening to this when it comes out of the
Socket ... either that or VC++ is not interpreting the size of the
''long'' variable (which im passing as a Int32)

please help!

解决方案

A long is a Int64, as per documentation:
Type Range Size .NET Framework type
long a??9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 Signed 64-bit
integer System.Int64
Hope that helps :)
On Tue, 07 Mar 2006 15:29:33 +0800, <Ro*********@gmail.com> wrote:

i have a C# program that is sending data in a byte array through a
socket.

the VC++ application server receives data in teh following format....

typedef struct advice
{
header sHdr;
char Code[33];
char pn[11];
char State[4];
char szType[4];
char szTravelDate[9];
char szAmount[13];
char szMethod[7];
char CATID[16];
long lStan;
...
}

the weird part is that EVERY char array after lStan is missing 3
characters.

if i do a QUICKWATCH in VC++, i get the value of lStan as "1128415488".
converting this to a byte array , yields the bytes {0,65,66,67} - which
is the first 3 values of the field after lStan.(the field after lStan
has ''ABCDEF'')

i CANT use any structures, marshalled or otherwise at the C# end ,
because my structure definitions come out of a XML Schema document.

conversion routine for the value in C#
int nData = int.Parse(strTempData);
//convert to Byte Array; this.bPvtData =
BitConverter.GetBytes(nData);
this converts 3652 into byte array = {68,14, 0 , 0}

something is evidently happening to this when it comes out of the
Socket ... either that or VC++ is not interpreting the size of the
''long'' variable (which im passing as a Int32)

please help!




thanks a ton Mark... but actually i did consider that fact and am using
a Int32 at C# end,

because my VC++ long (int) is a 32-bit Integer.


Ro*********@gmail.com wrote:

i have a C# program that is sending data in a byte array through a
socket.

the VC++ application server receives data in teh following format....

typedef struct advice
{
header sHdr;
char Code[33];
char pn[11];
char State[4];
char szType[4];
char szTravelDate[9];
char szAmount[13];
char szMethod[7];
char CATID[16];
long lStan;
...
}

the weird part is that EVERY char array after lStan is missing 3
characters.

if i do a QUICKWATCH in VC++, i get the value of lStan as "1128415488".
converting this to a byte array , yields the bytes {0,65,66,67} - which
is the first 3 values of the field after lStan.(the field after lStan
has ''ABCDEF'')



It sounds like it''s CATID that''s causing the problem then. How are you
transmitting that?

Jon


这篇关于从Socket接收数据时出现乱码字节偏移的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆