String Byte Length: Difference between revisions

From Rosetta Code
Content added Content deleted
mNo edit summary
Line 1: Line 1:
{{Template:split-review}}
[http://xionny.cn/topic/tv-/ tv 26 lcd philips] [http://wedner.info/content/view/sneakers-tela.htm sneakers tela] [http://soler.net.cn/data/men-con.htm men con grossi cazzi] [http://primonona.info/content/view/netgear-pcmcia/ netgear pcmcia] [http://xionny.cn/topic/onto/ onto-] [http://veggis.org.cn/resources/articles/router-con.htm router con modem adsl] [http://joperan.org/data/iomega-hard/ iomega hard disk drive] [http://soler.net.cn/data/acquistare-in.htm acquistare in internet] [http://manoke.cn/topic/dosatore-latte/ dosatore latte] [http://funkall.us/images/small/sud-carolina/ sud carolina ristorante] [http://manoke.cn/topic/gli-orsi/ gli orsi vanno in giappone] [http://soler.net.cn/data/archeologia-a.htm archeologia a perugia] [http://primonona.info/content/view/gps-windows/ gps windows ce] [http://manoke.cn/topic/free-sesso/ free sesso] [http://sneck.info/data/in-wind/ in wind] [http://budget7i.info/lib/sara-perche/ sara perche ti amo ricchi e poveri] [http://soler.net.cn/data/bmw-.htm bmw 320 blu cabrio] [http://veggis.org.cn/resources/articles/nike-calcetto.htm nike calcetto pace vapor] [http://sneck.info/data/cerco-donna/ cerco donna matura vogliosa] [http://joperan.org/data/volvo-/ volvo c70 2.4 t] [http://joperan.org/data/toner-konica/ toner konica minolta 1300] [http://manoke.cn/topic/sony-dvd/ sony dvd 92e] [http://wedner.info/content/view/party-to.htm party to] [http://manoke.cn/topic/meyday/ meyday] [http://budget7i.info/lib/usb-/ usb 2 pcmcia] [http://soler.net.cn/data/michelle-veith.htm michelle veith video] [http://veggis.org.cn/resources/articles/maria-remi.htm maria remi] [http://manoke.cn/topic/ydra/ ydra] [http://joperan.org/data/spy-collection/ spy collection] [http://soler.net.cn/data/hotel-da.htm hotel da verrazzano] [http://soler.net.cn/data/settemari-it.htm settemari it] [http://manoke.cn/topic/asus-socket/ asus socket 939] [http://polex.com.cn/resources/articles/combo-lettori.htm combo lettori dvd lg] [http://soler.net.cn/data/niente-special.htm niente special edition per the island] [http://sneck.info/data/game-boy/ game boy pokemon] [http://budget7i.info/lib/oakley-iridium/ oakley iridium] [http://xionny.cn/topic/novaja-zemlja/ novaja zemlja] [http://soler.net.cn/data/www-kazaa.htm www kazaa it] [http://primonona.info/content/view/butterfly-bubble/ butterfly bubble] [http://xionny.cn/topic/wellness-isernia/ wellness isernia] [http://polex.com.cn/resources/articles/cartoline-persone.htm cartoline persone vecchie] [http://funkall.us/images/small/cinema-emiro/ cinema emiro] [http://polex.com.cn/resources/articles/madame-claude.htm madame claude n. 3] [http://manoke.cn/topic/anello-solitario/ anello solitario] [http://klohy.info/img/styles/lisola-del.htm lisola del sogno - amori e canzoni] [http://klohy.info/img/styles/hotel-villa.htm hotel villa eden] [http://xionny.cn/topic/radio-sonnenschein/ radio sonnenschein] [http://funkall.us/images/small/niurka-y/ niurka y boby] [http://soler.net.cn/data/vacanze-madagascar.htm vacanze madagascar] [http://primonona.info/content/view/blast/ blast] [http://joperan.org/data/dance/ dance 1990] [http://klohy.info/img/styles/uzbeki.htm uzbeki] [http://wedner.info/content/view/tefal-tritatutto.htm tefal tritatutto] [http://veggis.org.cn/resources/articles/www-anna.htm www anna frank org] [http://xionny.cn/topic/mp-x/ mp3 x5 60gb] [http://budget7i.info/lib/www-oficina/ www oficina g3 com br] [http://soler.net.cn/data/mp-americano.htm mp3 americano decapitato] [http://joperan.org/data/www-life/ www life electronics com] [http://budget7i.info/lib/furioso-polka/ furioso polka] [http://xionny.cn/topic/www-dircon/ www dircon com] [http://wedner.info/content/view/fotocamera-mpx.htm fotocamera mpx] [http://sneck.info/data/fay-william/ fay, william george] [http://sneck.info/data/auto-noleggio/ auto noleggio gubbio] [http://primonona.info/content/view/bisex-uomo/ bisex uomo marche] [http://svike.info/view/new/harry-potter.htm harry potter calice fuoco] [http://funkall.us/images/small/e-in/ e in rete il primo trailer di metal gear acid] [http://primonona.info/content/view/di-logica/ di logica] [http://budget7i.info/lib/asus-barebone/ asus barebone terminator] [http://polex.com.cn/resources/articles/cartuccia-getto.htm cartuccia getto d inchiostro] [http://xionny.cn/topic/cine-box/ cine box hex keys] [http://funkall.us/images/small/giunti-cardanici/ giunti cardanici] [http://soler.net.cn/data/il-gioco.htm il gioco sudoku] [http://sneck.info/data/cd-illusioni/ cd illusioni parallele] [http://soler.net.cn/data/soldato-decapitato.htm soldato decapitato in iraq] [http://svike.info/view/new/lavatrici-.htm lavatrici 3 kg] [http://svike.info/view/new/ragazza-hannover.htm ragazza hannover] [http://wedner.info/content/view/repubblia.htm repubblia] [http://funkall.us/images/small/www-angela/ www angela grande fratello3 it] [http://primonona.info/content/view/me-namora/ me namora] [http://manoke.cn/topic/www-francesco/ www francesco renga] [http://klohy.info/img/styles/giggio.htm giggio] [http://soler.net.cn/data/televisori-lcd.htm televisori lcd hyundai l17t] [http://sneck.info/data/amd-atlon/ amd atlon 64] [http://funkall.us/images/small/dfi-lanparty/ dfi lanparty sli-d] [http://veggis.org.cn/resources/articles/brigante-musolino.htm brigante musolino] [http://wedner.info/content/view/jevoulais.htm jevoulais] [http://budget7i.info/lib/colin-monteath/ colin monteath] [http://xionny.cn/topic/hp-color/ hp color laserjet 3550] [http://svike.info/view/new/viersen.htm viersen] [http://wedner.info/content/view/c.htm c2 1 1 2003] [http://funkall.us/images/small/l-avventura/ l avventura magica] [http://klohy.info/img/styles/farmaci.htm farmaci] [http://funkall.us/images/small/lo-scandalo/ lo scandalo] [http://klohy.info/img/styles/treniitalia-it.htm treniitalia it] [http://klohy.info/img/styles/altec-lansing.htm altec lansing im4] [http://funkall.us/images/small/notebook-toshiba/ notebook toshiba sa60] [http://polex.com.cn/resources/articles/microsoft-mouse.htm microsoft mouse ottico wireless tastiera] [http://soler.net.cn/data/luv.htm luv] [http://sneck.info/data/fumo-tanta/ fumo tanta erba testo] [http://budget7i.info/lib/box-cani/ box cani] [http://sneck.info/data/carmen-consolle/ carmen consolle] [http://wedner.info/content/view/donna-cerca.htm donna cerca uomo] [http://svike.info/view/new/panasonic.htm panasonic 36] [http://veggis.org.cn/resources/articles/alnus.htm alnus] [http://manoke.cn/topic/que-vuelva/ que vuelva] [http://soler.net.cn/data/keyboard-palmari.htm keyboard palmari] [http://xionny.cn/topic/matrox-x/ matrox x100] [http://joperan.org/data/monitor-tft/ monitor tft nero] [http://sneck.info/data/jim-il/ jim il primo] [http://polex.com.cn/resources/articles/preparare-auto.htm preparare auto da rally] [http://sneck.info/data/squirting-pussy/ squirting pussy] [http://polex.com.cn/resources/articles/soli-nel.htm soli nel buio] [http://budget7i.info/lib/tiziana-pini/ tiziana pini] [http://klohy.info/img/styles/finanziamento-cesena.htm finanziamento cesena] [http://budget7i.info/lib/frammenti-di/ frammenti di paura] [http://budget7i.info/lib/teac-ar/ teac a-r600] [http://primonona.info/content/view/file-seca/ file seca wafercard] [http://xionny.cn/topic/www-trenitali/ www trenitali com] [http://soler.net.cn/data/commercio-equo.htm commercio equo solidale] [http://wedner.info/content/view/www-comune.htm www comune argenta fe it] [http://veggis.org.cn/resources/articles/muvrini.htm muvrini] [http://klohy.info/img/styles/dickens-photos.htm dickens photos] [http://svike.info/view/new/pluriball.htm pluriball] [http://klohy.info/img/styles/gobbo-di.htm gobbo di notre dame] [http://klohy.info/img/styles/tha-dead.htm tha dead case] [http://soler.net.cn/data/pescia.htm pescia] [http://primonona.info/content/view/tu-sei/ tu sei l unica donna per me] [http://soler.net.cn/data/sostitutivi-pasto.htm sostitutivi pasto] [http://funkall.us/images/small/la-frusta/ la frusta di fuoco] [http://klohy.info/img/styles/cd-dvd.htm cd dvd stampanti a sublimazione] [http://soler.net.cn/data/kilitbahir.htm kilitbahir] [http://klohy.info/img/styles/angela-.htm angela - il suo unico peccato era lamore...] [http://manoke.cn/topic/sony-zeiss/ sony zeiss] [http://budget7i.info/lib/erbe/ erbe] [http://manoke.cn/topic/inni-e/ inni e cori] [http://budget7i.info/lib/chi-fa/ chi fa pompini gratis] [http://budget7i.info/lib/everytime-we/ everytime we touch] [http://funkall.us/images/small/ferrari-/ ferrari 3400 acer portatili] [http://klohy.info/img/styles/la-notte.htm la notte se ne va] [http://klohy.info/img/styles/buoni-e.htm buoni e cattivi] [http://wedner.info/content/view/agp-x.htm agp 4x] [http://primonona.info/content/view/cannes-beach/ cannes beach residence] [http://veggis.org.cn/resources/articles/monitor-l.htm monitor l768] [http://veggis.org.cn/resources/articles/ian-fleming.htm ian fleming] [http://xionny.cn/topic/richard-widmark/ richard widmark] [http://primonona.info/content/view/tv-lcd/ tv lcd portatile] [http://klohy.info/img/styles/scarica-emule.htm scarica emule] [http://svike.info/view/new/epson-.htm epson 4000 rip professional] [http://wedner.info/content/view/cerco-chow.htm cerco chow chow] [http://budget7i.info/lib/video-a/ video a chi dice] [http://manoke.cn/topic/casa-di/ casa di] [http://joperan.org/data/www-comune/ www comune di ardea it] [http://svike.info/view/new/trans-campania.htm trans campania] [http://budget7i.info/lib/la-villanella/ la villanella] [http://manoke.cn/topic/armani-lei/ armani lei profumo] [http://wedner.info/content/view/dactrung-com.htm dactrung com] [http://joperan.org/data/she-weel/ she weel be love] [http://xionny.cn/topic/insegna-a/ insegna a led] [http://sneck.info/data/ix/ ix 30] [http://veggis.org.cn/resources/articles/linvasione.htm linvasione] [http://primonona.info/content/view/redwood-city/ redwood city] [http://soler.net.cn/data/cerco-la.htm cerco la tua voce] [http://polex.com.cn/resources/articles/quinta-estacion.htm quinta estacion] [http://funkall.us/images/small/delon-karena/ delon karena cinta] [http://wedner.info/content/view/freddie-francis.htm freddie francis] [http://joperan.org/data/resident-evil/ resident evil movie] [http://primonona.info/content/view/pic-adan/ pic adan sanchez] [http://klohy.info/img/styles/walking.htm walking] [http://klohy.info/img/styles/homosexual-pic.htm homosexual pic] [http://primonona.info/content/view/i-don/ i don t wanna kow] [http://soler.net.cn/data/tom-tom.htm tom tom 3 gps] [http://polex.com.cn/resources/articles/se-amare.htm se amare e impossibile] [http://joperan.org/data/punk-o/ punk o rama] [http://klohy.info/img/styles/volvo-s.htm volvo s40] [http://funkall.us/images/small/tipologia-curiculum/ tipologia curiculum] [http://budget7i.info/lib/sellano-agriturismo/ sellano agriturismo] [http://wedner.info/content/view/foto-porno.htm foto porno sado maso bondage] [http://funkall.us/images/small/fax-brother/ fax brother fax-t94] [http://wedner.info/content/view/biliardo.htm biliardo 2005] [http://polex.com.cn/resources/articles/blu-star.htm blu star ferries patras naxos] [http://sneck.info/data/mute-neoprene/ mute neoprene] [http://soler.net.cn/data/e-r.htm e 444r] [http://wedner.info/content/view/seagate-barracuda.htm seagate barracuda sata 250gb ncq 7200 8] [http://xionny.cn/topic/mercedes-c/ mercedes c 220 cdi esprit] [http://veggis.org.cn/resources/articles/liscate.htm liscate] [http://veggis.org.cn/resources/articles/scarpe-donna.htm scarpe donna] [http://xionny.cn/topic/pedrotti-nuda/ pedrotti nuda] [http://joperan.org/data/panocha/ panocha] [http://svike.info/view/new/logorrea.htm logorrea] [http://klohy.info/img/styles/eko-fresh.htm eko fresh] [http://soler.net.cn/data/cerco-superdotati.htm cerco superdotati] [http://manoke.cn/topic/julia-chanel/ julia chanel mpegs] [http://svike.info/view/new/prome-di.htm prome di maturita] [http://joperan.org/data/magic-combi/ magic combi] [http://primonona.info/content/view/tullio/ tullio] [http://primonona.info/content/view/jenny-from/ jenny from the block video] [http://polex.com.cn/resources/articles/esecuzione-ostaggi.htm esecuzione ostaggi in irak] [http://manoke.cn/topic/cuo/ cuo] [http://svike.info/view/new/santos-laguna.htm santos laguna] [http://soler.net.cn/data/partimpim.htm partimpim] {{Template:split-review}}
{{task}}
{{task}}


Line 65: Line 65:
char *p = (char *) string;
char *p = (char *) string;
while (*p != '\0') length ;
while (*p++ != '\0') length++;
return 0;
return 0;
Line 82: Line 82:
}
}


==[[C plus plus|C ]]==
==[[C plus plus|C++]]==
[[Category:C plus plus|C ]]
[[Category:C plus plus|C++]]


'''Standard:''' [[ISO C plus plus|ISO C ]] (AKA [[C plus plus 98|C 98]]):
'''Standard:''' [[ISO C plus plus|ISO C++]] (AKA [[C plus plus 98|C++98]]):


'''Compiler:''' g 4.0.2
'''Compiler:''' g++ 4.0.2


#include <string> // note: '''not''' <string.h>
#include <string> // note: '''not''' <string.h>
Line 114: Line 114:


'''Platform:''' [[.NET]]
'''Platform:''' [[.NET]]
'''Language Version:''' 1.0
'''Language Version:''' 1.0+


string s = "Hello, world!";
string s = "Hello, world!";

Revision as of 11:38, 28 August 2007

This task has has been split off from another task. Its programming examples are in need of review to ensure that they fit the requirements of the new task.
Task
String Byte Length
You are encouraged to solve this task according to the task description, using any language you may know.

In this task, the goal is to find the byte length of a string. This means encodings like UTF-8 may need to be handled specially, as there is not necessarily a one-to-one relationship between bytes and characters, and some languages recognize this.

For character length, see String Character Length.

4D

$length:=Length("Hello, world!")

ActionScript

myStrVar.length()

Ada

Compiler: GCC 4.1.2

Str    : String := "Hello World";
Length : constant Natural := Str'Length;

AppleScript

count of "Hello World"

AWK

From within any code block:

w=length("Hello, world!")      # static string example
x=length("Hello," s " world!") # dynamic string example
y=length($1)                   # input field example
z=length(s)                    # variable name example

Ad hoc program from command line:

echo "Hello, world!" | awk '{print length($0)}'

From executable script: (prints for every line arriving on stdin)

#!/usr/bin/awk -f
{print"The length of this line is "length($0)}

C

Standard: ANSI C (AKA C89):

Compiler: GCC 3.3.3

 #include <string.h>

 int main(void) 
 {
   const char *string = "Hello, world!";
   size_t length = strlen(string);
          
   return 0;
 }

or by hand:

 int main(void) 
 {
   const char *string = "Hello, world!";
   size_t length = 0;
   
   char *p = (char *) string;
   while (*p++ != '\0') length++;                                         
   
   return 0;
 }

or (for arrays of char only)

 #include <stdlib.h>
 
 int main(void)
 {
   char const s[] = "Hello, world!";
   size_t length = sizeof s - 1;
   
   return 0;
 }

C++

Standard: ISO C++ (AKA C++98):

Compiler: g++ 4.0.2

 #include <string> // note: not <string.h>
 
 int main()
 {
   std::string s = "Hello, world!";
   std::string::size_type length = s.length(); // option 1: In Characters/Bytes
   std::string::size_type size = s.size();     // option 2: In Characters/Bytes
   // In bytes same as above since sizeof(char) == 1
   std::string::size_type bytes = s.length() * sizeof(std::string::value_type); 
 }

For wide character strings:

 #include <string>
 
 int main()
 {
   std::wstring s = L"\u304A\u306F\u3088\u3046";
   std::wstring::size_type length = s.length() * sizeof(std::wstring::value_type); // in bytes
 }

C#

Platform: .NET Language Version: 1.0+

string s = "Hello, world!";
int clength = s.Length;  // In characters
int blength = System.Text.Encoding.GetBytes(s).length; // In Bytes.

Clean

Clean Strings are unboxed arrays of characters. Characters are always a single byte. The function size returns the number of elements in an array.

import StdEnv

strlen :: String -> Int
strlen string = size string 

Start = strlen "Hello, world!"

ColdFusion

  #len("Hello World")#

Common Lisp

  (length "Hello World")

Component Pascal

  LEN("Hello, World!")

Forth

Interpreter: ANS Forth

Strings in Forth come in two forms, neither of which are the null-terminated form commonly used in the C standard library.

