计算数组的位/字节大小 [英] Count bit/byte size of array

查看:233
本文介绍了计算数组的位/字节大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Javascript中有一个包含很多子数组的数组。计算数组保存的位数/字节数的最佳/最简单方法是什么?我要将数组发送到我的PHP服务器,它只能是5kB大。

I have an array in Javascript that has lot of sub arrays. What would be the best/simplest way to count how many bits/bytes the array holds? I'm gonna send the array to my PHP server, and it can only be 5kB big.

有没有本机方法?我不是很熟悉比特。如果我理解正确1个字符适合8b / 1B(虽然它明显取决于编码)。最好的方法是循环遍历所有数组并计算字符吗?

Is there a native method for this? I'm not so very well acquainted with bits. If I understood it correctly 1 character fits in 8b/1B (although it depends on the encoding obviously). Would the best way be to loop through all arrays and count the characters?

推荐答案

首先你必须将数组转换为字符串用于将数据传输到服务器的表示。数组的大小无关紧要,因为它将是要传输的字符串。根据序列化该数组的方式,大小差异可能非常大。

First you have to convert the array to the string representation used to transmit the data to the server. The size of the array does not matter since it will be that string that will be transmitted. Depending on the way you serialize that array, the size difference can be very significant.

序列化数组的可能选项是 jQuery.param() 方法或 JSON.stringify() 。您还可以构建自己的方法来转换值,以便PHP代码可以理解它。

Possible options to serialize the array are jQuery.param() method or JSON.stringify(). You can also build your own method that converts the values so that your PHP code can understand it.

获得该字符串后,您将获取 stringValue .length * 8 获取大小,假设a)值为ASCII或b)您对所有值进行了url编码,以便将Unicode字符转换为ASCII。 * 8 可以获取这些位,但是因为你提到限制是5kB - 那些很可能是字节所以你跳过乘法。

After you have that string, you take stringValue.length * 8 to get the size, assuming that a) the values are ASCII or b) you url-encoded all values so that Unicode characters are transformed into ASCII. *8 is there to get the bits, but since you mention that the limit is 5kB - those most probably are bytes so you skip the multiplication.

这篇关于计算数组的位/字节大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