Created On: 09 November 2011
Problem:
Customer has a Visual COBOL managed console application in Visual Studio that looks like the following:
When the program invokes the MessageBox::Show method using the PIC N(2) field it does not display the Chinese characters. Why is this?
When the program invokes the MessageBox::Show method using the PIC N(2) field it does not display the Chinese characters. Why is this?
$set ilusing"System.Windows.Forms"
program-id. Program1 as "testchinese.Program1".
data division.
working-storage section.
01 chinesetext
type Byte[] value x"E58F91E7A5A8".
01 wsEncoding type Encoding.
01 codePageValues type Byte[].
01 unicodeValues string.
01 b type Byte.
01 unicodeString string.
01 enumerator type System.Globalization.TextElementEnumerator.
01 s string.
01 i binary-long.
01 c type Char.
01 any-key pic x.
01 final-string pic n(2).
01 ind pic 9.
procedure division.
invoke type System.IO.File::WriteAllBytes("chinese.txt", chinesetext)
*> Specify the code page to correctly interpret byte values
set wsEncoding to type Encoding::UTF8
set codePageValues to type System.IO.File::ReadAllBytes("chinese.txt")
*> Same content is now encoded as UTF-16
set unicodeValues to wsencoding::GetString(codePageValues)
*> Show that the text content is still intact in Unicode string
*> (Add a reference to System.Windows.Forms.dll)
invoke type System.Windows.Forms.MessageBox::Show(unicodeValues)
invoke type System.IO.File::WriteAllText("chinese_unicode.txt", unicodeValues)
*> Conversion is complete. Show the bytes to prove the conversion.
display "8-bit encoding byte values:"
perform varying b thru codePageValues
invoke type Console::Write("{0:X}-", b)
end-perform
display " "
display "Unicode values:"
set unicodeString to type System.IO.File::ReadAllText("chinese_unicode.txt")
set enumerator to type System.Globalization.StringInfo::GetTextElementEnumerator(unicodeString)
move 1 to ind
perform until exit
if enumerator::MoveNext()
set s to enumerator::GetTextElement()
set i to type Char::ConvertToUtf32(s, 0)
*> set final-string(ind:1) to i as
*> How to perform the conversion to PIC(N)????
set c to i
set final-string(ind:1) to c
add 1 to ind
invoke type Console::Write("{0:X}-", i)
else
exit perform
end-if
end-perform
*> Show the chinese string converted
invoke type System.Windows.Forms.MessageBox::Show(final-string)
display " "
display "Press any key to exit."
accept any-key
goback.
end program Program1.
Resolution:
The problem is that the following directive is not being set:
$set NSYMBOL"NATIONAL"
The default setting of NSYMBOL is "DBCS" which is not Unicode. If the directive above is added to the program the Chinese characters will be displayed correctly as they are Unicode characters and they require "NATIONAL" to be specified.
$set NSYMBOL"NATIONAL"
The default setting of NSYMBOL is "DBCS" which is not Unicode. If the directive above is added to the program the Chinese characters will be displayed correctly as they are Unicode characters and they require "NATIONAL" to be specified.