在 C# WinForm 中将 int 转换为 Hex
Converting int to Hex in C# WinForm
所以我的 class 任务是让我们自己的算法不使用将 int 转换为十六进制的内置函数,并且在我的生活中它不会遵守。
我们文本中的示例将 24032 转换为 0x5DE0,但我得到的输出是 3210。
这是我的代码
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
List<string> errorList = new List<string>();
List<int> hexList = new List<int>();
string intInput = "";
string msgOutput = "";
private void btn_Hex_Click(object sender, EventArgs e)
{
intInput = box_Int.Text;
int toHexFunc = Validator(intInput);
ToHex(toHexFunc);
}
public void ToHex(int fromHexBtn)
{
int n = fromHexBtn;
char[] hexNum = new char[100];
int i = 0;
while (n != 0)
{
int iterTemp = n % 16;
// using ASCII table from https://www.dotnetperls.com/ascii-table
if (iterTemp < 10)
{
hexNum[i] = (char)(iterTemp + 48);
i++;
}
else
{
hexNum[i] = (char)(iterTemp + 55);
i++;
}
n = n / 16;
}
for (int j = i - 1; j >= 0; j--)
{
hexList.Add(j);
}
msgOutput = String.Join("", hexList);
lbl_Message.Text = msgOutput;
}
}
基于此https://www.permadi.com/tutorial/numDecToHex/
class Program
{
static void Main(string[] args)
{
var characters = "0123456789ABCDEF";
int number = 24032;
var hexidecimal = "";
while (number > 0)
{
var remainder = number % 16;
var res = Math.Abs(number / 16);
hexidecimal = characters[remainder] + hexidecimal;
number = res;
}
hexidecimal = "0x" + hexidecimal;
WriteLine(hexadecimal);
}
}
所以我的 class 任务是让我们自己的算法不使用将 int 转换为十六进制的内置函数,并且在我的生活中它不会遵守。
我们文本中的示例将 24032 转换为 0x5DE0,但我得到的输出是 3210。
这是我的代码
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
List<string> errorList = new List<string>();
List<int> hexList = new List<int>();
string intInput = "";
string msgOutput = "";
private void btn_Hex_Click(object sender, EventArgs e)
{
intInput = box_Int.Text;
int toHexFunc = Validator(intInput);
ToHex(toHexFunc);
}
public void ToHex(int fromHexBtn)
{
int n = fromHexBtn;
char[] hexNum = new char[100];
int i = 0;
while (n != 0)
{
int iterTemp = n % 16;
// using ASCII table from https://www.dotnetperls.com/ascii-table
if (iterTemp < 10)
{
hexNum[i] = (char)(iterTemp + 48);
i++;
}
else
{
hexNum[i] = (char)(iterTemp + 55);
i++;
}
n = n / 16;
}
for (int j = i - 1; j >= 0; j--)
{
hexList.Add(j);
}
msgOutput = String.Join("", hexList);
lbl_Message.Text = msgOutput;
}
}
基于此https://www.permadi.com/tutorial/numDecToHex/
class Program
{
static void Main(string[] args)
{
var characters = "0123456789ABCDEF";
int number = 24032;
var hexidecimal = "";
while (number > 0)
{
var remainder = number % 16;
var res = Math.Abs(number / 16);
hexidecimal = characters[remainder] + hexidecimal;
number = res;
}
hexidecimal = "0x" + hexidecimal;
WriteLine(hexadecimal);
}
}