Bug #4367 Wrong formula for the explanation of ORD() function
Submitted: 2 Jul 2004 4:08 Modified: 2 Jul 2004 6:26
Reporter: Shuichi Tamagawa Email Updates:
Status: Closed Impact on me:
None 
Category:MySQL Server: Documentation Severity:S3 (Non-critical)
Version:4.0, 4,1 OS:Linux (SuSE Linux 9.0)
Assigned to: Paul DuBois CPU Architecture:Any

[2 Jul 2004 4:08] Shuichi Tamagawa
Description:
In the String Functions part of MySQL documentation,
http://dev.mysql.com/doc/mysql/en/String_functions.html#IDX1288
it says...

<snip>
If the leftmost character of the string str is a multi-byte character, returns the code for that character, calculated from the numeric values of its constituent bytes using this formula:

  (1st byte code * 256)
+ (2nd byte code * 256^2)
+ (3rd byte code * 256^3) ...
</snip>

But the behavior of this function is different.

How to repeat:
mysql> select cast(0xa4a2 as char);    # show a double byte character 
mysql> select ord(cast(0xa4a2 as char)); #1 
mysql> select hex(cast(0xa4a2 as char)); 
mysql> select conv('A4',16,10)*256*256+conv('A2',16,10)*256; #2
mysql> select conv('A4',16,10)*256+conv('A2',16,10)*1; #3

#1 != #2
#1 = #3

Suggested fix:
I think the document is incorrect and formula should be

 (1st byte code * 256^0)
+ (2nd byte code * 256^1)
+ (3rd byte code * 256^2) ...
[2 Jul 2004 6:23] Paul DuBois
Verified:

mysql> select ord(convert(0x0102 using ucs2));
+---------------------------------+
| ord(convert(0x0102 using ucs2)) |
+---------------------------------+
|                             258 |
+---------------------------------+
1 row in set (0.00 sec)

mysql> select ord(convert(0x0201 using ucs2));
+---------------------------------+
| ord(convert(0x0201 using ucs2)) |
+---------------------------------+
|                             513 |
+---------------------------------+
[2 Jul 2004 6:26] Paul DuBois
Thank you for your bug report. This issue has been addressed in the
documentation. The updated documentation will appear on our website
shortly, and will be included in the next release of the relevant
product(s).