UNHEXING to integer (as opposed to a string)...
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
Re: UNHEXING to integer (as opposed to a string)...
Oops: This little bit doesn't make sense, logically: "The hex string is in little endian byte order (since it represents a 2-byte integer or short)"... What I was trying to say is that the integer that this hex string represents is multi-byte, so the order of the hex bytes **is** important... but I ensure that the bytes are in little endian order when sending the string to the server, and the server itself is little endian, so endianness is not the problem with this conversion that I'm trying to do. -- MySQL General Mailing List For list archives: http://lists.mysql.com/mysql To unsubscribe:http://lists.mysql.com/mysql?unsub=arch...@jab.org
Re: UNHEXING to integer (as opposed to a string)...
Oops: This little bit doesn't make sense, logically: "The hex string is in little endian byte order (since it represents a 2-byte integer or short)"... What I was trying to say is that the integer that this hex string represents is multi-byte, so the order of the hex bytes **is** important... but I ensure that the bytes are in little endian order when sending the string to the server, and the server itself is little endian, so endianness is not the problem with this conversion that I'm trying to do. -- MySQL General Mailing List For list archives: http://lists.mysql.com/mysql To unsubscribe:http://lists.mysql.com/mysql?unsub=arch...@jab.org
Re: UNHEXING to integer (as opposed to a string)...
SET @string := '1a4b'; SELECT CONV(@string,16,10); +-+ | CONV(@string,16,10) | +-+ | 6731 Thanks. That works perfectly. Much appreciated. On February 28, 2010, Michael Dykman wrote: It's still a string, but this should accomplish what you are after. No endian issues apply here. SET @string := '1a4b'; SELECT CONV(@string,16,10); +-+ | CONV(@string,16,10) | +-+ | 6731 - michael dykman On Sun, Feb 28, 2010 at 12:08 PM, mysql.l...@juun.com wrote: Oops: This little bit doesn't make sense, logically: "The hex string is in little endian byte order (since it represents a 2-byte integer or short)"... What I was trying to say is that the integer that this hex string represents is multi-byte, so the order of the hex bytes **is** important... but I ensure that the bytes are in little endian order when sending the string to the server, and the server itself is little endian, so endianness is not the problem with this conversion that I'm trying to do. -- MySQL General Mailing List For list archives: http://lists.mysql.com/mysql To unsubscribe: http://lists.mysql.com/mysql?unsub=mdyk...@gmail.com -- - michael dykman - mdyk...@gmail.com May the Source be with you. -- MySQL General Mailing List For list archives: http://lists.mysql.com/mysql To unsubscribe:http://lists.mysql.com/mysql?unsub=arch...@jab.org