How to try a range of hex values in C# code ?

Joined
Nov 19, 2022
Messages
1
Reaction score
0
I have the following C# Code :-


C#:
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;


namespace Unpac {
    class Program {

        //_____________________________________________________________________
        static string        input_filename_no_ext;
        static int            current_output_index;
        static string        output_directory;
        static List<byte>    output_buffer;
        static int            image_height;
        static int            line_min_length;
        static int            line_max_length;

        //_____________________________________________________________________
        static void Main(string[] args) {

            int?        forced_image_width;
            string        input_fullpath;
            string        input_directory;
            string        input_filename;
            byte[]        input_data;
            long        input_max_valid_position;
            long        current_input_position;
            byte        input_byte;
            byte        RLE_sumador;
            int            count;
            byte        b;
            int            n;
            bool        RLE_FC_swap;
            byte        b2;
            int            width_current;


            if(args.Length<2) {
                Console.WriteLine("Use: unpac.exe <InputFile.pac> <OutputDir> [ImageWidth]");
                return;
            }

            if(args.Length>2) {
                forced_image_width = int.Parse(args[2]);
            } else {
                forced_image_width = null;
            }
            
            try {
                input_fullpath        = Path.GetFullPath(args[0]);
                input_directory        = Path.GetDirectoryName(input_fullpath);
                input_filename        = Path.GetFileName(input_fullpath);

                input_fullpath        = Path.Combine(input_directory, input_filename);
                if(!File.Exists(input_fullpath)) throw new Exception();

                int p = input_filename.LastIndexOf('.');
                if(p==-1) {
                    input_filename_no_ext = input_filename;
                } else {
                    input_filename_no_ext = input_filename.Substring(0, p);
                }

            } catch {
                Console.WriteLine("ERROR: invalid input file");
                return;
            }

            try {
                output_directory = Path.GetFullPath(args[1]);
                if(!Directory.Exists(output_directory)) {
                    Directory.CreateDirectory(output_directory);
                }
            } catch {
                Console.WriteLine("ERROR: invalid output directory");
                return;
            }

            try {
                input_data = File.ReadAllBytes(input_fullpath);
            } catch {
                Console.WriteLine("ERROR: cannot read from input file");
                return;
            }

            Console.WriteLine($"{input_filename}");

            input_max_valid_position    = input_data.Length - 1;
            current_input_position        = 0;
            current_output_index        = 1;
            output_buffer                = new List<byte>();

            RLE_sumador                    = 0;
            line_min_length                = int.MaxValue;
            line_max_length                = 0;
            width_current                = 0;
            image_height                = 0;

            while(current_input_position <= input_max_valid_position) {

                input_byte = input_data[current_input_position];
                current_input_position++;

                if(input_byte == 0xFF) {
                    //==============
                    // Fin de chunk
                    //==============
                    Save();
                    current_output_index++;

                    RLE_sumador            = 0;
                    line_min_length        = int.MaxValue;
                    line_max_length        = 0;
                    width_current        = 0;
                    image_height        = 0;

                } else if(input_byte == 0xFE) {
                    //=================
                    // Siguiente linea
                    //=================
                    if(width_current < line_min_length) line_min_length = width_current;
                    if(width_current > line_max_length) line_max_length = width_current;

                    if(forced_image_width!=null) {
                        count = forced_image_width.Value - width_current;
                        if(count > 0) {
                            for(n=0; n<count; n++) {
                                output_buffer.Add(0);
                            }
                        }
                    }

                    image_height++;
                    width_current = 0;

                } else if(input_byte == 0xFD) {
                    count = (int) (input_data[current_input_position]) + 1;
                    current_input_position++;

                    b = input_data[current_input_position];
                    current_input_position++;

                    for(n=0; n<count; n++) {
                        output_buffer.Add(b);
                    }

                    width_current += count;

                } else if(input_byte == 0xFC) {
                    b = input_data[current_input_position];
                    current_input_position++;

                    b2 = (byte)(b + 1);

                    count = (int) (input_data[current_input_position]) + 1;
                    current_input_position++;

                    RLE_FC_swap = false;
                    for(n=0; n<count; n++) {
                        output_buffer.Add(
                            RLE_FC_swap ? b2 : b
                        );
                        RLE_FC_swap = !RLE_FC_swap;
                    }

                    width_current += count;

                } else if(input_byte == 0xFB) {
                    RLE_sumador = input_data[current_input_position];
                    current_input_position++;

                } else {
                    b = (byte)(input_byte >> 2);
                    b += RLE_sumador;
                    count = (input_byte & 3) + 1;

                    for(n=0; n<count; n++) {
                        output_buffer.Add(b);
                    }

                    width_current += count;
                }
            }

            Save();
        }

