• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 455
  • Last Modified:

C to VB6 translation or translator

I have C code that I need to translate to VB6.  Most I understand and have done.  I have no experience with tables, constants.  and little with arrays.  Is there a translator available (freeware/ shareware) that would do the job.  The program is about 75 pages long, I'd rather not do it by hand.  Here is a portion I need specific help on.

const char* CLARKE_1866 = "CC";
const char* CLARKE_1880 = "CD";
const char* BESSEL_1841 = "BR";
const char* BESSEL_1841_NAMIBIA = "BN";


typedef struct Latitude_Band_Value
{
  long letter;            /* letter representing latitude band  */
  double min_northing;    /* minimum northing for latitude band */      
  double north;           /* upper latitude for latitude band   */
  double south;           /* lower latitude for latitude band   */
} Latitude_Band;

static const Latitude_Band Latitude_Band_Table[20] =
  {{LETTER_C, 1100000.0, -72.0, -80.5},
  {LETTER_D, 2000000.0, -64.0, -72.0},
  {LETTER_E, 2800000.0, -56.0, -64.0},
  {LETTER_F, 3700000.0, -48.0, -56.0},
  {LETTER_G, 4600000.0, -40.0, -48.0},
  {LETTER_H, 5500000.0, -32.0, -40.0},
  {LETTER_J, 6400000.0, -24.0, -32.0},
  {LETTER_K, 7300000.0, -16.0, -24.0},
  {LETTER_L, 8200000.0, -8.0, -16.0},
  {LETTER_M, 9100000.0, 0.0, -8.0},
  {LETTER_N, 0.0, 8.0, 0.0},
  {LETTER_P, 800000.0, 16.0, 8.0},
  {LETTER_Q, 1700000.0, 24.0, 16.0},
  {LETTER_R, 2600000.0, 32.0, 24.0},
  {LETTER_S, 3500000.0, 40.0, 32.0},
  {LETTER_T, 4400000.0, 48.0, 40.0},
  {LETTER_U, 5300000.0, 56.0, 48.0},
  {LETTER_V, 6200000.0, 64.0, 56.0},
  {LETTER_W, 7000000.0, 72.0, 64.0},
  {LETTER_X, 7900000.0, 84.5, 72.0}};

 
typedef struct UPS_Constant_Value
{
  long letter;            /* letter representing latitude band      */
  long ltr2_low_value;    /* 2nd letter range - high number         */
  long ltr2_high_value;   /* 2nd letter range - low number          */
  long ltr3_high_value;   /* 3rd letter range - high number (UPS)   */
  double false_easting;   /* False easting based on 2nd letter      */
  double false_northing;  /* False northing based on 3rd letter     */
} UPS_Constant;

static const UPS_Constant UPS_Constant_Table[4] =
  {{LETTER_A, LETTER_J, LETTER_Z, LETTER_Z, 800000.0, 800000.0},
  {LETTER_B, LETTER_A, LETTER_R, LETTER_Z, 2000000.0, 800000.0},
  {LETTER_Y, LETTER_J, LETTER_Z, LETTER_P, 800000.0, 1300000.0},
  {LETTER_Z, LETTER_A, LETTER_J, LETTER_P, 2000000.0, 1300000.0}};


Please point me to a translator, or if one isn't available (not commercial) let me know how to deal with static const tables.
Thanks,
Russ
0
RUSS_EMI
Asked:
RUSS_EMI
  • 5
  • 4
1 Solution
 
ArkCommented:
//C++ code
const char* CLARKE_1866 = "CC"
'VB code
Const CLARKE_1866 = "CC"

//C++ code
typedef struct Latitude_Band_Value
{
  long letter;            /* letter representing latitude band  */
  double min_northing;    /* minimum northing for latitude band */      
  double north;           /* upper latitude for latitude band   */
  double south;           /* lower latitude for latitude band   */
} Latitude_Band;

'VB code
Type Latitude_Band
     letter As Long
     min_northing As Double
     north As Double
     south As Double
End Type

