简体   繁体   中英

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].

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].

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..

looks like this.

3026b2758e66cf11a6d900aa0062ce6c301600000000000008000000010240a4d0d207e3d21197f000a0c95ea850cc0000000000000004001c00530066004f0072006900670069006e0061006c00460050005300000003000400b49204001c0057004d004600530044004b00560065007200730069006f006e00000000001e00310031002e0030002e0036003000300031002e00370030003000300000001a0057004d004600530044004b004e006500650064006500640000000000160030002e0030002e0030002e00300030003000300000000c0049007300560042005200000002000400000000003326b2758e66cf11a6.......................................................................................................................................... d900aa0062ce6c54010000000000001e0000003a00da000000570dcb8b495848cea4609eca906bc24db442394f0ddac5eb0604fb99820bcc30ff0f1736eefd74cd4317a21a369e208c580dbb02f90e888f0a35901e08439ec6087c61d241bc3c476c24d311291a678596a98792a9000b68adf213906e0f00097c8d989e517ee532fcd6cb70e520ec9dd4fad8a1a37668bbd678bea11c1fcf2d187c4c4c6c09c3c2c53d3e64016cfebc34eace85d45a4c08cd78d05d3934e05b72ec1 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;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM