Skip to content

Commit

Permalink
Add BitLength support
Browse files Browse the repository at this point in the history
  • Loading branch information
NaroZeol committed Jun 2, 2024
1 parent 929d735 commit 469974e
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 7 deletions.
60 changes: 54 additions & 6 deletions src/ConvertHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,15 @@ private string BinToLittleEndian(string bin)
}
public ConvertResult Dec2Hex(string dec, bool upper)
{
string raw = System.Convert.ToString(System.Convert.ToInt64(dec, 10), 16);
string raw = settings.BitLength switch
{
8 => System.Convert.ToString(System.Convert.ToSByte(dec, 10), 16),
16 => System.Convert.ToString(System.Convert.ToInt16(dec, 10), 16),
32 => System.Convert.ToString(System.Convert.ToInt32(dec, 10), 16),
64 => System.Convert.ToString(System.Convert.ToInt64(dec, 10), 16),
_ => System.Convert.ToString(System.Convert.ToInt64(dec, 10), 16)
};

if (settings.InputEndian == SettingsHelper.BigEndian)
{
raw = HexToBigEndian(raw);
Expand All @@ -80,7 +88,15 @@ public ConvertResult Dec2Hex(string dec, bool upper)
}
public ConvertResult Dec2Bin(string dec)
{
string raw = System.Convert.ToString(System.Convert.ToInt64(dec, 10), 2);
string raw = settings.BitLength switch
{
8 => System.Convert.ToString(System.Convert.ToSByte(dec, 10), 2),
16 => System.Convert.ToString(System.Convert.ToInt16(dec, 10), 2),
32 => System.Convert.ToString(System.Convert.ToInt32(dec, 10), 2),
64 => System.Convert.ToString(System.Convert.ToInt64(dec, 10), 2),
_ => System.Convert.ToString(System.Convert.ToInt64(dec, 10), 2)
};

if (settings.InputEndian == SettingsHelper.BigEndian)
{
raw = BinToBigEndian(raw);
Expand Down Expand Up @@ -108,7 +124,14 @@ public ConvertResult Hex2Dec(string hex)
{
hex = HexToLittleEndian(hex); // Convert to little endian
}
string raw = System.Convert.ToInt64(hex, 16).ToString();
string raw = settings.BitLength switch
{
8 => System.Convert.ToSByte(hex, 16).ToString(),
16 => System.Convert.ToInt16(hex, 16).ToString(),
32 => System.Convert.ToInt32(hex, 16).ToString(),
64 => System.Convert.ToInt64(hex, 16).ToString(),
_ => System.Convert.ToInt64(hex, 16).ToString(),
};
return new ConvertResult(raw, raw);
}

Expand All @@ -118,7 +141,16 @@ public ConvertResult Hex2Bin(string hex)
{
hex = HexToLittleEndian(hex);
}
string raw = System.Convert.ToString(System.Convert.ToInt64(hex, 16), 2);

string raw = settings.BitLength switch
{
8 => System.Convert.ToString(System.Convert.ToSByte(hex, 16), 2),
16 => System.Convert.ToString(System.Convert.ToInt16(hex, 16), 2),
32 => System.Convert.ToString(System.Convert.ToInt32(hex, 16), 2),
64 => System.Convert.ToString(System.Convert.ToInt64(hex, 16), 2),
_ => System.Convert.ToString(System.Convert.ToInt64(hex, 16), 2)
};

if (settings.OutputEndian == SettingsHelper.BigEndian)
{
raw = BinToBigEndian(raw);
Expand Down Expand Up @@ -146,7 +178,14 @@ public ConvertResult Bin2Dec(string bin)
{
bin = BinToLittleEndian(bin);
}
string raw = System.Convert.ToInt64(bin, 2).ToString();
string raw = settings.BitLength switch
{
8 => System.Convert.ToSByte(bin, 2).ToString(),
16 => System.Convert.ToInt16(bin, 2).ToString(),
32 => System.Convert.ToInt32(bin, 2).ToString(),
64 => System.Convert.ToInt64(bin, 2).ToString(),
_ => System.Convert.ToInt64(bin, 2).ToString()
};
return new ConvertResult(raw, raw);
}

Expand All @@ -156,7 +195,16 @@ public ConvertResult Bin2Hex(string bin, bool upper)
{
bin = BinToLittleEndian(bin);
}
string raw = System.Convert.ToString(System.Convert.ToInt64(bin, 2), 16);

string raw = settings.BitLength switch
{
8 => System.Convert.ToString(System.Convert.ToSByte(bin, 2), 16),
16 => System.Convert.ToString(System.Convert.ToInt16(bin, 2), 16),
32 => System.Convert.ToString(System.Convert.ToInt32(bin, 2), 16),
64 => System.Convert.ToString(System.Convert.ToInt64(bin, 2), 16),
_ => System.Convert.ToString(System.Convert.ToInt64(bin, 2), 16)
};

if (settings.OutputEndian == SettingsHelper.BigEndian)
{
raw = HexToBigEndian(raw);
Expand Down
11 changes: 10 additions & 1 deletion src/HexInspector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,23 @@ private List<Result> ProduceResults(string queryStr)
conversions.Add((converter.Dec2Bin(queryStr), "BIN"));
}

string SubTitleAddition =
$" ({settings.BitLength switch
{
8 => "BYTE",
16 => "WORD",
32 => "DWORD",
64 => "QWORD",
_ => "BYTE"
}},{(settings.OutputEndian ? "Little Endian" : "Big Endian")})";
foreach ((Convert.ConvertResult res, string type) in conversions)
{
results.Add
(
new Result
{
Title = res.Format,
SubTitle = type + (settings.OutputEndian ? " (Little" : " (Big") + " Endian)",
SubTitle = type + SubTitleAddition,
IcoPath = IconPath,
Action = (e) =>
{
Expand Down

0 comments on commit 469974e

Please sign in to comment.