Skip to content

Commit 38dcf6f

Browse files
author
Chris Ward
committed
Move FAQ point string and byte type details
1 parent 94ccf18 commit 38dcf6f

2 files changed

Lines changed: 8 additions & 26 deletions

File tree

docs/frequently-asked-questions.rst

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -199,29 +199,6 @@ See `ping.sol <https://github.com/fivedogit/solidity-baby-steps/blob/master/cont
199199
and `pong.sol <https://github.com/fivedogit/solidity-baby-steps/blob/master/contracts/45_pong.sol>`_.
200200

201201

202-
203-
What is the relationship between ``bytes32`` and ``string``? Why is it that ``bytes32 somevar = "stringliteral";`` works and what does the saved 32-byte hex value mean?
204-
========================================================================================================================================================================
205-
206-
The type ``bytes32`` can hold 32 (raw) bytes. In the assignment ``bytes32 samevar = "stringliteral";``,
207-
the string literal is interpreted in its raw byte form and if you inspect ``somevar`` and
208-
see a 32-byte hex value, this is just ``"stringliteral"`` in hex.
209-
210-
The type ``bytes`` is similar, only that it can change its length.
211-
212-
Finally, ``string`` is basically identical to ``bytes`` only that it is assumed
213-
to hold the UTF-8 encoding of a real string. Since ``string`` stores the
214-
data in UTF-8 encoding it is quite expensive to compute the number of
215-
characters in the string (the encoding of some characters takes more
216-
than a single byte). Because of that, ``string s; s.length`` is not yet
217-
supported and not even index access ``s[2]``. But if you want to access
218-
the low-level byte encoding of the string, you can use
219-
``bytes(s).length`` and ``bytes(s)[2]`` which will result in the number
220-
of bytes in the UTF-8 encoding of the string (not the number of
221-
characters) and the second byte (not character) of the UTF-8 encoded
222-
string, respectively.
223-
224-
225202
Can a contract pass an array (static size) or string or ``bytes`` (dynamic size) to another contract?
226203
=====================================================================================================
227204

docs/types.rst

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -302,12 +302,18 @@ a non-rational number).
302302
uint128 a = 1;
303303
uint128 b = 2.5 + a + 0.5;
304304

305-
.. index:: literal, literal;string, string
305+
.. index:: literal, literal;string, string, string length, string size
306306

307307
String Literals
308308
---------------
309309

310-
String literals are written with either double or single-quotes (``"foo"`` or ``'bar'``). They do not imply trailing zeroes as in C; ``"foo"`` represents three bytes not four. As with integer literals, their type can vary, but they are implicitly convertible to ``bytes1``, ..., ``bytes32``, if they fit, to ``bytes`` and to ``string``.
310+
String literals are written with either double or single-quotes (``"foo"`` or ``'bar'``). They do not imply trailing zeroes as in C; ``"foo"`` represents three bytes not four. As with integer literals, their type can vary, but they are implicitly convertible to ``bytes1``, ..., ``bytes32``, if they fit, to ``bytes`` and to ``string``. For example, with ::
311+
312+
bytes32 samevar = "stringliteral";
313+
314+
The string literal is interpreted in its raw byte form and if you inspect ``somevar``, you see a 32-byte hex value, which is the ``"stringliteral"`` in hex.
315+
316+
Using a ``string`` type is basically identical to ``bytes``, but it is assumed to hold the UTF-8 encoding of a real string. Since ``string`` UTF-8 encoding of some characters takes more than a single byte, it is quite expensive to compute the number of characters in the string. Because of this, solidity does not support length methods ``string s; s.length``, or index access ``s[2]``. If you want to access the low-level byte encoding of the string, you can use ``bytes(s).length`` and ``bytes(s)[2]`` which returns the number of bytes in the UTF-8 encoding of the string (not the number of characters) and the second byte (not character) of the UTF-8 encoded string, respectively.
311317

312318
String literals support escape characters, such as ``\n``, ``\xNN`` and ``\uNNNN``. ``\xNN`` takes a hex value and inserts the appropriate byte, while ``\uNNNN`` takes a Unicode codepoint and inserts an UTF-8 sequence.
313319

@@ -989,4 +995,3 @@ converted to a matching size. This makes alignment and padding explicit::
989995
uint16 x = 0xffff;
990996
bytes32(uint256(x)); // pad on the left
991997
bytes32(bytes2(x)); // pad on the right
992-

0 commit comments

Comments
 (0)