2016-04-12 5 views
2

a="0xECBDE9721C47B"のような文字列変数を持っています。この部分をSystem.Data.SqlTypes.SqlBytesという新しい変数に変換します。私が必要とするのは、その文字列の値を解凍することです。解凍のために文字列をSystem.Data.SqlTypes.SqlBytesに変換します。C#

私は、この機能を使用したい:

/// Decompressing the data 
/// </summary> 
[Microsoft.SqlServer.Server.SqlFunction(IsDeterministic = true, IsPrecise = true, 
    DataAccess = DataAccessKind.None)] 
public static SqlBytes BinaryDecompress(SqlBytes input) 
{ 
    if (input.IsNull) 
    return SqlBytes.Null; 

    int batchSize = 32768; 
    byte[] buf = new byte[batchSize]; 

    using (MemoryStream result = new MemoryStream()) 
    { 
    using (DeflateStream deflateStream = 
     new DeflateStream(input.Stream, CompressionMode.Decompress, true)) 
    { 
     int bytesRead; 
     while ((bytesRead = deflateStream.Read(buf, 0, batchSize)) > 0) 
     result.Write(buf, 0, bytesRead); 
    } 
    return new SqlBytes(result.ToArray()); 
    } 
} 

答えて

2

たぶん、あなたは試すことができます:a0xで始まる、あなたの入力文字列である

public static byte[] GetBinaryFromHexaString (string hexa) 
{ 
    byte[] data = null; 
    List<byte> bList = new List<byte>(); 
    try { 
     for (int i = 2; i < hexa.Length - 1; i+=2) { 
      string hStr = hexa.Substring(i, 2); 
      byte b = byte.Parse(hStr, NumberStyles.HexNumber, CultureInfo.InvariantCulture); 
      bList.Add (b); 
     } 
     data = bList.ToArray(); 
    } 
    catch {} 
    return data; 
} 

var sqlBytes = new System.Data.SqlTypes.SqlBytes (GetBinaryFromHexaString(a)); 

を。
あなたができることを後:

var decompressed = BinaryDecompress(sqlBytes); 

編集:
この方法を試してください。

public static SqlBytes BinaryDecompress(SqlBytes input) 
{ 
    if (input.IsNull) 
    return SqlBytes.Null; 

    var outputStream = new MemoryStream(); 
    using (MemoryStream result = new MemoryStream()) 
    { 
     using (DeflateStream deflateStream = new DeflateStream(input.Stream, CompressionMode.Decompress)) 
     { 
      deflateStream.CopyTo(outputStream); 
     } 
    } 

    outputStream.Position = 0; 
    return new SqlBytes (outputStream); 

} 

編集2:
私はこれを試してみましたあなたのBinaryCompress & BinaryDecompress私のために働いています。
私のテストコード:

/// <summary> 
    /// Compressing the data 
    /// </summary> 
    [Microsoft.SqlServer.Server.SqlFunction(IsDeterministic = true, IsPrecise = true, DataAccess = DataAccessKind.None)] 
    public static SqlBytes BinaryCompress(SqlBytes input) 
    { 
     if (input.IsNull) return SqlBytes.Null; 

     using (MemoryStream result = new MemoryStream()) 
     { 
      using (DeflateStream deflateStream = 
        new DeflateStream(result, CompressionMode.Compress, true)) 
      { 
       deflateStream.Write(input.Buffer, 0, input.Buffer.Length); 
       deflateStream.Flush(); 
       deflateStream.Close(); 
      } 
      return new SqlBytes(result.ToArray()); 
     } 
    } 

    /// <summary> 
    /// Decompressing the data 
    /// </summary> 
    [Microsoft.SqlServer.Server.SqlFunction(IsDeterministic = true, IsPrecise = true, DataAccess = DataAccessKind.None)] 
    public static SqlBytes BinaryDecompress(SqlBytes input) 
    { 
     if (input.IsNull) return SqlBytes.Null; 

     int batchSize = 32768; 
     byte[] buf = new byte[batchSize]; 

     using (MemoryStream result = new MemoryStream()) 
     { 
      using (DeflateStream deflateStream = 
        new DeflateStream(input.Stream, CompressionMode.Decompress, true)) 
      { 
       int bytesRead; 
       while ((bytesRead = deflateStream.Read(buf, 0, batchSize)) > 0) 
        result.Write(buf, 0, bytesRead); 
      } 
      return new SqlBytes(result.ToArray()); 
     } 
    } 


    public static string GetHexaStringFromBinary (byte[] data) 
    { 
     string hexData = "0x"; 
     for (int i = 0; i < data.Length; i++) { 
      hexData = string.Concat (hexData, data[i].ToString("X2")); 
     } 
     return hexData; 
    } 


    public static byte[] GetBinaryFromHexaString (string hexa) 
    { 
     byte[] data = null; 
     List<byte> bList = new List<byte>(); 
     try { 
      for (int i = 2; i < hexa.Length - 1; i+=2) { 
       string hStr = hexa.Substring(i, 2); 
       byte b = byte.Parse(hStr, NumberStyles.HexNumber, CultureInfo.InvariantCulture); 
       bList.Add (b); 
      } 
      data = bList.ToArray(); 
     } 
     catch {} 
     return data; 
    } 

