views:

74

answers:

1

I need to find a way to accurately calculate the byte size of the text inside a particular textarea. I am working in .Net so I have access to those libraries, but I'd prefer a Javascript solution. How many bytes is each character worth? What would be the most efficient way to count and multiply? Or am I missing a better way entirely?

Edit: I'm attempting to determine the download size of a piece of Javascript that has been pasted into a textarea. The closest thing I could find to this is http://bytesizematters.com/. I don't want to just lift their code, especially since I don't fully understand it.

+1  A: 

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 on the design view to define the event handler in the code-behind 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'll have to define the mysterious method that you just called "StrToByt" in the same file define this method that will take a string str as a parameter and set it's encoding through the "System.Text.UTF8Encoding" object and then return the output as a string which we will count through Lenth property

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

P.S : I researched a little bit and found that the common encoding is UTF-8, but if you think otherwise you can just edit the encoding object instead of "System.Text.UTF8Encoding" you can type "System.Text." and you'll find in the Text name space all the encoding types available

If you was a little confused I could post the full code, HOPE IT HELPS! =)

lKashef
Works great! Thank you.
OSMman
My pleasure Osman =)
lKashef