views:

84

answers:

1

Trying to develop a text editor, I've got two textboxes, and a button below each one.

When the button below textbox1 is pressed, it is supposed to convert the Unicode text (intended to be Japanese) to Shift-JIS.

The reason why I am doing this is because the software VOCALOID2 only allows ANSI and Shift-JIS encoding text to be pasted into the lyrics system. Users of the application normally have their keyboard set to change to Japanese already, but it types in Unicode.

How can I convert Unicode text to Shift-JIS when SJIS isn't available in the System.Text.Encoding types?

A: 

Thankfully, that is not the way it works. As long as you are manipulating text in a .NET program, including the TextBox.Text property, there is only one encoding, UTF-16. When you need to work with the outside world, whether it is a file or a P/Invoked function, then you need to convert between Shift-Jis and UTF-16. Which is pretty straight forward:

        var enc = Encoding.GetEncoding("shift-jis");
        var value = enc.GetBytes("hello world");

Pass the value of "value" to whatever code needs the Shift-JIS encoded value. Make sure it is not a TextBox, it doesn't know how to display bytes, it only knows UTF-16.

Hans Passant
Oops, use Dim instead of var.
Hans Passant
Would a RichTextBox suffice for this? I've got it implemented, but yes as textbox it won't work.Thank you for your first reply also!
Yiu Korochko
I tried using RichTextBoxes and that worked like a charm! thank you very much!
Yiu Korochko
Well okay, I've revised some code. I couldn't get value (i changed the var name, but still) to work, so I did this:Dim enc As System.Text.Encoding = System.Text.Encoding.GetEncoding("shift-jis") My.Computer.FileSystem.WriteAllText("C:\q.w", TextBox1.Text, False, System.Text.Encoding.UTF8) TextBox2.Text = My.Computer.FileSystem.ReadAllText("C:\q.w", enc) My.Computer.FileSystem.DeleteFile("C:\q.w")
Yiu Korochko