        //_____________________________________________________________________
        static void Save() {

            if(output_buffer.Count == 0) return;

            string    output_filename;
            string    output_fullpath;
            byte[]    output_array;
            int        output_length;
            bool    is_valid_image;


            output_filename    = $"{input_filename_no_ext}.{current_output_index:D3}";
            output_fullpath    = Path.Combine(output_directory, output_filename);
            output_array    = output_buffer.ToArray();
            output_length    = output_array.Length;
            is_valid_image    = false;


            if(image_height > 0) {
                if(line_min_length != line_max_length) {
                    Console.WriteLine($" -> {output_filename} ({output_length}) (min width:{line_min_length}) (max width:{line_max_length}) (height:{image_height})");
                } else {
                    Console.WriteLine($" -> {output_filename} ({output_length}) (width:{line_min_length}) (height:{image_height})");
                    is_valid_image = true;
                }
            } else {
                Console.WriteLine($" -> {output_filename} ({output_length})");
            }

            try {
                if(is_valid_image) {
                    Bitmap.Save(output_fullpath + ".bmp", output_array, line_min_length, image_height);
                } else {
                    File.WriteAllBytes(output_fullpath, output_array);
                }
            } catch {
                Console.WriteLine("ERROR: cannot save to output file");
            }

            output_buffer.Clear();
        }

    }
}

How can I code in C# to try the Hex values, 0x00 to 0xFF in this code for input_byte == , i.e what should I type in the posted Code, to try the range each hex value in turn ? I looked on the internet about ranges in c# but I didn't find anything suitable, to help me with.

What I have tried:

I have tried altering the input_byte values. But I need to be able, to try all possible combineations, of all individual Hex Values i.e. there are either 255 or 256. Where it says input_byte == in the Code. I wrote, and also copy and pasted 4x256, else if Statements in my latest modification, of my Program.cs Code. But I don't Think the Compiled Program, is doing what I wan't to achieve.

I asked a person who has a knowledge of C# he said maybe I could try the following in a for loop ? :-

C#:
for(int nSomething=0; nSomething<256: n++) { doSomething(nSomething);}

Could someone suggest a modification I could make, to my original Code, using the above C# code, to achieve what I want ? obviously the Something would be replaced with other text ? I am okay with all output Files, being saved to the same Folder. There is a seperate C# Code called Bitmap.cs, which makes up the unpac program. any help would be much appreciated. Eddie Winch

Here a a few lines, of the 4x256 else if Statements in my Code :-