Counted string

A counted string is a single pointer to a short string in memory. The string's first byte is the count of the number of characters in the string. This is how symbols are stored in a Forth dictionary.

 CREATE s ," Hello world" \ create string "s"
 s C@ ( -- length=11 )

Stack string

A string on the stack is represented by a pair of cells: the address of the string data and the length of the string data (in characters). The word COUNT converts a counted string into a stack string. The STRING utility wordset of ANS Forth works on these addr-len pairs. This representation has the advantages of not requiring null-termination, easy representation of substrings, and not being limited to 255 characters.

S" string" ( addr len)
DUP .   \ 6

Haskell

Interpreter: GHCi 6.6, Hugs

Compiler: GHC 6.6

strlen = length "Hello, world!"

IDL

Compiler: any IDL compiler should do

 length = strlen("Hello, world!")

Java

Java encodes strings in UTF-16, which represents each character with one or two 16-bit values. The length method of String objects returns the number of 16-bit values used to encode a string, so the number of bytes can be determined by doubling that number.

String s = "Hello, world!";
int byteCount = s.length() * 2;

An other way to know the byte length of a string is to explicitly specify the charset we desire.

String s = "Hello, world!";
int byteCountUTF16 = s.getByte("UTF-16").length;
int byteCountUTF8  = s.getByte("UTF-8").length;

