Converting VB.NET to Visual Cobol

I'm converting this VB.NET code to Visual Cobol but I'm
with difficulty in the declaration: declare a() as binary-char unsigned occurs any = type Encoding::UTF8::GetBytes(SourceText)

VB.NET code running

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim SourceText As String = "1234567890"
    Dim a() As Byte
    a = Encoding.UTF8.GetBytes(SourceText)
    Dim b As Byte()
    Dim c As New SHA512Managed
    b = c.ComputeHash(a)
    Dim d As String = Convert.ToBase64String(b)
End Sub

Visual Cobol code with error.

 method-id button1_Click final private.
 procedure division using by value sender as object e as type System.EventArgs.
     declare sourceText as string = "1234567890"
     declare a() as binary-char unsigned occurs any = type Encoding::UTF8::GetBytes(SourceText)
     declare b as binary-char unsigned occurs any
     declare c as type SHA512Managed
     set b to c::ComputeHash(a)           
     declare d as string = type Convert::ToBase64String(b)         
     set tbxOut::Text to d       
 end method.

Any suggestion?

  • Verified Answer

    You get a NullReferenceException because c is not initialized. Try this:

    declare c as type SHA512Managed = new SHA512Managed

    Freundliche Grüsse

    Werner Lanter

  • Verified Answer

    ...and to answer the original question, the syntax:

        declare a() as binary-char unsigned occurs any = type Encoding::UTF8::GetBytes(SourceText)

    ...is not quite right.  Should be:

        declare a as binary-char unsigned occurs any = type Encoding::UTF8::GetBytes(SourceText)

    (i.e. same thing without the parentheses following 'a').

    In fact, for this and similar declare statements it is not necessary to define the type explicitly as the compiler is able to infer the type from the value expression, so the following is equally good:

        declare a = type Encoding::UTF8::GetBytes(SourceText)

    This is similar to the use of 'var' in C# and it is of course a matter of opinion as to whether it's better to state the type explicitly.

  • Thanks for the tip. I got the solution with the following code.

    1 $set ilusing"System.Security"

    2 $set ilusing"System.Security.Cryptography"      

    3 class-id PasswordSHA.Form1 is partial

    4           inherits type System.Windows.Forms.Form.

    5

    6 working-storage section.

    7 1 wsHex          string.

    8 1 wsByteArray  binary-char unsigned occurs any. *> Array Byte

    9 1 wsHash       binary-char unsigned occurs any. *> Array Byte

    10 1 wsPassword   pic x(20).      

    11

    12 method-id NEW.

    13 procedure division.

    14     invoke self::InitializeComponent

    15     goback.

    16 end method.

    17

    18 *-----------------------------------------------------------------

    19 *------------------ SHA1 -----------------------------------------

    20 *-----------------------------------------------------------------

    21 method-id button1_Click final private.

    22 procedure division using by value sender as object e as type System.EventArgs.

    23     try

    24       declare mySHA1 SHA1Managed = new type SHA1Managed

    25       set wsPassword to tbxIn::Text

    26       set wsByteArray to wsPassword

    27       set wsHash to mySHA1::ComputeHash(wsByteArray)

    28       perform Create-string-hex

    29       set tbxOut::Text to wsHex

    30     catch ex as type Exception

    31          invoke type MessageBox::Show("Erro: " & ex::Message)

    32     end-try    

    33

    34     goback.

    35

    36 Create-string-hex section.      

    37     set wsHex to ""

    38     perform varying y as binary-char unsigned through wsHash

    39         set wsHex to wsHex & String::Format("{0:x2}", y)

    40     end-perform      

    41 end method.

    42

    43 *-----------------------------------------------------------------

    44 *------------------ SHA512 ---------------------------------------

    45 *-----------------------------------------------------------------

    46 method-id button2_Click final private.

    47 procedure division using by value sender as object e as type System.EventArgs.

    48 //////////////////////////////////////////////////////////////////

    49 /////////////////// Função em C# /////////////////////////////////  

    50 //////////////////////////////////////////////////////////////////

    51  

    52 /  private static string GetSHA512(string text)

    53 /  {

    54 /      UnicodeEncoding UE = new UnicodeEncoding();

    55 /      byte[] hashValue;

    56 /      byte[] message = UE.GetBytes(text);

    57 /      

    58 /      SHA512Managed hashString = new SHA512Managed();

    59 /      string hex = "";

    60 /

    61 /      hashValue = hashString.ComputeHash(message);

    62 /      foreach (byte x in hashValue)

    63 /      {

    64 /          hex = String.Format("{0:x2}", x);

    65 /      }

    66 /      return hex;

    67 /  }      

    68

    69 //////////////////////////////////////////////////////////////////

    70 /////////// Convertida de C# para Visual Cobol ///////////////////  

    71 //////////////////////////////////////////////////////////////////

    72

    73     try

    74       set wsPassword  to tbxIn::Text

    75       declare UE UnicodeEncoding = new UnicodeEncoding

    76       declare hashValue as binary-char unsigned occurs any

    77       declare myMessage = UE::GetBytes(wsPassword)

    78       declare hashString SHA512Managed = new SHA512Managed

    79       set hashValue to hashString::ComputeHash(myMessage)

    80       set wsHex to ""

    81       perform varying y as binary-char unsigned through hashValue

    82           set wsHex to wsHex & String::Format("{0:x2}", y)

    83       end-perform

    84     catch ex as type Exception

    85          invoke type MessageBox::Show("Erro: " & ex::Message)

    86     end-try  

    87     set tbxOut::Text to wsHex    

    88 end method.      

    89

    90 end class.

  • Thanks for the tip. I got the solution with the following code.

    1 $set ilusing"System.Security"

    2 $set ilusing"System.Security.Cryptography"      

    3 class-id PasswordSHA.Form1 is partial

    4           inherits type System.Windows.Forms.Form.

    5

    6 working-storage section.

    7 1 wsHex          string.

    8 1 wsByteArray  binary-char unsigned occurs any. *> Array Byte

    9 1 wsHash       binary-char unsigned occurs any. *> Array Byte

    10 1 wsPassword   pic x(20).      

    11

    12 method-id NEW.

    13 procedure division.

    14     invoke self::InitializeComponent

    15     goback.

    16 end method.

    17

    18 *-----------------------------------------------------------------

    19 *------------------ SHA1 -----------------------------------------

    20 *-----------------------------------------------------------------

    21 method-id button1_Click final private.

    22 procedure division using by value sender as object e as type System.EventArgs.

    23     try

    24       declare mySHA1 SHA1Managed = new type SHA1Managed

    25       set wsPassword to tbxIn::Text

    26       set wsByteArray to wsPassword

    27       set wsHash to mySHA1::ComputeHash(wsByteArray)

    28       perform Create-string-hex

    29       set tbxOut::Text to wsHex

    30     catch ex as type Exception

    31          invoke type MessageBox::Show("Erro: " & ex::Message)

    32     end-try    

    33

    34     goback.

    35

    36 Create-string-hex section.      

    37     set wsHex to ""

    38     perform varying y as binary-char unsigned through wsHash

    39         set wsHex to wsHex & String::Format("{0:x2}", y)

    40     end-perform      

    41 end method.

    42

    43 *-----------------------------------------------------------------

    44 *------------------ SHA512 ---------------------------------------

    45 *-----------------------------------------------------------------

    46 method-id button2_Click final private.

    47 procedure division using by value sender as object e as type System.EventArgs.

    48 //////////////////////////////////////////////////////////////////

    49 /////////////////// Função em C# /////////////////////////////////  

    50 //////////////////////////////////////////////////////////////////

    51  

    52 /  private static string GetSHA512(string text)

    53 /  {

    54 /      UnicodeEncoding UE = new UnicodeEncoding();

    55 /      byte[] hashValue;

    56 /      byte[] message = UE.GetBytes(text);

    57 /      

    58 /      SHA512Managed hashString = new SHA512Managed();

    59 /      string hex = "";

    60 /

    61 /      hashValue = hashString.ComputeHash(message);

    62 /      foreach (byte x in hashValue)

    63 /      {

    64 /          hex = String.Format("{0:x2}", x);

    65 /      }

    66 /      return hex;

    67 /  }      

    68

    69 //////////////////////////////////////////////////////////////////

    70 /////////// Convertida de C# para Visual Cobol ///////////////////  

    71 //////////////////////////////////////////////////////////////////

    72

    73     try

    74       set wsPassword  to tbxIn::Text

    75       declare UE UnicodeEncoding = new UnicodeEncoding

    76       declare hashValue as binary-char unsigned occurs any

    77       declare myMessage = UE::GetBytes(wsPassword)

    78       declare hashString SHA512Managed = new SHA512Managed

    79       set hashValue to hashString::ComputeHash(myMessage)

    80       set wsHex to ""

    81       perform varying y as binary-char unsigned through hashValue

    82           set wsHex to wsHex & String::Format("{0:x2}", y)

    83       end-perform

    84     catch ex as type Exception

    85          invoke type MessageBox::Show("Erro: " & ex::Message)

    86     end-try  

    87     set tbxOut::Text to wsHex    

    88 end method.      

    89

    90 end class.

  • Thanks for the tip. I got the solution with the following code.

    1 $set ilusing"System.Security"

    2 $set ilusing"System.Security.Cryptography"      

    3 class-id PasswordSHA.Form1 is partial

    4           inherits type System.Windows.Forms.Form.

    5

    6 working-storage section.

    7 1 wsHex          string.

    8 1 wsByteArray  binary-char unsigned occurs any. *> Array Byte

    9 1 wsHash       binary-char unsigned occurs any. *> Array Byte

    10 1 wsPassword   pic x(20).      

    11

    12 method-id NEW.

    13 procedure division.

    14     invoke self::InitializeComponent

    15     goback.

    16 end method.

    17

    18 *-----------------------------------------------------------------

    19 *------------------ SHA1 -----------------------------------------

    20 *-----------------------------------------------------------------

    21 method-id button1_Click final private.

    22 procedure division using by value sender as object e as type System.EventArgs.

    23     try

    24       declare mySHA1 SHA1Managed = new type SHA1Managed

    25       set wsPassword to tbxIn::Text

    26       set wsByteArray to wsPassword

    27       set wsHash to mySHA1::ComputeHash(wsByteArray)

    28       perform Create-string-hex

    29       set tbxOut::Text to wsHex

    30     catch ex as type Exception

    31          invoke type MessageBox::Show("Erro: " & ex::Message)

    32     end-try    

    33

    34     goback.

    35

    36 Create-string-hex section.      

    37     set wsHex to ""

    38     perform varying y as binary-char unsigned through wsHash

    39         set wsHex to wsHex & String::Format("{0:x2}", y)

    40     end-perform      

    41 end method.

    42

    43 *-----------------------------------------------------------------

    44 *------------------ SHA512 ---------------------------------------

    45 *-----------------------------------------------------------------

    46 method-id button2_Click final private.

    47 procedure division using by value sender as object e as type System.EventArgs.

    48 //////////////////////////////////////////////////////////////////

    49 /////////////////// Função em C# /////////////////////////////////  

    50 //////////////////////////////////////////////////////////////////

    51  

    52 /  private static string GetSHA512(string text)

    53 /  {

    54 /      UnicodeEncoding UE = new UnicodeEncoding();

    55 /      byte[] hashValue;

    56 /      byte[] message = UE.GetBytes(text);

    57 /      

    58 /      SHA512Managed hashString = new SHA512Managed();

    59 /      string hex = "";

    60 /

    61 /      hashValue = hashString.ComputeHash(message);

    62 /      foreach (byte x in hashValue)

    63 /      {

    64 /          hex = String.Format("{0:x2}", x);

    65 /      }

    66 /      return hex;

    67 /  }      

    68

    69 //////////////////////////////////////////////////////////////////

    70 /////////// Convertida de C# para Visual Cobol ///////////////////  

    71 //////////////////////////////////////////////////////////////////

    72

    73     try

    74       set wsPassword  to tbxIn::Text

    75       declare UE UnicodeEncoding = new UnicodeEncoding

    76       declare hashValue as binary-char unsigned occurs any

    77       declare myMessage = UE::GetBytes(wsPassword)

    78       declare hashString SHA512Managed = new SHA512Managed

    79       set hashValue to hashString::ComputeHash(myMessage)

    80       set wsHex to ""

    81       perform varying y as binary-char unsigned through hashValue

    82           set wsHex to wsHex & String::Format("{0:x2}", y)

    83       end-perform

    84     catch ex as type Exception

    85          invoke type MessageBox::Show("Erro: " & ex::Message)

    86     end-try  

    87     set tbxOut::Text to wsHex    

    88 end method.      

    89

    90 end class.