1

I am a newbie at C#.
I have a Textbox and it is allowed to accept double_byte string and it is used to input/show Date Value. Now I am having a problem and I don't know how to solve this.I googled about it,and can't find any solution for it.
When I wrote Double_Byte Characters( 2012/12/31) ,I want to change this value to (2012/12/31) at Leave_Event of TextBox or Text_Changed Event of this TextBox. So, How can I solve the problem.

Edit->My Solution is Window Application.

Thanks in advance.

6
  • what do you mean by " is allowed to accept double_byte string " ? Commented Mar 11, 2013 at 5:36
  • to store that value in database , what datatype you have used at backend[database]? Commented Mar 11, 2013 at 5:37
  • I means User can Type Double_Byte Character in the Textbox. Commented Mar 11, 2013 at 5:38
  • you are using ASP .NET or its windows app? Commented Mar 11, 2013 at 5:39
  • you might try to do something similar to this: var bytes = Encoding.Convert(Encoding.Unicode, Encoding.ASCII, sourceString); string ascii = Encoding.ASCII.GetString(bytes); Commented Mar 11, 2013 at 5:48

3 Answers 3

4

You should be able to use Encoding.Default to convert the double_byte string to a single_byte string

 string singleByteString = Encoding.Default.GetString(Encoding.Default.GetBytes(inputText));

Tests:

    private void button1_Click(object sender, EventArgs e)
    {
        string inputText = textBox1.Text;
        string singleByteString = Encoding.Default.GetString(Encoding.Default.GetBytes(inputText));

        textBox2.Text = singleByteString;
        textBox3.Text = inputText;
    }

Result:

enter image description here

Sign up to request clarification or add additional context in comments.

4 Comments

It still give 2012/12/31.I tested simple window application like you mentioned above.
try Encoding.ASCII.GetString(Encoding.Default.GetBytes(inputText))
?Q?O?P?Q?^?P?Q?^?R?P is the result of Encoding.ASCII.GetString(Encoding.Default.GetBytes(inputText))
Thank you, I added a solution.
1

Thank you for all of your answers and interests.
I searched a solution for this and not found any answer .Finally ,I got a nice answer from my colleague. Therefore, I share the answer of this problem.

  1. Add Reference Microsoft.VisualBasic
  2. write code like this(test like sa_ddam213's answer)-

using Microsoft.VisualBasic;

private void button1_Click(object sender, EventArgs e)
{
      string inputText = textBox1.Text;
      string singleByteString =  Strings.StrConv(inputText, VbStrConv.Narrow, 0);

      textBox2.Text = singleByteString;
      textBox3.Text = inputText;
}

1 Comment

+1, Another hidden gem in the Microsoft.VisualBasic namespace :)
-1

When you send your text box value in database , parse it to Date.

Make sure that you are storing that text box'x value in database as DateTime datatype.

parsing can be done as follows>

double d = double.Parse(txtDate.text);
DateTime conv = DateTime.FromOADate(d);

Or simplest way use>>

DateTime.Parse(txtDate.Text);

Hope this will help you.

3 Comments

With the Unicode strings the OP is dealing with, this will fail.
DateTime.Parse(txtDate.Text); will not fail. I have tried it in many windows apps.
Really? Have you tried it on the Unicode string the OP just posted, which uses code-points that are way outside the standard ASCII range? It fails. It doesn't know that Unicode code-point FF10 is a '0' for instance. Unless there's something funky with your localization settings?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.