JavaScript

JavaScript encodes strings in UTF-16, which represents each character with one or two 16-bit values. The length property of string objects gives the number of 16-bit values used to encode a string, so the number of bytes can be determined by doubling that number.

var s = "Hello, world!";
var byteCount = s.length * 2; //26

JudoScript

 //Store length of hello world in length and print it
 . length = "Hello World".length();

Lua

Interpreter: Lua 5.0 or later.

 string="Hello world"
 length=#string

mIRC Scripting Language

Interpreter: mIRC

alias stringlength { echo -a Your Name is: $len($$?="Whats your name") letters long! }

OCaml

Interpreter/Compiler: Ocaml 3.09

String.length "Hello world";;


Perl

Interpreter: perl 5.8

Strings in Perl consist of characters. Measuring the byte length therefore requires conversion to some binary representation (called encoding, both noun and verb).

use utf8; # so we can use literal characters like ☺ in source
use Encode qw(encode);

print length encode 'UTF-8', "Hello, world! ☺";
# 17. The last character takes 3 bytes, the others 1 byte each.

print length encode 'UTF-16', "Hello, world! ☺";
# 32. 2 bytes for the BOM, then 15 byte pairs for each character.

PHP

 $length = strlen('Hello, world!');

PL/SQL

DECLARE
  string VARCHAR2( 50 ) := 'Hello, world!';
  stringlength NUMBER;
BEGIN
  stringlength := length( string );
END;

Pop11

Currently Pop11 supports only strings consisting of 1-byte units. Strings can carry arbitrary binary data, so user can for example use UTF-8 (however builtin procedures will treat each byte as a single character). The length function for strings returns length in bytes:

lvars str = 'Hello, world!';
lvars len = length(str);

Python

Interpreter: Python 2.4

length = len("The length of this string will be determined")

Ruby

 string="Hello world"
 print string.length

or

 puts "Hello World".length

Scheme

 (string-length "Hello world")

Smalltalk

 string := 'Hello, world!".
 string size.

Standard ML

Interpreter: SML/NJ 110.60, Moscow ML 2.01 (January 2004)

Compiler: MLton 20061107

val strlen = size "Hello, world!";

Tcl

Basic version:

 string bytelength "Hello, world!"

or more elaborately, needs Interpreter any 8.X. Tested on 8.4.12.

 fconfigure stdout -encoding utf-8; #So that Unicode string will print correctly
 set s1 "hello, world"
 set s2 "\u304A\u306F\u3088\u3046"
 puts [format "length of \"%s\" in bytes is %d"  $s1 [string bytelength $s1]]
 puts [format "length of \"%s\" in bytes is %d"  $s2 [string bytelength $s2]]

Toka

This will include the terminating 0 in the length.

 " hello, world!" count 

UNIX Shell

With external utilities:

Interpreter: any bourne shell

 string='Hello, world!'
 length=`echo -n "$string" | wc -c | tr -dc '0-9'`
 echo $length # if you want it printed to the terminal

With SUSv3 parameter expansion modifier:

Interpreter: Almquist SHell (NetBSD 3.0), Bourne Again SHell 3.2, Korn SHell (5.2.14 99/07/13.2), Z SHell

 string='Hello, world!'
 length="${#string}"
 echo $length # if you want it printed to the terminal


VBScript

LenB(string|varname) 

Returns the number of bytes required to store a string in memory Returns null if string|varname is null

xTalk

Interpreter: HyperCard

 put the length of "Hello World"

or

 put the number of characters in "Hello World"