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.

nnnn
  • 1,041
  • 3
  • 18
  • 35
  • what do you mean by " is allowed to accept double_byte string " ? – Freelancer Mar 11 '13 at 05:36
  • to store that value in database , what datatype you have used at backend[database]? – Freelancer Mar 11 '13 at 05:37
  • I means User can Type Double_Byte Character in the Textbox. – nnnn Mar 11 '13 at 05:38
  • you are using ASP .NET or its windows app? – Freelancer Mar 11 '13 at 05: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); – Jens Meinecke Mar 11 '13 at 05:48
  • The example you posted *looks* like a valid date, but actually isn't. The Unicode code-points you used (0xFF12, 0xFF10, 0xFF11, 0xFF12, 0xFF0F, ...) may *look* like the ASCII numbers for the date, but they are actually full-width glyphs for digits and various ASCII characters. You need to convert them to ASCII before you can use them. – Corey Mar 11 '13 at 05:54

3 Answers3

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

sa_ddam213
  • 42,848
  • 7
  • 101
  • 110
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;
}
Community
  • 1
  • 1
nnnn
  • 1,041
  • 3
  • 18
  • 35
-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.

Freelancer
  • 9,008
  • 7
  • 42
  • 81
  • With the Unicode strings the OP is dealing with, this will fail. – Corey Mar 11 '13 at 05:48
  • 1
    DateTime.Parse(txtDate.Text); will not fail. I have tried it in many windows apps. – Freelancer Mar 11 '13 at 05:49
  • 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? – Corey Mar 11 '13 at 05:58