public static bool InHexFormat(string hexString)
        {
            bool flag = true;

            foreach (char c in hexString)
            {
                if (!HexEncoding.IsHexDigit(c))
                {
                    flag = false;
                    break;
                }
            }
            return(flag);
        }
        public static int GetByteCount(string hexString)
        {
            int num = 0;

            for (int index = 0; index < hexString.Length; ++index)
            {
                if (HexEncoding.IsHexDigit(hexString[index]))
                {
                    ++num;
                }
            }
            if (num % 2 != 0)
            {
                --num;
            }
            return(num / 2);
        }
Esempio n. 3
0
        /// <summary>
        /// Obtener una imagen desde la informacion RAW en formato hexadecimal
        /// </summary>
        /// <param name="hexRaw">RAW en hexadecimal</param>
        /// <param name="pixelFormat">Profundidad en bits</param>
        /// <param name="size">Tamaño de la imagen (ancho x alto)</param>
        /// <returns>Imagen creada</returns>
        public static Bitmap RawToImage(string hexRaw, PixelFormat pixelFormat, Size size)
        {
            Bitmap     bmp = null;
            BitmapData bd  = null;

            try
            {
                int    discarded = 0;
                byte[] raw       = HexEncoding.GetBytes(hexRaw, out discarded);
                bmp = new Bitmap(size.Width, size.Height, pixelFormat);
                bd  = bmp.LockBits(new Rectangle(Point.Empty, size), ImageLockMode.WriteOnly, pixelFormat);
                Marshal.Copy(raw, 0, bd.Scan0, raw.Length);
                return(bmp);
            }
            finally
            {
                if (bd != null)
                {
                    bmp.UnlockBits(bd);
                }
            }
        }
        public static Bitmap RawToImage(string hexRaw, PixelFormat pixelFormat, Size size)
        {
            Bitmap     bitmap     = (Bitmap)null;
            BitmapData bitmapdata = (BitmapData)null;

            try
            {
                int    discarded = 0;
                byte[] bytes     = HexEncoding.GetBytes(hexRaw, out discarded);
                bitmap     = new Bitmap(size.Width, size.Height, pixelFormat);
                bitmapdata = bitmap.LockBits(new Rectangle(Point.Empty, size), ImageLockMode.WriteOnly, pixelFormat);
                Marshal.Copy(bytes, 0, bitmapdata.Scan0, bytes.Length);
                return(bitmap);
            }
            finally
            {
                if (bitmapdata != null)
                {
                    bitmap.UnlockBits(bitmapdata);
                }
            }
        }
        public static byte[] GetBytes(string hexString, out int discarded)
        {
            discarded = 0;
            string str = "";

            for (int index = 0; index < hexString.Length; ++index)
            {
                char c = hexString[index];
                if (HexEncoding.IsHexDigit(c))
                {
                    str += (string)(object)c;
                }
                else
                {
                    ++discarded;
                }
            }
            if (str.Length % 2 != 0)
            {
                ++discarded;
                str = str.Substring(0, str.Length - 1);
            }
            byte[] numArray = new byte[str.Length / 2];
            int    index1   = 0;

            for (int index2 = 0; index2 < numArray.Length; ++index2)
            {
                string hex = new string(new char[2]
                {
                    str[index1],
                    str[index1 + 1]
                });
                numArray[index2] = HexEncoding.HexToByte(hex);
                index1          += 2;
            }
            return(numArray);
        }
        public static string ImageToRaw(Bitmap bmBitmap)
        {
            BitmapData bitmapdata = (BitmapData)null;

            try
            {
                if (bmBitmap.PixelFormat != PixelFormat.Format1bppIndexed)
                {
                    bmBitmap = PrintingUtils.Monocrome(bmBitmap, false);
                }
                bitmapdata = bmBitmap.LockBits(new Rectangle(0, 0, bmBitmap.Width, bmBitmap.Height), ImageLockMode.ReadOnly, PixelFormat.Format1bppIndexed);
                int    length   = bitmapdata.Height * bitmapdata.Stride;
                byte[] numArray = new byte[length];
                Marshal.Copy(bitmapdata.Scan0, numArray, 0, length);
                return(HexEncoding.ToString(numArray));
            }
            finally
            {
                if (bitmapdata != null)
                {
                    bmBitmap.UnlockBits(bitmapdata);
                }
            }
        }
        public static string CompressHex(string hexData)
        {
            if (HexEncoding.encode2.Count == 0)
            {
                HexEncoding.encode2.Add(20, "g");
                HexEncoding.encode2.Add(40, "h");
                HexEncoding.encode2.Add(60, "i");
                HexEncoding.encode2.Add(80, "j");
                HexEncoding.encode2.Add(100, "k");
                HexEncoding.encode2.Add(120, "l");
                HexEncoding.encode2.Add(140, "m");
                HexEncoding.encode2.Add(160, "n");
                HexEncoding.encode2.Add(180, "o");
                HexEncoding.encode2.Add(200, "p");
                HexEncoding.encode2.Add(220, "q");
                HexEncoding.encode2.Add(240, "r");
                HexEncoding.encode2.Add(260, "s");
                HexEncoding.encode2.Add(280, "t");
                HexEncoding.encode2.Add(300, "u");
                HexEncoding.encode2.Add(320, "v");
                HexEncoding.encode2.Add(340, "w");
                HexEncoding.encode2.Add(360, "x");
                HexEncoding.encode2.Add(380, "y");
                HexEncoding.encode2.Add(400, "z");
            }
            StringBuilder stringBuilder = new StringBuilder();
            string        lastChar      = hexData.Substring(0, 1);
            int           countChar1    = 0;
            string        str           = "";

            for (int startIndex = 1; startIndex < hexData.Length; ++startIndex)
            {
                if (hexData.Substring(startIndex, 1) == lastChar)
                {
                    ++countChar1;
                }
                else if (countChar1 > 0)
                {
                    if (countChar1 > 419)
                    {
                        int num1       = 2;
                        int num2       = countChar1;
                        int countChar2 = 0;
                        while (countChar1 / num1 > 419)
                        {
                            ++num1;
                        }
                        do
                        {
                            if (countChar2 == 0)
                            {
                                countChar2 = countChar1 / num1;
                                num2       = countChar1 - num1;
                            }
                            else
                            {
                                if (countChar2 > num2)
                                {
                                    countChar2 = num2;
                                }
                                num2 -= countChar2;
                            }
                            str += HexEncoding.getEncodedString(countChar2, lastChar);
                        }while (num2 > 0);
                    }
                    else
                    {
                        str = HexEncoding.getEncodedString(countChar1, lastChar);
                    }
                    stringBuilder.Append(str);
                    countChar1 = 0;
                    lastChar   = hexData.Substring(startIndex, 1);
                }
            }
            return(stringBuilder.ToString());
        }