//C++ code
static const Latitude_Band Latitude_Band_Table[20] =
  {{LETTER_C, 1100000.0, -72.0, -80.5},
  {LETTER_D, 2000000.0, -64.0, -72.0},
//etc.

'VB Code
'C++ allow set values at same time when you dim variable, VB - not.
Public Latitude_Band_Table(20) As Latitude_Band
'Somwhere in code:
With Latitude_Band_Table(0)
     letter = LETTER_C 'I think LETTER_C is a constant defined somewhere in C++ code
     min_northing = 1100000.0
     north = -72.0
     south = -80.5
End With

With Latitude_Band_Table(1)
     letter = LETTER_D 'I think LETTER_D is a constant defined somewhere in C++ code
     min_northing = 2000000.0
     north = -64.0
     south = -72.0
End With

'Same with UPS constants.

'AFAIK, there aren't any C/VB translators.
'If you need more help with reference-ellipsoid calculations, let me know
0
 
RUSS_EMIAuthor Commented:
Thank you.  Is there anything to help do the many pages of code, or am I stuck doing it by hand.
What I am trying to do is have a routine that will convert from MGRS to Lat/Long degree decimal using VB.
Russ
0
 
ArkCommented:
Which precisson do you need? Also, do you really need all these ancient ellipsoids like Bessel/Clark etc? Probably WGS-84 is enough?
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
RUSS_EMIAuthor Commented:
It varies 3 - 5.  WGS-84 is all in need, just scraped it for sample, not going to be included.
The end user will input 38SMB123456 -> 38SMB1234567890.
Got any routines already, to save time?
Thanks,
Russ
0
 
ArkCommented:
PS
Here is my app to prepare tests for cadets (I'm a lecturer of Navigation at Marine University) to convert WGS-84 coordinates into special ellipsoid (chart based on) coordinates. Each ellipsoid determined with dX, dY, dZ (center difference between ellipsoid and WGS-84), a (major semiaxis) and dF (latitude factor). All this data available from numerous literature (Brown's nautical almanac, for example).

VERSION 5.00
Begin VB.Form Form1
   Caption         =   "Form1"
   ClientHeight    =   2730
   ClientLeft      =   60
   ClientTop       =   345
   ClientWidth     =   3945
   LinkTopic       =   "Form1"
   ScaleHeight     =   2730
   ScaleWidth      =   3945
   StartUpPosition =   3  'Windows Default
   Begin VB.CommandButton Command1
      Caption         =   "Command1"
      Height          =   375
      Left            =   2040
      TabIndex        =   8
      Top             =   1920
      Width           =   1695
   End
   Begin VB.Frame Frame1
      Caption         =   "Frame1"
      Height          =   915
      Left            =   180
      TabIndex        =   5
      Top             =   1680
      Width           =   3615
      Begin VB.TextBox Text3
         Height          =   315
         Left            =   1080
         TabIndex        =   6
         Text            =   "Text3"
         Top             =   300
         Width           =   495
      End
      Begin VB.Label Label2
         Caption         =   "Label2"
         Height          =   315
         Left            =   120
         TabIndex        =   7
         Top             =   300
         Width           =   915
      End
   End
   Begin VB.CommandButton Command2
      Caption         =   "Command2"
      Height          =   315
      Left            =   180
      TabIndex        =   4
      Top             =   1260
      Width           =   1635
   End
   Begin VB.TextBox Text2
      Alignment       =   1  'Right Justify
      Height          =   285
      Left            =   180
      TabIndex        =   2
      Text            =   "Text2"
      Top             =   540
      Width           =   1635
   End
   Begin VB.TextBox Text1
      Alignment       =   1  'Right Justify
      Height          =   315
      Left            =   180
      TabIndex        =   1
      Text            =   "Text1"
      Top             =   120
      Width           =   1635
   End
   Begin VB.ComboBox Combo1
      Height          =   315
      Left            =   180
      Style           =   2  'Dropdown List
      TabIndex        =   0
      Top             =   900
      Width           =   1635
   End
   Begin VB.Label Label1
      BorderStyle     =   1  'Fixed Single
      Caption         =   "Label1"
      Height          =   1035
      Left            =   1920
      TabIndex        =   3
      Top             =   120
      Width           =   1815
   End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Private Enum Ell_Name
   Jakarta
   HongKong
   Tokio
   NAD
   NZGD
   Luson
End Enum

Private Type Position
   Lat As Double
   Lon As Double
End Type

Private Type Ell_Data
    dx As Double
    dy As Double
    dz As Double
    a As Double
    dF As Double
End Type
Dim sEllName As Variant
Dim ed As Ell_Data
Dim pos As Position

Private Sub Command1_Click()
   Dim DeltaF As Double, DeltaL As Double
   Dim nEll As Integer
   Dim i As Long
   Dim sVar As String, sResult As String, sVarPrefix As String, sVarSuffix As String
   sResult = Space(35) & "Coordinates conversion" & vbCrLf & vbCrLf
   sResult = sResult & String(82, "-") & vbCrLf
   sResult = sResult & "№ |             TESTS                 ||                   ANSWERS                "
   sResult = sResult & vbCrLf & "  |" & String(79, "-") & vbCrLf
   sResult = sResult & "в.|Geodet.syst.|    F84   |    L84    ||   dF    |   dL    |    Fk    |    Lk     "
   sResult = sResult & vbCrLf & String(82, "=") & vbCrLf
   sVarPrefix = "   Coordinates conversion    " & vbCrLf & "Test No: "
   sVarSuffix = String(42, "-") & vbCrLf & "dF' = ? dL' = ? Fk = ? Lk = ?" & vbCrLf & String(42, "-")
   For i = 1 To Val(Text3)
       nEll = GetRandom(0, 5)
       ed = GetElData(nEll)
       pos.Lat = (GetRandom(0, 180 * 60 * 1000&) - 90 * 60 * 1000&) / 60000
       pos.Lon = (GetRandom(0, 360 * 60 * 1000&) - 180 * 60 * 1000&) / 60000
       DeltaF = dF(pos.Lat, pos.Lon, ed)
       DeltaL = dL(pos.Lat, pos.Lon, ed)
       sResult = sResult & Format(i, "00") & "|" & Format(sEllName(nEll), "@@@@@@@@@@@@!") & "|"
       sResult = sResult & FormatCoord(pos.Lat) & "|" & FormatCoord(pos.Lon, True) & "||"
       sResult = sResult & Format(DeltaF, "+0.000000;-0.000000") & "|" & Format(DeltaL, "+0.000000;-0.000000") & "|"
       sResult = sResult & FormatCoord(pos.Lat + DeltaF / 60) & "|" & FormatCoord(pos.Lon + DeltaL / 60, True) & vbCrLf
       sVar = sVar & sVarPrefix & i & vbCrLf & "Geodetical chart system: " & sEllName(nEll) & vbCrLf
       sVar = sVar & "F84 = " & FormatCoord(pos.Lat) & "; L84 = " & FormatCoord(pos.Lon, True)
       sVar = sVar & vbCrLf & sVarSuffix & vbCrLf
   Next i
   Open "Result.txt" For Output As #1
       Print #1, sResult
   Close #1
   Open "Var.txt" For Output As #1
       Print #1, sVar
   Close #1
End Sub

Private Function dF(phi As Double, lambda As Double, ed As Ell_Data) As Double
    dF = (3438 / 6378137) * (ed.dx * Sind(phi) * Cosd(lambda) + ed.dy * Sind(phi) * Sind(lambda) - ed.dz * Cosd(phi) - ed.a * ed.dF * Sind(2 * phi) * 0.0001)
End Function

Private Function dL(phi As Double, lambda As Double, ed As Ell_Data) As Double
    dL = (3438 / 6378137 / Cosd(phi)) * (ed.dx * Sind(lambda) - ed.dy * Cosd(lambda))
End Function


Public Function Rad(ByVal X As Double) As Double
  Rad = X * Atn(1) / 45#
End Function

Public Function Deg(ByVal X As Double) As Double
  Deg = X * 45# / Atn(1)
End Function

'Helpfull functions to compute sin, cos, tan
'with argument in degrees
Public Function Sind(ByVal X As Double) As Double
   Sind = Sin(Rad(X))
End Function

Public Function Cosd(ByVal X As Double) As Double
   Cosd = Cos(Rad(X))
End Function

Private Function GetElData(ByVal en As Ell_Name) As Ell_Data
    Select Case en
        Case Jakarta
             GetElData.dx = 377
             GetElData.dy = -681
             GetElData.dz = 50
             GetElData.a = 6377397.155
             GetElData.dF = -0.100374
        Case HongKong
             GetElData.dx = 156
             GetElData.dy = 271
             GetElData.dz = 189
             GetElData.a = 6378388
             GetElData.dF = 0.141922702
        Case Tokio
             GetElData.dx = 148
             GetElData.dy = -507
             GetElData.dz = -685
             GetElData.a = 6377397.155
             GetElData.dF = -0.10037483
        Case NAD
             GetElData.dx = 8
             GetElData.dy = -160
             GetElData.dz = -176
             GetElData.a = 6378206.4
             GetElData.dF = 0.37264639
        Case NZGD
             GetElData.dx = -84
             GetElData.dy = 22
             GetElData.dz = -209
             GetElData.a = 6378388
             GetElData.dF = 0.14192702
        Case Luson
             GetElData.dx = 133
             GetElData.dy = 77
             GetElData.dz = 51
             GetElData.a = 6378206.4
             GetElData.dF = 0.37264639
    End Select
End Function

Private Function GetRandom(min As Long, max As Long) As Long
   GetRandom = Int((max - min + 1) * Rnd + min)
End Function

Public Function FormatCoord(ByVal X As Double, Optional bLon As Boolean = False) As String
   Dim sSgn As String, sFmt As String
   sFmt = String(Abs(bLon) + 2, "000")
   If X < 0 Then
      If bLon Then sSgn = "W" Else sSgn = "S"
   Else
      If bLon Then sSgn = "E" Else sSgn = "N"
   End If
   X = Abs(X)
   FormatCoord = Format(Int(X), sFmt) & "-" & Format((X - Int(X)) * 60, "00.000") & sSgn
End Function

Private Sub Command2_Click()
   Dim sTemp As String, iSign As Integer, iSep As Integer
   sTemp = Text1.Text
   iSign = IIf(Right(sTemp, 1) = "N", 1, -1)
   iSep = InStr(1, sTemp, "-")
   pos.Lat = Left(sTemp, iSep - 1) + Mid(sTemp, iSep + 1, Len(sTemp) - iSep - 1) / 60
   pos.Lat = pos.Lat * iSign
   sTemp = Text2.Text
   iSign = IIf(Right(sTemp, 1) = "E", 1, -1)
   iSep = InStr(1, sTemp, "-")
   pos.Lon = Left(sTemp, iSep - 1) + Mid(sTemp, iSep + 1, Len(sTemp) - iSep - 1) / 60
   pos.Lon = pos.Lon * iSign
   Dim DeltaF As Double, DeltaL As Double
   Dim sLabel As String
   DeltaF = dF(pos.Lat, pos.Lon, GetElData(Combo1.ListIndex))
   DeltaL = dL(pos.Lat, pos.Lon, GetElData(Combo1.ListIndex))
   sLabel = "dF = " & Format(DeltaF, "+0.000000;-0.000000") & vbCrLf & "dL = " & Format(DeltaL, "+0.000000;-0.000000") & vbCrLf
   sLabel = sLabel & vbCrLf & "F = " & FormatCoord(pos.Lat + DeltaF / 60) & vbCrLf & "L = " & FormatCoord(pos.Lon + DeltaL / 60, True)
   Label1 = sLabel
End Sub

Private Sub Form_Load()
  sEllName = Array("Jakarta", "H_Kong 1963", "Tokyo", "NAD 1927", "NZGD 1949", "Louson")
  Text1 = "00-00.000N"
  Text2 = "000-00.000E"
  For i = 0 To 5
      Combo1.AddItem sEllName(i)
  Next i
  Combo1.ListIndex = 0
  Frame1.Caption = "Random"
  Label2.Caption = "Num tests:"
  Command1.Caption = "Calculate"
  Command2.Caption = "Calculate"
  Text3 = 25
End Sub

Regards
Ark
0
 
ArkCommented:
Oops, sorry, posted at same time with you, seems you don't need this sample. Unfortunatelly, I didn't work with MGRS yet. I'll try to look what I can do with it.
0
 
RUSS_EMIAuthor Commented:
Thank you.  This will be used where I'm stationed 38SMB 45956 86195.  
Russ
0
 
ArkCommented:
OK, I have a look on MGRS system. Actually, conversion is simple, though it require a lot of routine job applying al those constants for different letters (latitude/UPS tables in your sample). My suggestions:
You have 2 ways to accomplish your task:
1. Rebuild C++ code to make standard/activeX dll which can be call from VB.
2. Rebuild this all in VB.

To simplify 2nd way I recommend load all [table] initialization into text editor (Word/Notepad), remove brackets in batch (Replace "{" and "}" with "") and save as CSV (comma-separated) file. Then in Form_Load/Sub Main load all this data into array of UDTs, smth like this:
Public Latitude_Band_Table(20) As Latitude_Band
'Somwhere in code:
For i = 0 to 20
     With Latitude_Band_Table(i)
          Input #1, letter
          Input #1, min_northing
          Input #1, north
          Input #1, south
     End with
End With
Next i

If you send me source at ark@msun.ru I can try
0
 
RUSS_EMIAuthor Commented:
I tried to send all source code, but it was too much.  I'll trim it down and send it later today.
Russ
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now