ToCharArray() public static method

Converts an array of bytes to an array of chars
public static ToCharArray ( byte byteArray ) : char[]
byteArray byte The array of bytes to convert
return char[]
Beispiel #1
0
        /// <summary> Create a String from a byte []. Optionally swap adjacent byte
        /// pairs.  Intended to be used to create integer String representations
        /// allowing for endian translations.
        /// </summary>
        /// <param name="bfr">data array
        /// </param>
        /// <param name="offset">start of data in array
        /// </param>
        /// <param name="length">length of data in array
        /// </param>
        /// <param name="swap">swap adjacent bytes?
        /// </param>
        /// <returns> String rep of data
        /// </returns>
        public static string getString(byte[] bfr, int offset, int length, bool swap)
        {
            byte[] result = new byte[length];
            int    incr   = swap ? -1 : 1;
            int    start  = swap ? offset + length - 1 : offset;

            for (int i = 0, j = start; i < length; ++i)
            {
                result[i] = bfr[j];
                j        += incr;
            }
            return(new string(SupportClass.ToCharArray(result)));
        }
Beispiel #2
0
        /// <summary>Encode a 32bit integer according to the Seaman-Pence proposal.</summary>
        /// <param name="c">The checksum previously calculated.</param>
        /// <return>The encoded string of 16 bytes.</param>
        private static String ChecksumEnc(long c, bool compl)
        {
            byte[] asc     = new byte[16];
            int[]  exclude = { 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, 0x40, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f, 0x60 };
            long[] mask    = { 0xff000000L, 0xff0000L, 0xff00L, 0xffL };
            int    offset  = 0x30; /* ASCII 0 (zero */
            long   value   = compl ? ~c : c;

            for (int i = 0; i < 4; i++)
            {
                int   byt       = (int)((value & mask[i]) >> (24 - 8 * i)); // each byte becomes four
                int   quotient  = byt / 4 + offset;
                int   remainder = byt % 4;
                int[] ch        = new int[4];
                for (int j = 0; j < 4; j++)
                {
                    ch[j] = quotient;
                }

                ch[0] += remainder;
                bool check = true;
                for (; check;) // avoid ASCII punctuation
                {
                    check = false;

                    for (int k = 0; k < exclude.Length; k++)
                    {
                        for (int j = 0; j < 4; j += 2)
                        {
                            if (ch[j] == exclude[k] || ch[j + 1] == exclude[k])
                            {
                                ch[j]++;
                                ch[j + 1]--;
                                check = true;
                            }
                        }
                    }
                }

                for (int j = 0; j < 4; j++) // assign the bytes
                {
                    asc[4 * j + i] = (byte)(ch[j]);
                }
            }

            // shift the bytes 1 to the right circularly.
            String resul = new String(SupportClass.ToCharArray(asc), 15, 1);

            return(resul.Insert(resul.Length, new String(SupportClass.ToCharArray(asc), 0, 15)));
        }
Beispiel #3
0
 public static object doEncoding(object obj, string encoding)
 {
     if ((obj is string) && (encoding != null))
     {
         Encoding encoding2 = Encoding.Default;
         try
         {
             encoding2 = Encoding.GetEncoding(encoding);
         }
         catch (Exception)
         {
         }
         obj = new string(SupportClass.ToCharArray(encoding2.GetBytes((string)obj)));
     }
     return(obj);
 }
Beispiel #4
0
        /// <summary>
        /// Verify a signature.
        /// </summary>
        /// <param name="signature">The signature to verify.</param>
        /// <param name="data">The data from which the signature was created.</param>
        /// <returns></returns>
        public override bool VerifySignature(byte[] signature, byte[] data)
        {
            ByteBuffer buffer = new ByteBuffer(signature);

            byte[] sig    = buffer.ReadBinaryString();
            String header = new String(SupportClass.ToCharArray(sig));

            if (!header.Equals("ssh-dss"))
            {
                return(false);
            }

            signature = buffer.ReadBinaryString();

            return(base.VerifySignature(signature, data));
        }
Beispiel #5
0
        /// <summary> Converts a 1-Wire Network address byte array (little endian)
        /// to a hex string representation (big endian).
        ///
        /// </summary>
        /// <param name="address">family code first.
        ///
        /// </param>
        /// <returns> address represented in a String, family code last.
        /// </returns>
        public static System.String toString(byte[] address)
        {
            // When displaying, the CRC is first, family code is last so
            // that the center 6 bytes are a real serial number (not byte reversed).

            byte[] barr  = new byte[16];
            int    index = 0;
            int    ch;

            for (int i = 7; i >= 0; i--)
            {
                ch            = (address[i] >> 4) & 0x0F;
                ch           += ((ch > 9)?'A' - 10:'0');
                barr[index++] = (byte)ch;
                ch            = address[i] & 0x0F;
                ch           += ((ch > 9)?'A' - 10:'0');
                barr[index++] = (byte)ch;
            }

            return(new System.String(SupportClass.ToCharArray(barr)));
        }
