gpt4 book ai didi

c# - 将字符串转换为 System.Data.SqlTypes.SqlBytes C#

转载 作者:行者123 更新时间:2023-11-30 23:27:25 32 4
gpt4 key购买 nike

我有像 a="0xECBDE9721C47B" 这样的字符串变量,我想将这部分转换为 System.Data.SqlTypes.SqlBytes 类型的新变量。我需要的是解压缩该字符串中的值。

对于解压缩我想使用这个函数:

/// 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 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));

其中 a 是您的输入字符串,以 0x 开头。
之后您可以:

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:
我已经用你的 BinaryCompressBinaryDecompress 试过了,对我有用。
我的测试代码:

    /// <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!!"

希望对你有帮助

关于c# - 将字符串转换为 System.Data.SqlTypes.SqlBytes C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36567848/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com