と使用方法:

string originalStr = "This is a test string!!"; 
byte[] data = Encoding.ASCII.GetBytes (originalStr); 
SqlBytes sbCompressed = BinaryCompress (new SqlBytes (data)); 

string a = GetHexaStringFromBinary (sbCompressed.Value); 
//a = 0x0BC9C82C5600A2448592D4E21285E292A2CCBC74454500 

var sqlBytes = new SqlBytes(GetBinaryFromHexaString (a)); 
SqlBytes deCompressed = BinaryDecompress (sqlBytes); 
string finalStr = Encoding.ASCII.GetString (deCompressed.Value); 
//finalStr = "This is a test string!!" 

は、私は、この行(VAR sqlBytes = System.Data上の間違いを持っ​​て

+0

...それがお役に立てば幸いです。 SqlTypes.SqlBytes(GetBinaryFromHexaString(a));)、System.Data.SqlTypes.SqlBytesが指定されたコンテキストで無効な 'type'であることを書き込みます – VasyPupkin

+0

申し訳ありません。私のせい...私はその投稿を編集した。次のようにしてください: 'var sqlBytes = new System.Data.SqlTypes.SqlBytes(GetBinaryFromHexaString(a));' –

+0

私は新しい行を追加しますか?間違いはどこでしたか?この行のような別のapeare、BYTしばらく((bytesRead = deflateStream.Read(bufは、0、BATCHSIZE))> 0)InvalidDataExceptionが未処理だっについて、あなたの助けが – VasyPupkin

0
Code ALL 

    /// <summary> 
    /// Compressing the data 
    /// </summary> 
    [Microsoft.SqlServer.Server.SqlFunction(IsDeterministic = true, IsPrecise = true, 
     DataAccess = DataAccessKind.None)] 
    public static SqlBytes BinaryCompress(SqlBytes input) 
    { 
     if (input.IsNull) 
     return SqlBytes.Null; 

    using (MemoryStream result = new MemoryStream()) 
    { 
     using (DeflateStream deflateStream = 
      new DeflateStream(result, CompressionMode.Compress, true)) 
     { 
      deflateStream.Write(input.Buffer, 0, input.Buffer.Length); 
      deflateStream.Flush(); 
      deflateStream.Close(); 
     } 
     return new SqlBytes(result.ToArray()); 
    } 
    } 

/// <summary> 
/// Decompressing the data 
/// </summary> 
[Microsoft.SqlServer.Server.SqlFunction(IsDeterministic = true, IsPrecise = true, 
     DataAccess = DataAccessKind.None)] 
public static SqlBytes BinaryDecompress(SqlBytes input) 
{ 
    if (input.IsNull) 
     return SqlBytes.Null; 

    int batchSize = 32768; 
    byte[] buf = new byte[batchSize]; 

    using (MemoryStream result = new MemoryStream()) 
    { 
     using (DeflateStream deflateStream = 
      new DeflateStream(input.Stream, CompressionMode.Decompress, true)) 
     { 
      int bytesRead; 
      while ((bytesRead = deflateStream.Read(buf, 0, batchSize)) > 0) 
       result.Write(buf, 0, bytesRead); 
     } 
     return new SqlBytes(result.ToArray()); 
    } 
} 
+0

は、あなたは私が何を書くの近くにも – VasyPupkin

+0

を私を助けることができます – VasyPupkin

関連する問題