Beispiel #6
0
        public static string ToString(byte[] data)
        {
            int i, row, col, rem, rows, cols;

            StringBuilder rep  = new StringBuilder();
            StringBuilder rep0 = null;
            StringBuilder rep1 = null;
            StringBuilder rep2 = null;

            cols = 16;
            rows = data.Length / cols;
            rem  = data.Length % cols;

            byte[] lbytes = new byte[8];
            for (row = 0, i = 0; row < rows; ++row)
            {
                rep1 = new StringBuilder();
                rep2 = new StringBuilder();

                for (i = 0; i < 8; ++i)
                {
                    lbytes[i] = 0;
                }
                byte[] tbytes = Encoding.UTF8.GetBytes(Convert.ToString(row * 16, 16));
                for (int t = 0, l = lbytes.Length - tbytes.Length; t < tbytes.Length; ++l, ++t)
                {
                    lbytes[l] = tbytes[t];
                }

                rep0 = new StringBuilder(new string(SupportClass.ToCharArray(lbytes)));

                for (col = 0; col < cols; ++col)
                {
                    byte b = data[i++];
                    rep1.Append(toHexString(b)).Append(i % 2 == 0 ? " " : "");
                    if ((char.IsLetter((char)b) || ((char)b).CompareTo('$') == 0 || ((char)b).CompareTo('_') == 0))
                    {
                        rep2.Append((char)b);
                    }
                    else
                    {
                        rep2.Append(".");
                    }
                }
                rep.Append(rep0).Append(" :  ").Append(rep1).Append(":  ").Append(rep2).Append(eol);
            }

            rep1 = new System.Text.StringBuilder();
            rep2 = new System.Text.StringBuilder();

            for (i = 0; i < 8; ++i)
            {
                lbytes[i] = 0;
            }
            byte[] tbytes2 = System.Text.Encoding.UTF8.GetBytes(System.Convert.ToString(row * 16, 16));
            for (int t = 0, l = lbytes.Length - tbytes2.Length; t < tbytes2.Length; ++l, ++t)
            {
                lbytes[l] = tbytes2[t];
            }

            rep0 = new StringBuilder(System.Text.Encoding.UTF8.GetString(lbytes, 0, lbytes.Length));

            for (col = 0; col < rem; ++col)
            {
                byte b = data[i++];
                rep1.Append(toHexString(b)).Append(i % 2 == 0 ? " " : "");
                if ((char.IsLetter((char)b) || ((char)b).CompareTo('$') == 0 || ((char)b).CompareTo('_') == 0))
                {
                    rep2.Append((char)b);
                }
                else
                {
                    rep2.Append(".");
                }
            }
            for (col = rem; col < 16; ++col)
            {
                rep1.Append("  ").Append(col % 2 == 0 ? " " : "");
            }

            rep.Append(rep0).Append(" :  ").Append(rep1).Append(":  ").Append(rep2).Append(eol);

            return(rep.ToString());
        }
Beispiel #7
0
        /*
         * Take a string and return the base64 representation of its SHA-1.
         */
        public static System.String encodeBase64(System.String str)
        {
            // Convert a string to a sequence of 16-word blocks, stored as an array.
            // Append padding bits and the length, as described in the SHA1 standard

            sbyte[] x    = SupportClass.ToSByteArray(SupportClass.ToByteArray(str));
            int[]   blks = new int[(((x.Length + 8) >> 6) + 1) * 16];
            int     i;

            for (i = 0; i < x.Length; i++)
            {
                blks[i >> 2] |= x[i] << (24 - (i % 4) * 8);
            }

            blks[i >> 2]         |= 0x80 << (24 - (i % 4) * 8);
            blks[blks.Length - 1] = x.Length * 8;

            // calculate 160 bit SHA1 hash of the sequence of blocks

            int[] w = new int[80];

            int a = 1732584193;
            int b = -271733879;
            int c = -1732584194;
            int d = 271733878;
            int e = -1009589776;

            for (i = 0; i < blks.Length; i += 16)
            {
                int olda = a;
                int oldb = b;
                int oldc = c;
                int oldd = d;
                int olde = e;

                for (int j = 0; j < 80; j++)
                {
                    w[j] = (j < 16)?blks[i + j]:(rol(w[j - 3] ^ w[j - 8] ^ w[j - 14] ^ w[j - 16], 1));

                    int t = rol(a, 5) + e + w[j] + ((j < 20)?1518500249 + ((b & c) | ((~b) & d)):((j < 40)?1859775393 + (b ^ c ^ d):((j < 60)?-1894007588 + ((b & c) | (b & d) | (c & d)):-899497514 + (b ^ c ^ d))));
                    e = d;
                    d = c;
                    c = rol(b, 30);
                    b = a;
                    a = t;
                }

                a = a + olda;
                b = b + oldb;
                c = c + oldc;
                d = d + oldd;
                e = e + olde;
            }

            // Convert 160 bit hash to base64

            int[]   words  = new int[] { a, b, c, d, e, 0 };
            sbyte[] base64 = SupportClass.ToSByteArray(SupportClass.ToByteArray("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"));
            sbyte[] result = new sbyte[28];
            for (i = 0; i < 27; i++)
            {
                int start  = i * 6;
                int word   = start >> 5;
                int offset = start & 0x1f;

                if (offset <= 26)
                {
                    result[i] = base64[(words[word] >> (26 - offset)) & 0x3F];
                }
                else if (offset == 28)
                {
                    result[i] = base64[(((words[word] & 0x0F) << 2) | ((words[word + 1] >> 30) & 0x03)) & 0x3F];
                }
                else
                {
                    result[i] = base64[(((words[word] & 0x03) << 4) | ((words[word + 1] >> 28) & 0x0F)) & 0x3F];
                }
            }
            result[27] = (sbyte)'=';

            return(new System.String(SupportClass.ToCharArray(SupportClass.ToByteArray(result))));
        }