How to best calculate text bytes in TextArea

I need to find a way to accurately calculate the byte size of the text inside a specific text field. I work in .Net, so I have access to these libraries, but I would prefer a Javascript solution. How many bytes does each character cost? What will be the most efficient way to count and multiply? Or did I miss the best way?

Edit: I am trying to determine the load size of the part of Javascript that has been inserted into the text box. The closest I could find is http://bytesizematters.com/ . I don’t want to just raise my code, especially since I don’t quite understand it.

+3
source share
1 answer

I think your answer is here

Calculate.aspx:

<body>
    <form id="form1" runat="server">
    <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
    </form>
</body>

Then double-click the button in the project view to define the event handler in the code file (Calculate.aspx.cs), then add the following code to the method

    protected void Button1_Click(object sender, EventArgs e)
    {
        Label1.Text = StrToByt(TextBox1.Text).Length.ToString();
    } 

Finally, you will need to define a mysterious method that you just called "StrToByt" in the same file to define this method, which will take the string str as a parameter and set its encoding through the object "System.Text.UTF8Encoding" and then return output in the form of a string, which we will consider using the Length property

        public static byte[] StrToByt(string str)
    {
        System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();
        return encoding.GetBytes(str);
    }

PS: , - UTF-8, , "System.Text.UTF8Encoding", "System.Text." " "

, , , ! =)

+1

Source: https://habr.com/ru/post/1762193/


All Articles