-1

I am trying to convert an array of bytes to hexadecimal string, so I see a lot of examples to generate this code

the following code have this error :

Error CS1503 Argument 2: cannot convert from 'int' to 'System.Globalization.NumberStyles' SecurityLibrary

any help?

public string bytesToString(byte[,]array ,int row ,int column)
        {
            string result = "";

            for(int i=0; i< row; i++)
            {
                byte[] arr = new byte[4];
                for(int j=0; j<column;j++)
                {
                    arr[j] = array[i,j];

                }

                string  num = Convert.ToString(long.Parse(arr.ToString() ,16) );
                result += num.ToString();
            }

            return result;
        }
  • 1
    Possible duplicate of [How do you convert a byte array to a hexadecimal string, and vice versa?](https://stackoverflow.com/questions/311165/how-do-you-convert-a-byte-array-to-a-hexadecimal-string-and-vice-versa) – d.moncada Apr 05 '19 at 15:09
  • None of the `long.Parse` overloads takes an int as 2nd parameter – vc 74 Apr 05 '19 at 15:09
  • so how to define the number base ex 16, 2? – khadeeja salem Apr 05 '19 at 15:12
  • This solution should be working
    https://stackoverflow.com/questions/311165/how-do-you-convert-a-byte-array-to-a-hexadecimal-string-and-vice-versa
    – Alaaeddine HFIDHI Apr 05 '19 at 15:46

1 Answers1

0

You can't use a byte array in this way. You need to convert each byte to hex, something like this should work:

string num = string.Join("", arr.Select(a =>  a.ToString("X")));

Alternatively if you fancy it as old school hex pairs:

string num = string.Join("", arr.Select((a, i) => i > 0 && i % 2 == 0 ? " " + a.ToString("X") : a.ToString("X")));
Simon
  • 1,081
  • 9
  • 14