| 1 |
LuaBit |
| 2 |
------ |
| 3 |
LuaBit is a bitwise operation lib completely written in Lua. It's |
| 4 |
written in the belief that Lua is self-contained. |
| 5 |
|
| 6 |
The supported operations are: not, and, or, xor, right shift, logic |
| 7 |
right shift and left shift. |
| 8 |
|
| 9 |
Several utilities are designed to leverage the power of bit operation: |
| 10 |
1. hex: a dec <-> hex number converter |
| 11 |
2. utf8: convert utf8 string to ucs2 |
| 12 |
3. noki: convert nokia pc suite backuped SMS file to .txt |
| 13 |
|
| 14 |
Under the MIT license. |
| 15 |
|
| 16 |
Visit http://luaforge.net/projects/bit/ to get latest version. |
| 17 |
|
| 18 |
Status |
| 19 |
------ |
| 20 |
Now LuaBit is in v0.4. |
| 21 |
Release date: Mar 18, 2007 |
| 22 |
|
| 23 |
Content |
| 24 |
------- |
| 25 |
3 files are there for LuaBit: |
| 26 |
1) bit.lua |
| 27 |
is the bitwise operation lib, all operations are implemented here. |
| 28 |
|
| 29 |
2) hex.lua |
| 30 |
is a helper lib for ease of using hex numbers with bitwise |
| 31 |
operation. |
| 32 |
|
| 33 |
3) noki.lua |
| 34 |
a utility(based on bit and hex) to convert Nokia PC Suite backuped |
| 35 |
SMS to a unicode .txt file, which is more accessible than the |
| 36 |
original .nfb or .nfc file. |
| 37 |
|
| 38 |
4) utf8.lua |
| 39 |
convert utf8 string to ucs2 string |
| 40 |
|
| 41 |
How to use |
| 42 |
---------- |
| 43 |
Bit |
| 44 |
--- |
| 45 |
Just require 'bit' in your project and the bit lib will be |
| 46 |
available: |
| 47 |
bit.bnot(n) -- bitwise not (~n) |
| 48 |
bit.band(m, n) -- bitwise and (m & n) |
| 49 |
bit.bor(m, n) -- bitwise or (m | n) |
| 50 |
bit.bxor(m, n) -- bitwise xor (m ^ n) |
| 51 |
bit.brshift(n, bits) -- right shift (n >> bits) |
| 52 |
bit.blshift(n, bits) -- left shift (n << bits) |
| 53 |
bit.blogic_rshift(n, bits) -- logic right shift(zero fill >>>) |
| 54 |
|
| 55 |
Please note that bit.brshift and bit.blshift only support number within |
| 56 |
32 bits. |
| 57 |
|
| 58 |
2 utility functions are provided too: |
| 59 |
bit.tobits(n) -- convert n into a bit table(which is a 1/0 sequence) |
| 60 |
-- high bits first |
| 61 |
bit.tonumb(bit_tbl) -- convert a bit table into a number |
| 62 |
|
| 63 |
Hex |
| 64 |
--- |
| 65 |
For ease of using hex numbers, a utility hex lib is also included in |
| 66 |
LuaBit. You can require 'hex' to use them: |
| 67 |
hex.to_hex(n) -- convert a number to a hex string |
| 68 |
hex.to_dec(hex) -- convert a hex string(prefix with '0x' or '0X') to number |
| 69 |
|
| 70 |
With hex, you can write code like: |
| 71 |
bit.band(258, hex.to_dec('0xFF')) |
| 72 |
to get the lower 8 bits of 258, that's 2. |
| 73 |
|
| 74 |
Noki |
| 75 |
---- |
| 76 |
require 'noki', to save your sms to .txt file: |
| 77 |
noki.save_sms('nokia.nfb', 'sms.txt') |
| 78 |
and you can view the output sms.txt in notepad or other editor which |
| 79 |
support unicode. |
| 80 |
|
| 81 |
Utf8 |
| 82 |
---- |
| 83 |
require 'utf8', to convert a utf8 string: |
| 84 |
ucs2_string = utf8.utf_to_uni(utf8_string) |
| 85 |
|
| 86 |
History |
| 87 |
------- |
| 88 |
v0.4 |
| 89 |
* utf8 to ucs2 converter(utf8.lua). |
| 90 |
* clean up for compatible with Lua5.1 and 5.0. |
| 91 |
* add 'How to use' section for bit.lua and hex.lua. |
| 92 |
|
| 93 |
v0.3 |
| 94 |
* noki added as an application of bit. |
| 95 |
* typo correction. |
| 96 |
|
| 97 |
v0.2 |
| 98 |
* add logic right shift(>>>) support: bit.blogic_rshift. |
| 99 |
* add 2 utility functions: bit.tobits and bit.tonumb. |
| 100 |
* update hex.to_hex(in hex.lua) to support negative number. |
| 101 |
|
| 102 |
v0.1 |
| 103 |
LuaBit is written when I do my own game project(Fio at http://fio.edithis.info). |
| 104 |
When loading resources, I have to do some bit operation. And I do not |
| 105 |
like the embedded way of bit operation. So I decide to implement those |
| 106 |
ops in lua. And that's LuaBit. It's not as fast as the embedded one, but |
| 107 |
it works. And Lua is self-contained :-) |
| 108 |
|
| 109 |
To-Do List |
| 110 |
--------- |
| 111 |
v0.1 |
| 112 |
It'll be useful if LuaBit support those bitwise op like: |
| 113 |
bit.band(258, '0xFF') |
| 114 |
ease to type and use. This will be supported in next release. |
| 115 |
|
| 116 |
v0.2 |
| 117 |
I decide to delay this feature to later version for it'll mess up the |
| 118 |
interface of LuaBit. |
| 119 |
|
| 120 |
v0.3 |
| 121 |
May more utility functions add to Noki - phonebook might be a nice candidate. |
| 122 |
|
| 123 |
v0.4 |
| 124 |
There's no UCS2 -> UTF8 convertion now, this feature may add in next release |
| 125 |
or when the project need. |
| 126 |
|
| 127 |
Noki'll be be exluded from LuaBit in next release; I decide to let Noki grow |
| 128 |
into a powerful tool to support more Nokia PC Suite backup format(.nfb, |
| 129 |
.nfc and .nbu). |
| 130 |
|
| 131 |
Trial Noki demo at http://nokisms.googlepages.com/(in Chinese) |
| 132 |
|
| 133 |
Known issues |
| 134 |
------------ |
| 135 |
LuaBit doesn't play very well with negative number. The return value of the |
| 136 |
bitwise operations might change to positive when applied on negative numbers |
| 137 |
though the bit sequence is correct. So if you want do some arithmetic with |
| 138 |
the result of bit operation, be careful. |
| 139 |
|
| 140 |
Feedback |
| 141 |
-------- |
| 142 |
Please send your comments, bugs, patches or change request to |
| 143 |
hanzhao([email protected]). |