c# - How to convert an integer to fixed length hex string format

String format hex padding
string-format-hex-padding.aspx

<%@ Page Language="C#" AutoEventWireup="true"%>

<!DOCTYPE html>  
<script runat="server"> 
    protected void Button1_Click(object sender, System.EventArgs e)  
    {
        //this line create an integer variable.
        int integerValue = 183455;

        Label1.Text = "integer value: " + integerValue.ToString();

        //string format number to fixed length hexadecimal format using string.format method.
        Label1.Text += "<br /><br />number in hexadecimal format: " + string.Format("{0:x}", integerValue);
        Label1.Text += "<br />hexadecimal format(0:x6) padding length 6: " + string.Format("{0:x6}", integerValue);
        Label1.Text += "<br />hexadecimal format fixed length 8: " + string.Format("{0:x8}", integerValue);
        Label1.Text += "<br />hexadecimal format pad length 10: " + string.Format("{0:x10}", integerValue);

        Label1.Text += "<br /><br />using ToString method fixed length 6: " + integerValue.ToString("x6");
    }  
</script>  
  
<html xmlns="http://www.w3.org/1999/xhtml">  
<head id="Head1" runat="server">  
    <title>c# example - string format hex padding</title>  
</head>  
<body>  
    <form id="form1" runat="server">  
    <div>  
        <h2 style="color:MidnightBlue; font-style:italic;">  
            c# example - string format hex padding
        </h2>  
        <hr width="550" align="left" color="Gainsboro" />  
        <asp:Label   
            ID="Label1"   
            runat="server"  
            Font-Size="Large"  
            >  
        </asp:Label>  
        <br /><br />
        <asp:Button   
            ID="Button1"   
            runat="server"   
            Text="string format hex padding"  
            OnClick="Button1_Click"
            Height="40"  
            Font-Bold="true"  
            />  
    </div>  
    </form>  
</body>  
</html>
More c# examples