C#コードで16進値の範囲を試す方法は?


私は次のC#コードを持っています:-

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();
		}

	}
}

C# でコーディングして、このコードで 0x00 から 0xFF の 16 進数値を試すにはどうすればよいですか? input_byte == 、つまり、各 16 進値の範囲を順番に試すには、投稿されたコードに何を入力すればよいですか? C#の範囲についてインターネットで調べましたが、適切なものが見つかりませんでした。 どんな助けでも大歓迎です よろしく エディ・ウィンチ

私が試したこと:

input_byte の値を変更してみました。 しかし、すべての個々の 16 進数値のすべての可能な組み合わせを試すことができる必要があります。つまり、255 または 256 のいずれかです。コードで input_byte == と表示されている場所。

解決策 1

使う switch ブロック:

C#
switch (input_byte)
{
case 0xFF:
// code for FF
    break;
case 0xFE:
// code for FE
    break;
case 0xFD:
// code for FD
    break;
default:
// code for all other values
    break;
}

[edit]

を追加しました break ステートメント; ありがとうOG。
[/edit]

解決策 2

リチャードの言うことに追加するには、あなたも必要です break 各セットの最後にあるステートメント case コード:

C#
switch (input_byte)
    {
    case 0xFF:
        ... code for FF...
        break;
    case 0xFE:
        ... code for FE ...
        break;
    case 0xFD:
        ... code for FD ...
        break;
    default:
        ... code for all other values ...
        break;
    }

解決策 3

コードで16進表記を使用している理由がわかりません。 2 文字の 16 進数は、base 16 形式で表現された単純なバイトです。 1 バイトを 2 つの 4 ビット ニブルとして表示するため、ビット演算子を使用する場合に便利です。 あなたを助けるかもしれない範囲を選択するためのテクニックがあります。 のリストを使用します。 Func を返すデリゲート isHandled bool. これらの線に沿った何か。

C#
//construct a list to hold the required functions
 List<Func<int, int, bool>> list = new();
//an example function that can be  added to the list
private bool HandleA(int highRange, int lowRange)
           {
                 if(highRange<=int.MaxValue && lowRange >=75)
                  {
                  //do something
                  Console.WriteLine("Handled A");
                  return true;
                  }
                //let the next delegate try to handle it
                return false;

           }
//Iterate over the list until one of the functions is able to
//handle the parameters
 int h=77;
 int l=75;
              foreach (var func in list)
                {
                    if (func(h, l) == true)
                    {
                        Console.WriteLine($"Handled {h} {l}");
                        break;
                    }

                }

解決策 4

これは、たとえばコードのこれらの部分で機能しますか? :-

C#
<pre>switch(input_byte)
				{
					case when (input_byte >= 0x00 && 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;
					break;

				case when (input_byte >= 0x00 && input_byte <= 0xFF):
					//=================
					// 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;
                    break;



Source link

コメント

タイトルとURLをコピーしました