nnnn
nnnn

Reputation: 1051

How to Convert Double Byte String to Single Byte String?

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.

Upvotes: 1

Views: 5969

Answers (3)

nnnn
nnnn

Reputation: 1051

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;
}

Upvotes: 1

sa_ddam213
sa_ddam213

Reputation: 43596

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

Upvotes: 4

Freelancer
Freelancer

Reputation: 9064

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.

Upvotes: -1

Related Questions