File: util.cs

package info (click to toggle)
ampsharp 2.0.4-2
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd, wheezy
  • size: 328 kB
  • ctags: 446
  • sloc: cs: 3,150; makefile: 10; ansic: 7
file content (43 lines) | stat: -rw-r--r-- 1,166 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/* Copyright (c) 2008-2011 - Eric P. Mangold
 * Released under the terms of the MIT/X11 license - see LICENSE.txt */
using System;
using System.Text;

namespace AMP
{
    internal class BytesUtil
    {
        public static bool AreEqual(byte[] x, byte[] y)
        {
            if (x.Length != y.Length) return false;
            for (int i = 0; i < x.Length; i++)
            {
                if (x[i] != y[i]) return false;
            }
            return true;
        }
        public static string ToHexStr(byte[] bytes)
        {
            var sb = new StringBuilder();
            foreach (byte b in bytes)
            {
                if (b < 32 || b > 126)
                {
                    sb.AppendFormat("\\x{0:x2}", b);
                }
                else
                {
                    if (b == '\\')
                    {
                        sb.Append("\\\\");
                    }
                    else
                    {
                        sb.Append((char)b);
                    }
                }
            }
            return sb.ToString();
        }
    }
}