Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

binary to XML Encoding problem Expand / Collapse
Author
Message
Posted Thursday, October 7, 2010 5:24 AM


Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Monday, November 24, 2014 2:48 AM
Points: 16, Visits: 131
hi all,

I have CLR function written in following code,

=======CLR FUNCTION=========

public static string Str2UTF8ASCII(string sMain,string RCSeq)
{
ASCIIEncoding ascii = new ASCIIEncoding();
byte[] byteArray = Encoding.UTF8.GetBytes(sMain);
byte[] asciiArray = Encoding.Convert(Encoding.UTF8, Encoding.ASCII, byteArray);
string finalString = ascii.GetString(asciiArray);
return finalString.Replace("?X?X?", RCSeq);
}

public static byte[] StringToByte(String hex)
{
int NumberChars = hex.Length;
byte[] bytes = new byte[NumberChars / 2];
for (int i = 0; i < NumberChars; i += 2)
bytes[i / 2] = Convert.ToByte(hex.Substring(i, 2), 16);
return bytes;
}

[Microsoft.SqlServer.Server.SqlFunction(DataAccess = DataAccessKind.None)]
public static string fn_CLR_BytesAsString(string ZipInput, string Replaceto)
{
string strDecompressedDataStream;
strDecompressedDataStream = "";
try
{
Stream InputStream = new MemoryStream(StringToByte(ZipInput));
using (GZipStream Decompress = new GZipStream(InputStream,
CompressionMode.Decompress))
{
MemoryStream OutStream = new MemoryStream();
byte[] buffer = new byte[4096];
int numRead;
while ((numRead = Decompress.Read(buffer, 0, buffer.Length)) != 0)
{
OutStream.Write(buffer, 0, numRead);

}
OutStream.Position = 0;
StreamReader reader = new StreamReader(OutStream);
strDecompressedDataStream = reader.ReadToEnd();
strDecompressedDataStream = Str2UTF8ASCII(strDecompressedDataStream, Replaceto);
}
}
catch
{
strDecompressedDataStream = "-1";
}
finally { }

return strDecompressedDataStream;
}



==========================



after created ASSEMBLY and execute result contain garbage value(?) as follows,

select CONVERT(XML,dbo.fn_CLR_BytesAsString(<my value>,'ObjectTag="')) ssql

Results:
<void property="description">
<string>test b? abc, abc axv tt-wwwwww-ffffffff</string>
</void>



garbage value is "?"

what is the Encoding problem in above CLR function?



Thanks

Tharindu Dhaneenja


Tharindu Dhaneenja.
MCTS,MCITP(SQL Server),OCA(Oracle)
http://www.databaseusergroup.com
Post #1000306
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse