SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


C# conversion function


C# conversion function

Author
Message
gravitysucks
gravitysucks
SSC-Enthusiastic
SSC-Enthusiastic (154 reputation)SSC-Enthusiastic (154 reputation)SSC-Enthusiastic (154 reputation)SSC-Enthusiastic (154 reputation)SSC-Enthusiastic (154 reputation)SSC-Enthusiastic (154 reputation)SSC-Enthusiastic (154 reputation)SSC-Enthusiastic (154 reputation)

Group: General Forum Members
Points: 154 Visits: 232
I have no knowledge on c#. However i have the following code below that converts double byte to unicode. My requirement is reverse engineering i.e convert unicode (non english characters) to double byte ANSI.I have no idea what changes I should make to achieve that.Please help. thanks.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.ComponentModel;
using System.Data.SqlTypes;



namespace ConvertToUnicode
{

public static class ConvertStringToUnicode
{

public static string ConvertToUnicode(int codePage, SqlString input)
{
string output;

Encoding srcEncoding = Encoding.GetEncoding(codePage);
byte[] byteInput = input.GetNonUnicodeBytes();
byte[] byteOutput =UnicodeEncoding.Convert(srcEncoding, Encoding.Unicode, byteInput);

output = Encoding.Unicode.GetString(byteOutput);
return output;
}

}

}


Orlando Colamatteo
Orlando Colamatteo
SSC-Dedicated
SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)

Group: General Forum Members
Points: 39344 Visits: 14411
MisLead (4/23/2012)
I have no knowledge on c#. However i have the following code below that converts double byte to unicode. My requirement is reverse engineering i.e convert unicode (non english characters) to double byte ANSI.I have no idea what changes I should make to achieve that.Please help. thanks.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.ComponentModel;
using System.Data.SqlTypes;



namespace ConvertToUnicode
{

public static class ConvertStringToUnicode
{

public static string ConvertToUnicode(int codePage, SqlString input)
{
string output;

Encoding srcEncoding = Encoding.GetEncoding(codePage);
byte[] byteInput = input.GetNonUnicodeBytes();
byte[] byteOutput =UnicodeEncoding.Convert(srcEncoding, Encoding.Unicode, byteInput);

output = Encoding.Unicode.GetString(byteOutput);
return output;
}

}

}


Not enough info...

What you posted is presumably from a standard C# class...not necessarily a SQLCLR object.

What is your goal? To create a SQLCLR function that does the same thing?

What do you mean by "double byte ANSI"? UCS-2, i.e. Windows version of "Unicode"? Or Windows 1252 stored in double-byte format? What is the desired destination code page?

__________________________________________________________________________________________________
There are no special teachers of virtue, because virtue is taught by the whole community. --Plato
gravitysucks
gravitysucks
SSC-Enthusiastic
SSC-Enthusiastic (154 reputation)SSC-Enthusiastic (154 reputation)SSC-Enthusiastic (154 reputation)SSC-Enthusiastic (154 reputation)SSC-Enthusiastic (154 reputation)SSC-Enthusiastic (154 reputation)SSC-Enthusiastic (154 reputation)SSC-Enthusiastic (154 reputation)

Group: General Forum Members
Points: 154 Visits: 232
opc.three (4/24/2012)
MisLead (4/23/2012)
I have no knowledge on c#. However i have the following code below that converts double byte to unicode. My requirement is reverse engineering i.e convert unicode (non english characters) to double byte ANSI.I have no idea what changes I should make to achieve that.Please help. thanks.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.ComponentModel;
using System.Data.SqlTypes;



namespace ConvertToUnicode
{

public static class ConvertStringToUnicode
{

public static string ConvertToUnicode(int codePage, SqlString input)
{
string output;

Encoding srcEncoding = Encoding.GetEncoding(codePage);
byte[] byteInput = input.GetNonUnicodeBytes();
byte[] byteOutput =UnicodeEncoding.Convert(srcEncoding, Encoding.Unicode, byteInput);

output = Encoding.Unicode.GetString(byteOutput);
return output;
}

}

}


Not enough info...

What you posted is presumably from a standard C# class...not necessarily a SQLCLR object.

What is your goal? To create a SQLCLR function that does the same thing?

What do you mean by "double byte ANSI"? UCS-2, i.e. Windows version of "Unicode"? Or Windows 1252 stored in double-byte format? What is the desired destination code page?


I agree its a C# class but when i have proper codes that does what it needs to do, i want to deploy as a clr function ssms.

As stated in my post, what this code does is converts double byte ANSI characters (ansi characters>127) back to Unicode (chinesse/japanese). My requirement though is to convert those chinese/japanese characters to double byte characters(don't ask me why, its the requirement) because these non english letters cannot be contained within single byte unlike English.

Apparently there is a way to map the unicode(when i say unicode its strictly non english) to extended ansi characters. the link below has the list of extended characters from 128-255.
http://ascii-table.com/ansi-codes.php

my guess is that you just need to tweak some code up there, but as i said i have NULL in c#.
Orlando Colamatteo
Orlando Colamatteo
SSC-Dedicated
SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)

Group: General Forum Members
Points: 39344 Visits: 14411
MisLead (4/24/2012)
opc.three (4/24/2012)
MisLead (4/23/2012)
I have no knowledge on c#. However i have the following code below that converts double byte to unicode. My requirement is reverse engineering i.e convert unicode (non english characters) to double byte ANSI.I have no idea what changes I should make to achieve that.Please help. thanks.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.ComponentModel;
using System.Data.SqlTypes;



namespace ConvertToUnicode
{

public static class ConvertStringToUnicode
{

public static string ConvertToUnicode(int codePage, SqlString input)
{
string output;

Encoding srcEncoding = Encoding.GetEncoding(codePage);
byte[] byteInput = input.GetNonUnicodeBytes();
byte[] byteOutput =UnicodeEncoding.Convert(srcEncoding, Encoding.Unicode, byteInput);

output = Encoding.Unicode.GetString(byteOutput);
return output;
}

}

}


Not enough info...

What you posted is presumably from a standard C# class...not necessarily a SQLCLR object.

What is your goal? To create a SQLCLR function that does the same thing?

What do you mean by "double byte ANSI"? UCS-2, i.e. Windows version of "Unicode"? Or Windows 1252 stored in double-byte format? What is the desired destination code page?


I agree its a C# class but when i have proper codes that does what it needs to do, i want to deploy as a clr function ssms.

As stated in my post, what this code does is converts double byte ANSI characters (ansi characters>127) back to Unicode (chinesse/japanese). My requirement though is to convert those chinese/japanese characters to double byte characters(don't ask me why, its the requirement) because these non english letters cannot be contained within single byte unlike English.

Your understanding of "double byte ANSI characters (ansi characters>127)" may need a tweak. Characters represented on the ASCII map at positions 128-255 still only occupy 1 byte.

Apparently there is a way to map the unicode(when i say unicode its strictly non english) to extended ansi characters. the link below has the list of extended characters from 128-255.
http://ascii-table.com/ansi-codes.php

my guess is that you just need to tweak some code up there, but as i said i have NULL in c#.

Only the converse is true...while the characters in ASCII 128-255 may have Unicode representations, by definition ASCII != Unicode and certainly cannot model anything in the realm of Korean or Chinese per its single-byte representation. Futher to that, you still need to apply a codepage before the binary will make sense. Do you know if the dialects you are trying to model are included the Unicode BMP? If so then you can simply store them in SQL Server as NVARCHAR or NCHAR. If you are simply trying to fool SQL Server into storing multi-byte Chinese and Korean characters in a VARCHAR column, and then encoding the contents on the way and and out then you are playing with fire. Collations in a VARCHAR column may eat characters and replace them with '?' (question marks).

I need to know more about your data source, your column storage, and your goals on storage and retrieval before I can help.

__________________________________________________________________________________________________
There are no special teachers of virtue, because virtue is taught by the whole community. --Plato
adrian.facio
adrian.facio
Right there with Babe
Right there with Babe (714 reputation)Right there with Babe (714 reputation)Right there with Babe (714 reputation)Right there with Babe (714 reputation)Right there with Babe (714 reputation)Right there with Babe (714 reputation)Right there with Babe (714 reputation)Right there with Babe (714 reputation)

Group: General Forum Members
Points: 714 Visits: 386
Hi man,

This is just a guess.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.ComponentModel;
using System.Data.SqlTypes;



namespace ConvertToASCII
{

public static class ConvertStringToASCII
{

public static string ConvertToASCII(int codePage, SqlString input)
{
string output;

Encoding srcEncoding = Encoding.GetEncoding(codePage);
byte[] byteInput = input.GetUnicodeBytes();
byte[] byteOutput =ASCIIEncoding.Convert(srcEncoding, Encoding.ASCII, byteInput);

output = Encoding.ASCII.GetString(byteOutput);
return output;
}

}

}
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search