Advertisement
Guest User

Wayne Bloss

a guest
Oct 29th, 2007
957
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.52 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4.  
  5. namespace Utils
  6. {
  7.     public class Hex
  8.     {
  9.         private static char[] _HexDigits = {
  10.             '0', '1', '2', '3', '4', '5', '6', '7',
  11.             '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
  12.             };
  13.        
  14.         public static string ToHexString(byte[] bytes)
  15.         {          
  16.             int j = bytes.Length;
  17.            
  18.             char[] chars = new char[j * 2];
  19.            
  20.             for (int i = 0; i < j; i++)
  21.             {
  22.                 int b = bytes[i];
  23.                 chars[i * 2] = _HexDigits[b >> 4];
  24.                 chars[i * 2 + 1] = _HexDigits[b & 0xF];
  25.             }
  26.             return new string(chars);
  27.         }
  28.        
  29.         public static string ToHexString(string sPrefix, byte[] bytes)
  30.         {          
  31.             int k = sPrefix.Length;
  32.            
  33.             int j = bytes.Length;
  34.            
  35.             char[] chars = new char[ k + (j * 2) ];
  36.            
  37.             sPrefix.CopyTo(0, chars, 0, k);
  38.            
  39.             for (int i = 0; i < j; i++)
  40.             {
  41.                 int b = bytes[i];
  42.                 int c = (i * 2) + k;
  43.                 chars[c] = _HexDigits[b >> 4];
  44.                 chars[c + 1] = _HexDigits[b & 0xF];
  45.             }
  46.             return new string(chars);
  47.         }
  48.        
  49.         public static string ToHexString(string sPrefix, byte[] bytes, string sSuffix)
  50.         {          
  51.             int k = sPrefix.Length;
  52.            
  53.             int m = sSuffix.Length;
  54.            
  55.             int j = bytes.Length;
  56.            
  57.             char[] chars = new char[ k + (j * 2) + m ];
  58.            
  59.             sPrefix.CopyTo(0, chars, 0, k);
  60.            
  61.             for (int i = 0; i < j; i++)
  62.             {
  63.                 int b = bytes[i];
  64.                 int c = (i * 2) + k;
  65.                 chars[c] = _HexDigits[b >> 4];
  66.                 chars[c + 1] = _HexDigits[b & 0xF];
  67.             }
  68.            
  69.             sSuffix.CopyTo(0, chars, k + (j * 2), m);
  70.            
  71.             return new string(chars);
  72.         }
  73.     }
  74. }
  75.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement