0

I get byte by a file and i want to build the same file from the byte array. I tried this but it isn't working Write bytes to file

try
{
    using (var fs = new FileStream(fileName, FileMode.Create, FileAccess.Write))
    {
        string[] strings = { splitedPacket[3] };
        byte[] bytesConverted = Array.ConvertAll(strings, Byte.Parse);
        fs.Write(bytesConverted, 0, bytesConverted.Length);
    }
}
catch (Exception ex)
{
    Console.WriteLine("Exception caught in process: {0}", ex);
}

Exception thrown: 'System.FormatException' in mscorlib.dll
Exception caught in process: System.FormatException: The format of the input string is incorrect.
at System.Number.StringToNumber(String str, NumberStyles options, 
NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal)
at System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info)
at System.Byte.Parse(String s, NumberStyles style, NumberFormatInfo info)
at System.Byte.Parse(String s)
at System.Array.ConvertAll[TInput,TOutput](TInput[] array, Converter`2 converter)
at JeSaisPas.ConnectionUtils.<>c__DisplayClass5_0.<Server>b__0(Object s, DoWorkEventArgs args) in Util.cs:line 142
  • One of your string has a wrong format. It should be a number between 0 and 255. – SomeBody May 19 '22 at 08:31
  • Which string and where? – IdkWhatShouldIUse May 19 '22 at 08:34
  • 1
    I have a strong suspicion that this `Array.ConvertAll(strings, Byte.Parse);` is not at all doing what you think it is. But first of all: what exactly do the `string[]` from `splitedPackage[3]` represent? – Fildor May 19 '22 at 08:40
  • Fildor it's splitting a string like string[] ::: so i use [3] for byte – IdkWhatShouldIUse May 19 '22 at 08:45
  • Yes, but it's strings, not bytes. So what do they look like? – Fildor May 19 '22 at 08:46
  • What does the `file bytes` string look like? Is it perchance in hex? `Byte.Parse` expects the string to be decimal format by default. – Matthew Watson May 19 '22 at 08:46
  • `byte[] pictureArray = File.ReadAllBytes(picturePath);` i use this code for get bytes from a file and send it as bytes to a function. I don't know if it's a error but on func reqs i have only this `(string ip, int port, string message )` i only use message for send something – IdkWhatShouldIUse May 19 '22 at 08:50

0 Answers0