I have a function that I wrote to convert from EBCDIC to ASCII. Please see below. I'm having a problem loading a string into an array in order to convert each character.
When I try to load the array I get the following error:
'Unable to cast object of type "System.String of type System.String[ ]"
Why can't I load the array by using:
InputArray = FieldContent
Code below:
Function EBCDIC2ASCII(ByVal FieldContent)
Dim EBCDICCodePage As Encoding = Encoding.GetEncoding("IBM037")
Dim CharIndex As Integer
Dim StrLength As Integer
Dim InputString As String
InputString = FieldContent
StrLength = InputString.Length
Dim InputArray(StrLength) As String
Dim OutputArray(StrLength) As String
InputArray = FieldContent
For CharIndex = 0 To 7
OutputArray(CharIndex) = EBCDICCodePage.Equals(InputArray(CharIndex))
Next
Return OutputArray
End Function

Load an Array with a string & convert EBCDIC to ASCII
Rudi De Vos
I solved my Array issue. But I'm having problems getting my EBCDIC to ASCII Encoding to work. Please see code to below:
Function EBCDIC2ASCII(ByVal FieldContent)
Dim EBCDICCodePage As Encoding = Encoding.GetEncoding("IBM037")
Dim CharIndex As Integer
Dim StrLength As Integer
Dim InputString As String = FieldContent
Dim EBCDIC_Char As String
Dim OutputChar As String
StrLength = InputString.Length
Dim InputArray() As Char = InputString.ToCharArray
Dim OutputArray() As Char
For CharIndex = 0 To 7
EBCDIC_Char = InputArray(CharIndex)
OutputChar = EBCDICCodePage.Equals(EBCDIC_Char).ToString
Next
Return OutputArray
End Function
I was expecting the above code to return the converted ASCII character. Instead I received a "Fales" result.
What am I doing wrong here.
Alfredo Mendez MSFT
You are setting the OutputChar to the result of the Equals() method, which returns a boolean value.
Try using the Convert() method instead. Also, check the things that spotty pointed out... either you've left out some code when you posted, or your subroutine is incomplete.
Wendy_B
Is fieldcontent a string that is being passed. It is is then you need to specify the ptype of the parameter.
Is the array meant to be an array of strings or an array of characters that is the length of the string. in which case you would be converting a string to an array of chars
Now the following is an example that simply takes a string and converts it to a byte array and writes a duplicate copy of the byte array and returns this copy array. Its not an efficient way of doing the action but shows the general idea of string to byte array and working with individual bytes in the array to create an output array.
I think this will help you understand the data typing issue you encountering.
Example
Function StringToByteArray(ByVal FieldContent As String) As Char()
Dim i As Integer = FieldContent.Length
Dim InputArray(i) As Char
Dim OutputArray(i) As Char
InputArray = FieldContent
'//Simply Doing a byte by byte copy of the character arrays
Dim x As Integer = 0
For x = 0 To i - 1
OutputArray(x) = InputArray(x)
Next
Return OutputArray
End Function
Kim Madsen
A couple of items
declare you type on the following line, what type is field content - is it a string
Function EBCDIC2ASCII(ByVal FieldContent)
what is you expected return type You need to specify something like this.
Function EBCDIC2ASCII(ByVal FieldContent as string ) as char()
Then
you are never setting OutputArray. Within the loop you are using the OutputChar but you never actually set any of the OutputArray so it will still be nothing.
Is OutputChar the character you want to write into the array.