/    Sign up×
Community /Pin to ProfileBookmark

convert the string text to byte

is there any functions so to convert a string text “utf-8” in bytes and calculate the bytes length , how can i do this???

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@toicontienSep 12.2007 — That all depends on the character encoding you are using. Every string variable in JavaScript has a property called .length that tells you how many characters are in the string. If you are using UTF-8 on the web page, you can estimate the size in bytes by multiplying the number of characters in the string by 8 and dividing by 8 (UTF-8 is generally 8 bits per character).

Basically, multiply the number of characters by how many bits make up one character in whichever character set is being used on the page, and divide that number by 8.
number_of_chars * bits_per_char_in_character_set / 8
×

Success!

Help @chriskaza81 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.6,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...