将C转换为vb.net问题 [英] translating C to vb.net problem
本文介绍了将C转换为vb.net问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
好的,我用C语言编写了这段代码,但我想在vb.net中做同样的功能.我尝试了许多变体,但没有人能正常工作.
ok i got this code write in C and i want to make a the same function but in vb.net. I''ve tryed many variants but no one work properly.
float CMapAndreas::FindZ_For2DCoord(float X, float Y)
{
if(X < -3000.0f || X > 3000.0f || Y > 3000.0f || Y < -3000.0f) return 0.0f;
unsigned short *m_pPointData;
int iGridX = ((int)X) + 3000;
int iGridY = (((int)Y) - 3000) * -1;
int iDataPos;
FILE *mapFile;
m_pPointData = (unsigned short *)calloc(1,sizeof(unsigned short));
mapFile = fopen(MAP_ANDREAS_HMAP_FILE_FULL,"rb");
iDataPos = (iGridY * 6000) + iGridX;
fseek(mapFile, iDataPos * sizeof(unsigned short), SEEK_SET);
fread(&m_pPointData[0], 1, sizeof(unsigned short), mapFile);
return (float)m_pPointData[0] / 100.0f;
}
Public Function GetZFromXY(ByVal X As Single, ByVal Y As Single) As Single
If Not (-3000.0 < X < 3000.0 Or -3000.0 < Y < 3000.0) Then Return 0.0
Dim Z As UShort, Reader As New IO.BinaryReader(New IO.FileStream(My.Application.Info.DirectoryPath & "\SAfull.hmap", IO.FileMode.Open))
Dim iGridX As Integer = (CInt(X)) + 3000
Dim iGridY As Integer = ((CInt(Y)) - 3000) * -1
Dim iDataPos As Integer = (iGridY * 6000) + iGridX
Reader.BaseStream.Seek(iDataPos * 16, IO.SeekOrigin.Begin)
Z = Reader.ReadUInt16()
Reader.Close()
Return Z / 100
End Function
有人可以帮我吗?
Anyone can help me with this?
推荐答案
我首先看到的是:不要将System.IO.FileStream
用作BinaryReader
构造函数的参数,请使用:System.IO.File.Open(fileName, FileMode.Open)
.请参见以下代码示例: http://msdn.microsoft.com/zh-CN /library/system.io.binaryreader.aspx#Y2689 [ ^ ].
第二件事:Z
被声明为System.UInt32
,并且您需要float
.除以100之前,请强制类型转换Z
以浮点:
First thing I see: don''t useSystem.IO.FileStream
for a parameter toBinaryReader
constructor, use instead:System.IO.File.Open(fileName, FileMode.Open)
. See this code sample: http://msdn.microsoft.com/en-us/library/system.io.binaryreader.aspx#Y2689[^].
Second thing:Z
is declared asSystem.UInt32
and you needfloat
. Before dividing by 100, typecastZ
to float:
Return CSng(Z)/100
否则,您将失去小数部分,如果Z < 100
,您将得到0,这是您不期望的.
Otherwise you will loose fractional part, if Z < 100
you will get 0, something you don''t expect.
Public Function GetZFromXY(ByVal X As Single, ByVal Y As Single) As Single
If Not (-3000.0 < X < 3000.0 Or -3000.0 < Y < 3000.0) Then Return 0.0
Dim Z As UShort, Reader As New IO.BinaryReader(IO.File.Open(My.Application.Info.DirectoryPath & "\SAfull.hmap", IO.FileMode.Open))
Dim iGridX As Integer = (CInt(X)) + 3000
Dim iGridY As Integer = ((CInt(Y)) - 3000) * -1
Dim iDataPos As Integer = (iGridY * 6000) + iGridX
Reader.BaseStream.Seek(iDataPos * 2, IO.SeekOrigin.Begin)
Z = Reader.ReadUInt16()
Reader.Close()
Return CSng(Z) / 100
End Function
这篇关于将C转换为vb.net问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文