I've tried several ways to get my hex-encoded string representation of an integer to convert back to an integer, but none of them work (the input string '1a4b' should evaluate to 6731):

(Note that this is not an endianness problem. The hex string is in little endian byte order (since it represents a 2-byte integer or short) and this is running on a little endian system. Also, method 4 (below) works with a string literal, but not a string variable, as explained in the notes for method 4...)


method 1

   SET @string := '1a4b';
   SELECT UNHEX(@string);

   +----------------+
   | UNHEX(@string) |
   +----------------+
| K | +----------------+



method 2

   SET @string := '1a4b';
   SELECT CAST(@string AS UNSIGNED);

   +---------------------------+
   | CAST(@string AS UNSIGNED) |
   +---------------------------+
| 1 | +---------------------------+



method 3

   SET @string := '1a4b';

   SELECT CONCAT('0x', @string) into @string;
   SELECT @string + 0;

   +-------------+
   | @string + 0 |
   +-------------+
| 0 | +-------------+



method 4

   SELECT CAST(X'1a4b' AS UNSIGNED);

   +---------------------------+
   | CAST(X'1a4b' AS UNSIGNED) |
   +---------------------------+
| 6731 | +---------------------------+

-- that is the correct result, but this method seems to only work with literals...



   SET @string := '1a4b';
   SELECT CAST(x...@string AS UNSIGNED);

   -- syntax error



   SET @string := '1a4b';
   SELECT CAST(X'@string' AS UNSIGNED);

   -- syntax error



   SET @string := '1a4b';
   SELECT CAST(X"@string" AS UNSIGNED);

   -- syntax error



   SET @string := '1a4b';
   SELECT CAST('x...@string' AS UNSIGNED);

   -- syntax error



   SET @string := '1a4b';
   SELECT CAST(X @string AS UNSIGNED);

   -- syntax error







... so, what can I do?






--
MySQL General Mailing List
For list archives: http://lists.mysql.com/mysql
To unsubscribe:    http://lists.mysql.com/mysql?unsub=arch...@jab.org

Reply via email to