C#:
if (input_byte == 0x00)
                  {
                    //==============
                    // Fin de chunk
                    //==============
                    Save();
                    current_output_index++;

                    RLE_sumador = 0;
                    line_min_length = int.MaxValue;
                    line_max_length = 0;
                    width_current = 0;
                    image_height = 0;
                    }
                 else if (input_byte == 0x01)
                    {
                    //==============
                    // Fin de chunk
                    //==============
                    Save();
                    current_output_index++;

                    RLE_sumador = 0;
                    line_min_length = int.MaxValue;
                    line_max_length = 0;
                    width_current = 0;
                    image_height = 0;

                }
                else if (input_byte == 0x02)
                {
                    //==============
                    // Fin de chunk
                    //==============
                    Save();
                    current_output_index++;

                    RLE_sumador = 0;
                    line_min_length = int.MaxValue;
                    line_max_length = 0;
                    width_current = 0;
                    image_height = 0;

                }
                else if (input_byte == 0x03)
                {
                    //==============
                    // Fin de chunk
                    //==============
                    Save();
                    current_output_index++;

                    RLE_sumador = 0;
                    line_min_length = int.MaxValue;
                    line_max_length = 0;
                    width_current = 0;
                    image_height = 0;

                }
                else if (input_byte == 0x04)
                {
                    //==============
                    // Fin de chunk
                    //==============
                    Save();
                    current_output_index++;

                    RLE_sumador = 0;
                    line_min_length = int.MaxValue;
                    line_max_length = 0;
                    width_current = 0;
                    image_height = 0;

                }
                else if (input_byte == 0x05)
                {
                    //==============
                    // Fin de chunk
                    //==============
                    Save();
                    current_output_index++;

                    RLE_sumador = 0;
                    line_min_length = int.MaxValue;
                    line_max_length = 0;
                    width_current = 0;
                    image_height = 0;
                    

if (input_byte == 0x00)
                {    //=================
                     // Siguiente linea
                     //=================
                     if (width_current < line_min_length) line_min_length = width_current;
                     if (width_current > line_max_length) line_max_length = width_current;

                     if (forced_image_width != null)
                     {
                         count = forced_image_width.Value - width_current;
                         if (count > 0)
                         {
                             for (n = 0; n < count; n++)
                             {
                                 output_buffer.Add(0);
                             }
                         }
                     }

                     image_height++;
                     width_current = 0;

                 }
                 else if (input_byte == 0x01)
                 {
                     // Siguiente linea
                     //=================
                     if (width_current < line_min_length) line_min_length = width_current;
                     if (width_current > line_max_length) line_max_length = width_current;

                     if (forced_image_width != null)
                     {
                         count = forced_image_width.Value - width_current;
                         if (count > 0)
                         {
                             for (n = 0; n < count; n++)
                             {
                                 output_buffer.Add(0);
                             }
                         }
                     }

                     image_height++;
                     width_current = 0;

                 }
                 else if (input_byte == 0x02)
                 {
                     // Siguiente linea
                     //=================
                     if (width_current < line_min_length) line_min_length = width_current;
                     if (width_current > line_max_length) line_max_length = width_current;

                     if (forced_image_width != null)
                     {
                         count = forced_image_width.Value - width_current;
                         if (count > 0)
                         {
                             for (n = 0; n < count; n++)
                             {
                                 output_buffer.Add(0);
                             }
                         }
                     }

                     image_height++;
                     width_current = 0;

                 }
                 else if (input_byte == 0x03)
                 {
                     // Siguiente linea
                     //=================
                     if (width_current < line_min_length) line_min_length = width_current;
                     if (width_current > line_max_length) line_max_length = width_current;

                     if (forced_image_width != null)
                     {
                         count = forced_image_width.Value - width_current;
                         if (count > 0)
                         {
                             for (n = 0; n < count; n++)
                             {
                                 output_buffer.Add(0);
                             }
                         }
                     }

                     image_height++;
                     width_current = 0;

                 }
                 else if (input_byte == 0x04)
                 {
                     // Siguiente linea
                     //=================
                     if (width_current < line_min_length) line_min_length = width_current;
                     if (width_current > line_max_length) line_max_length = width_current;

                     if (forced_image_width != null)
                     {
                         count = forced_image_width.Value - width_current;
                         if (count > 0)
                         {
                             for (n = 0; n < count; n++)
                             {
                                 output_buffer.Add(0);
                             }
                         }
                     }

                     image_height++;
                     width_current = 0;

                 }
                 else if (input_byte == 0x05)
                 {
                     // Siguiente linea
                     //=================
                     if (width_current < line_min_length) line_min_length = width_current;
                     if (width_current > line_max_length) line_max_length = width_current;

                     if (forced_image_width != null)
                     {
                         count = forced_image_width.Value - width_current;
                         if (count > 0)
                         {
                             for (n = 0; n < count; n++)
                             {
                                 output_buffer.Add(0);
                             }
                         }
                     }

                     image_height++;
                     width_current = 0;

etc
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top