[英]How to convert a large hexadecimal string to a byte array?
Hi I am in need of using file handling,for that i used a method for converting a hexadecimal string into a byte array.嗨,我需要使用文件处理,为此我使用了一种将十六进制字符串转换为字节数组的方法。
public static byte[] StringToByteArray(string hex)
{
return Enumerable.Range(0, hex.Length)
.Where(x => x % 2 == 0)
.Select(x => Convert.ToByte(hex.Substring(x, 2), 16))
.ToArray();
}
My problem is ,when i give a small hexadecimal string as a parameter to this function it will produce the right output,but when i used a large hexadecimal string as a parameter output is not that expected.我的问题是,当我给这个函数提供一个小的十六进制字符串作为参数时,它会产生正确的输出,但是当我使用一个大的十六进制字符串作为参数输出时,并不是预期的。
for your clear understanding -为了您的清楚了解 -
I used a hexadecimal string which is being converted from a byte array of value [26246026], when i convert that hex string into a byte array it should return a byte value as [26246026] - but its returning only the partial bytes ie.[262144].我使用了一个从值 [26246026] 的字节数组转换的十六进制字符串,当我将该十六进制字符串转换为字节数组时,它应该返回一个字节值 [26246026] - 但它只返回部分字节即。 262144]。
i cant get the exact byte value from the hex string,how can i get that?我无法从十六进制字符串中获得确切的字节值,我该如何获得? Please someone help me to get the expected output.
请有人帮助我获得预期的输出。
My input string for that method contains this hexadecimel string which is a 25mb size file-it should return a byte value of [26246026]---but its returning only the byte value of [262144].我的该方法的输入字符串包含这个十六进制字符串,它是一个 25mb 大小的文件 - 它应该返回 [26246026] 的字节值---但它只返回 [262144] 的字节值。
when am using small hex string (min size file) its working fine,but when i work on big files i cant get the original file byte.当我使用小十六进制字符串(最小文件大小)时,它工作正常,但是当我处理大文件时,我无法获得原始文件字节。 please suggest me what to do.
请建议我该怎么做。
my input parameter string content is as follow as asked in comment.我的输入参数字符串内容如下评论中所问。
Its totally 524288 characters in length..它的总长度为 524288 个字符。
looks like this.看起来像这样。
3026b2758e66cf11a6d900aa0062ce6c301600000000000008000000010240a4d0d207e3d21197f000a0c95ea850cc0000000000000004001c00530066004f0072006900670069006e0061006c00460050005300000003000400b49204001c0057004d004600530044004b00560065007200730069006f006e00000000001e00310031002e0030002e0036003000300031002e00370030003000300000001a0057004d004600530044004b004e006500650064006500640000000000160030002e0030002e0030002e00300030003000300000000c0049007300560042005200000002000400000000003326b2758e66cf11a6.......................................................................................................................................... d900aa0062ce6c54010000000000001e0000003a00da000000570dcb8b495848cea4609eca906bc24db442394f0ddac5eb0604fb99820bcc30ff0f1736eefd74cd4317a21a369e208c580dbb02f90e888f0a35901e08439ec6087c61d241bc3c476c24d311291a678596a98792a9000b68adf213906e0f00097c8d989e517ee532fcd6cb70e520ec9dd4fad8a1a37668bbd678bea11c1fcf2d187c4c4c6c09c3c2c53d3e64016cfebc34eace85d45a4c08cd78d05d3934e05b72ec1 3026b2758e66cf11a6d900aa0062ce6c301600000000000008000000010240a4d0d207e3d21197f000a0c95ea850cc0000000000000004001c00530066004f0072006900670069006e0061006c00460050005300000003000400b49204001c0057004d004600530044004b00560065007200730069006f006e00000000001e00310031002e0030002e0036003000300031002e00370030003000300000001a0057004d004600530044004b004e006500650064006500640000000000160030002e0030002e0030002e00300030003000300000000c0049007300560042005200000002000400000000003326b2758e66cf11a6 ................................................. ………………………………………………………………………………………………………………………………………………………… ....................................... d900aa0062ce6c54010000000000001e0000003a00da000000570dcb8b495848cea4609eca906bc24db442394f0ddac5eb0604fb99820bcc30ff0f1736eefd74cd4317a21a369e208c580dbb02f90e888f0a35901e08439ec6087c61d241bc3c476c24d311291a678596a98792a9000b68adf213906e0f00097c8d989e517ee532fcd6cb70e520ec9dd4fad8a1a37668bbd678bea11c1fcf2d187c4c4c6c09c3c2c53d3e64016cfebc34eace85d45a4c08cd78d05d3934e05b72ec1 94304848165a8c1a585c78423
94304848165a8c1a585c78423
/// <summary>
/// Parses a continuous hex stream from a string.
/// </summary>
public static byte[] ParseHexBytes(this string s)
{
if (s == null)
throw new ArgumentNullException("s");
if (s.Length == 0)
return new byte[0];
if (s.Length % 2 != 0)
throw new ArgumentException("Source length error", "s");
int length = s.Length >> 1;
byte[] result = new byte[length];
for (int i = 0; i < length; i++)
{
result[i] = Byte.Parse(s.Substring(i * 2, 2), NumberStyles.HexNumber);
}
return result;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.