Keine Cache-Version

Caching deaktiviert Standardeinstellung für diese Seite:aktiviert (code DEF204)
Wenn die Anzeige zu langsam ist, können Sie den Benutzermodus deaktivieren, um die zwischengespeicherte Version anzuzeigen.

Rechercher dans le manuel MySQL

9.1.4 Hexadecimal Literals

Hexadecimal literal values are written using X'val' or 0xval notation, where val contains hexadecimal digits (0..9, A..F). Lettercase of the digits and of any leading X does not matter. A leading 0x is case-sensitive and cannot be written as 0X.

Legal hexadecimal literals:

  1. X'01AF'
  2. X'01af'
  3. x'01AF'
  4. x'01af'
  5. 0x01AF
  6. 0x01af

Illegal hexadecimal literals:

X'0G'   (G is not a hexadecimal digit)
0X01AF  (0X must be written as 0x)

Values written using X'val' notation must contain an even number of digits or a syntax error occurs. To correct the problem, pad the value with a leading zero:

  1. mysql> SET @s = X'FFF';
  2. ERROR 1064 (42000): You have an error in your SQL syntax;
  3. check the manual that corresponds to your MySQL server
  4. version for the right syntax to use near 'X'FFF''
  5.  
  6. mysql> SET @s = X'0FFF';
  7. Query OK, 0 rows affected (0.00 sec)

Values written using 0xval notation that contain an odd number of digits are treated as having an extra leading 0. For example, 0xaaa is interpreted as 0x0aaa.

By default, a hexadecimal literal is a binary string, where each pair of hexadecimal digits represents a character:

  1. mysql> SELECT X'4D7953514C', CHARSET(X'4D7953514C');
  2. +---------------+------------------------+
  3. | X'4D7953514C' | CHARSET(X'4D7953514C') |
  4. +---------------+------------------------+
  5. | MySQL         | binary                 |
  6. +---------------+------------------------+
  7. mysql> SELECT 0x5461626c65, CHARSET(0x5461626c65);
  8. +--------------+-----------------------+
  9. | 0x5461626c65 | CHARSET(0x5461626c65) |
  10. +--------------+-----------------------+
  11. | Table        | binary                |
  12. +--------------+-----------------------+

A hexadecimal literal may have an optional character set introducer and COLLATE clause, to designate it as a string that uses a particular character set and collation:

  1. [_charset_name] X'val' [COLLATE collation_name]

Examples:

  1. SELECT _latin1 X'4D7953514C';
  2. SELECT _utf8 0x4D7953514C COLLATE utf8_danish_ci;

The examples use X'val' notation, but 0xval notation permits introducers as well. For information about introducers, see Section 10.3.8, “Character Set Introducers”.

In numeric contexts, MySQL treats a hexadecimal literal like a BIGINT (64-bit integer). To ensure numeric treatment of a hexadecimal literal, use it in numeric context. Ways to do this include adding 0 or using CAST(... AS UNSIGNED). For example, a hexadecimal literal assigned to a user-defined variable is a binary string by default. To assign the value as a number, use it in numeric context:

  1. mysql> SET @v1 = X'41';
  2. mysql> SET @v2 = X'41'+0;
  3. mysql> SET @v3 = CAST(X'41' AS UNSIGNED);
  4. mysql> SELECT @v1, @v2, @v3;
  5. +------+------+------+
  6. | @v1  | @v2  | @v3  |
  7. +------+------+------+
  8. | A    |   65 |   65 |
  9. +------+------+------+

An empty hexadecimal value (X'') evaluates to a zero-length binary string. Converted to a number, it produces 0:

  1. mysql> SELECT CHARSET(X''), LENGTH(X'');
  2. +--------------+-------------+
  3. | CHARSET(X'') | LENGTH(X'') |
  4. +--------------+-------------+
  5. | binary       |           0 |
  6. +--------------+-------------+
  7. mysql> SELECT X''+0;
  8. +-------+
  9. | X''+0 |
  10. +-------+
  11. |     0 |
  12. +-------+

The X'val' notation is based on standard SQL. The 0x notation is based on ODBC, for which hexadecimal strings are often used to supply values for BLOB columns.

To convert a string or a number to a string in hexadecimal format, use the HEX() function:

  1. mysql> SELECT HEX('cat');
  2. +------------+
  3. | HEX('cat') |
  4. +------------+
  5. | 636174     |
  6. +------------+
  7. mysql> SELECT X'636174';
  8. +-----------+
  9. | X'636174' |
  10. +-----------+
  11. | cat       |
  12. +-----------+

For hexadecimal literals, bit operations are considered numeric context, but bit operations permit numeric or binary string arguments in MySQL 8.0 and higher. To explicitly specify binary string context for hexadecimal literals, use a _binary introducer for at least one of the arguments:

  1. mysql> SET @v1 = X'000D' | X'0BC0';
  2. mysql> SET @v2 = _binary X'000D' | X'0BC0';
  3. mysql> SELECT HEX(@v1), HEX(@v2);
  4. +----------+----------+
  5. | HEX(@v1) | HEX(@v2) |
  6. +----------+----------+
  7. | BCD      | 0BCD     |
  8. +----------+----------+

The displayed result appears similar for both bit operations, but the result without _binary is a BIGINT value, whereas the result with _binary is a binary string. Due to the difference in result types, the displayed values differ: High-order 0 digits are not displayed for the numeric result.


Suchen Sie im MySQL-Handbuch

Deutsche Übersetzung

Sie haben gebeten, diese Seite auf Deutsch zu besuchen. Momentan ist nur die Oberfläche übersetzt, aber noch nicht der gesamte Inhalt.

Wenn Sie mir bei Übersetzungen helfen wollen, ist Ihr Beitrag willkommen. Alles, was Sie tun müssen, ist, sich auf der Website zu registrieren und mir eine Nachricht zu schicken, in der Sie gebeten werden, Sie der Gruppe der Übersetzer hinzuzufügen, die Ihnen die Möglichkeit gibt, die gewünschten Seiten zu übersetzen. Ein Link am Ende jeder übersetzten Seite zeigt an, dass Sie der Übersetzer sind und einen Link zu Ihrem Profil haben.

Vielen Dank im Voraus.

Dokument erstellt 26/06/2006, zuletzt geändert 26/10/2018
Quelle des gedruckten Dokuments:https://www.gaudry.be/de/mysql-rf-hexadecimal-literals.html

Die Infobro ist eine persönliche Seite, deren Inhalt in meiner alleinigen Verantwortung liegt. Der Text ist unter der CreativeCommons-Lizenz (BY-NC-SA) verfügbar. Weitere Informationen auf die Nutzungsbedingungen und dem Autor.

Referenzen

  1. Zeigen Sie - html-Dokument Sprache des Dokuments:en Manuel MySQL : https://dev.mysql.com/

Diese Verweise und Links verweisen auf Dokumente, die während des Schreibens dieser Seite konsultiert wurden, oder die zusätzliche Informationen liefern können, aber die Autoren dieser Quellen können nicht für den Inhalt dieser Seite verantwortlich gemacht werden.
Der Autor Diese Website ist allein dafür verantwortlich, wie die verschiedenen Konzepte und Freiheiten, die mit den Nachschlagewerken gemacht werden, hier dargestellt werden. Denken Sie daran, dass Sie mehrere Quellinformationen austauschen müssen, um das Risiko von Fehlern zu reduzieren.

Inhaltsverzeichnis Haut