3

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.

Dawson Goodell
  • 418
  • 2
  • 5
  • 15
  • Depending on the encoding, all characters could be the same number of bytes, but not necessarily (I'm not really sure what encodings have what size for bytes; I believe ASCII uses 1 byte per character), so I would not recommend just counting and multiplying – codersarepeople Aug 29 '10 at 23:25
  • 1
    This related question contains a good code sample in Borgar's answer: http://stackoverflow.com/questions/1240408/reading-bytes-from-javascript-string – BalusC Aug 29 '10 at 23:32
  • 1
    ASCII, Win 1252 and ISO-8859-1 have 1 byte per character, UTF-8 has 1,2,3 or 4 bytes per character and UTF-16 has 2 or 4 bytes per character. Which encoding do you need a byte size for? – Alohci Aug 30 '10 at 00:50
  • What I'm actually doing is having a user paste in Javascript to determine the download size if that Javascript were to be placed on their website. – Dawson Goodell Aug 30 '10 at 02:40
  • 1
    That would vary depending on the compression applied to it. – Quentin Apr 16 '11 at 13:17

1 Answers1

1

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 Length 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 are still confused, I could post the full code, Hope it helps! =)

Mazen Elkashef
  • 3,430
  • 6
  • 44
  • 72