fork download
  1. -- Binary, Decimal, Hexadecimal, Octal conversion
  2. -- Author: Tim Kelly and Andrew Stacey
  3. -- Websites: http://w...content-available-to-author-only...s.com/Lua/code/BinDecHex.shtml
  4. -- http://w...content-available-to-author-only...u.no/~stacey/HowDidIDoThat/iPad/Codea.html
  5. -- Licence: See below
  6.  
  7. --[[
  8. /*
  9.  * Copyright (c) 2007 Tim Kelly/Dialectronics
  10.  *
  11.  * Permission is hereby granted, free of charge, to any person obtaining
  12.  * a copy of this software and associated documentation files (the
  13.  * "Software"), to deal in the Software without restriction, including
  14.  * without limitation the rights to use, copy, modify, merge, publish,
  15.  * distribute, sublicense, and/or sell copies of the Software, and to permit
  16.  * persons to whom the Software is furnished to do so, subject to the
  17.  * following conditions:
  18.  *
  19.  * The above copyright notice and this permission notice shall be
  20.  * included in all copies or substantial portions of the Software.
  21.  *
  22.  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  23.  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  24.  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  25.  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  26.  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
  27.  * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
  28.  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  29.  */
  30.  
  31. --]]
  32.  
  33. --[[
  34. Formatting suitable for Codea on iPad together with octal functions by
  35. Andrew Stacey. Additions released under the original license.
  36. --]]
  37.  
  38. local hex2bin = {
  39. ["0"] = "0000",
  40. ["1"] = "0001",
  41. ["2"] = "0010",
  42. ["3"] = "0011",
  43. ["4"] = "0100",
  44. ["5"] = "0101",
  45. ["6"] = "0110",
  46. ["7"] = "0111",
  47. ["8"] = "1000",
  48. ["9"] = "1001",
  49. ["a"] = "1010",
  50. ["b"] = "1011",
  51. ["c"] = "1100",
  52. ["d"] = "1101",
  53. ["e"] = "1110",
  54. ["f"] = "1111"
  55. }
  56.  
  57.  
  58. local bin2hex = {
  59. ["0000"] = "0",
  60. ["0001"] = "1",
  61. ["0010"] = "2",
  62. ["0011"] = "3",
  63. ["0100"] = "4",
  64. ["0101"] = "5",
  65. ["0110"] = "6",
  66. ["0111"] = "7",
  67. ["1000"] = "8",
  68. ["1001"] = "9",
  69. ["1010"] = "A",
  70. ["1011"] = "B",
  71. ["1100"] = "C",
  72. ["1101"] = "D",
  73. ["1110"] = "E",
  74. ["1111"] = "F"
  75. }
  76.  
  77. local bin2oct = {
  78. ["000"] = "0",
  79. ["001"] = "1",
  80. ["010"] = "2",
  81. ["011"] = "3",
  82. ["100"] = "4",
  83. ["101"] = "5",
  84. ["110"] = "6",
  85. ["111"] = "7"
  86. }
  87.  
  88. local oct2bin = {
  89. ["0"] = "000",
  90. ["1"] = "001",
  91. ["2"] = "010",
  92. ["3"] = "011",
  93. ["4"] = "100",
  94. ["5"] = "101",
  95. ["6"] = "110",
  96. ["7"] = "111"
  97. }
  98.  
  99.  
  100. -- These functions are big-endian and take up to 32 bits
  101.  
  102. -- Hex2Bin
  103. -- Bin2Hex
  104. -- Hex2Dec
  105. -- Dec2Hex
  106. -- Bin2Dec
  107. -- Dec2Bin
  108.  
  109.  
  110. function Hex2Bin(s)
  111.  
  112. -- s -> hexadecimal string
  113.  
  114. local ret = ""
  115. local i = 0
  116.  
  117.  
  118. for i in string.gfind(s, ".") do
  119. i = string.lower(i)
  120.  
  121. ret = ret..hex2bin[i]
  122.  
  123. end
  124.  
  125. return ret
  126. end
  127.  
  128. function Bin2Hex(s)
  129.  
  130. -- s -> binary string
  131.  
  132. local l = 0
  133. local h = ""
  134. local b = ""
  135. local rem
  136.  
  137. l = string.len(s)
  138. rem = l % 4
  139. l = l-1
  140. h = ""
  141.  
  142. -- need to prepend zeros to eliminate mod 4
  143. if (rem > 0) then
  144. s = string.rep("0", 4 - rem)..s
  145. end
  146.  
  147. for i = 1, l, 4 do
  148. b = string.sub(s, i, i+3)
  149. h = h..bin2hex[b]
  150. end
  151.  
  152. return h
  153.  
  154. end
  155.  
  156. function Bin2Oct(s)
  157.  
  158. -- s -> binary string
  159.  
  160. local l = 0
  161. local h = ""
  162. local b = ""
  163. local rem
  164.  
  165. l = string.len(s)
  166. rem = l % 3
  167. l = l-1
  168. h = ""
  169.  
  170. -- need to prepend zeros to eliminate mod 3
  171. if (rem > 0) then
  172. s = string.rep("0", 3 - rem)..s
  173. end
  174.  
  175. for i = 1, l, 3 do
  176. b = string.sub(s, i, i+2)
  177. h = h..bin2oct[b]
  178. end
  179.  
  180. return h
  181.  
  182. end
  183.  
  184. function Oct2Bin(s)
  185.  
  186. -- s -> octal string
  187.  
  188. local ret = ""
  189. local i = 0
  190.  
  191.  
  192. for i in string.gfind(s, ".") do
  193. i = string.lower(i)
  194.  
  195. ret = ret..oct2bin[i]
  196.  
  197. end
  198.  
  199. return ret
  200. end
  201.  
  202. function Bin2Dec(s)
  203.  
  204. -- s -> binary string
  205.  
  206. local num = 0
  207. local ex = string.len(s) - 1
  208. local l = 0
  209.  
  210. l = ex + 1
  211. for i = 1, l do
  212. b = string.sub(s, i, i)
  213. if b == "1" then
  214. num = num + 2^ex
  215. end
  216. ex = ex - 1
  217. end
  218.  
  219. return string.format("%u", num)
  220.  
  221. end
  222.  
  223.  
  224.  
  225. function Dec2Bin(s, num)
  226.  
  227. -- s -> Base10 string
  228. -- num -> string length to extend to
  229.  
  230. local n
  231.  
  232. if (num == nil) then
  233. n = 0
  234. else
  235. n = num
  236. end
  237.  
  238. s = string.format("%x", s)
  239.  
  240. s = Hex2Bin(s)
  241.  
  242. while string.len(s) < n do
  243. s = "0"..s
  244. end
  245.  
  246. return s
  247.  
  248. end
  249.  
  250.  
  251.  
  252.  
  253. function Hex2Dec(s)
  254.  
  255. -- s -> hexadecimal string
  256.  
  257. local s = Hex2Bin(s)
  258.  
  259. return Bin2Dec(s)
  260.  
  261. end
  262.  
  263.  
  264.  
  265. function Dec2Hex(s)
  266.  
  267. -- s -> Base10 string
  268.  
  269. s = string.format("%x", s)
  270.  
  271. return s
  272.  
  273. end
  274.  
  275. function Hex2Oct(s)
  276.  
  277. -- s -> hexadecimal string
  278.  
  279. local s = Hex2Bin(s)
  280.  
  281. return Bin2Oct(s)
  282.  
  283. end
  284.  
  285.  
  286.  
  287. function Oct2Hex(s)
  288.  
  289. -- s -> Base8 string
  290.  
  291. local s = Oct2Bin(s)
  292.  
  293. return Bin2Hex(s)
  294.  
  295. end
  296.  
  297. function Dec2Oct(s)
  298.  
  299. -- s -> decimal string
  300.  
  301. s = string.format("%o", s)
  302.  
  303. return s
  304.  
  305. end
  306.  
  307.  
  308.  
  309. function Oct2Dec(s)
  310.  
  311. -- s -> Base10 string
  312.  
  313. local s = Oct2Bin(s)
  314.  
  315. return Bin2Dec(s)
  316.  
  317. end
  318.  
  319. -- These functions are big-endian and will extend to 32 bits
  320.  
  321. -- BMAnd
  322. -- BMNAnd
  323. -- BMOr
  324. -- BMXOr
  325. -- BMNot
  326.  
  327.  
  328. function BMAnd(v, m)
  329.  
  330. -- v -> hex string to be masked
  331. -- m -> hex string mask
  332.  
  333. -- s -> hex string as masked
  334.  
  335. -- bv -> binary string of v
  336. -- bm -> binary string mask
  337.  
  338. local bv = Hex2Bin(v)
  339. local bm = Hex2Bin(m)
  340.  
  341. local i = 0
  342. local s = ""
  343.  
  344. while (string.len(bv) < 32) do
  345. bv = "0000"..bv
  346. end
  347.  
  348. while (string.len(bm) < 32) do
  349. bm = "0000"..bm
  350. end
  351.  
  352.  
  353. for i = 1, 32 do
  354. cv = string.sub(bv, i, i)
  355. cm = string.sub(bm, i, i)
  356. if cv == cm then
  357. if cv == "1" then
  358. s = s.."1"
  359. else
  360. s = s.."0"
  361. end
  362. else
  363. s = s.."0"
  364.  
  365. end
  366. end
  367.  
  368. return Bin2Hex(s)
  369.  
  370. end
  371.  
  372.  
  373. function BMNAnd(v, m)
  374.  
  375. -- v -> hex string to be masked
  376. -- m -> hex string mask
  377.  
  378. -- s -> hex string as masked
  379.  
  380. -- bv -> binary string of v
  381. -- bm -> binary string mask
  382.  
  383. local bv = Hex2Bin(v)
  384. local bm = Hex2Bin(m)
  385.  
  386. local i = 0
  387. local s = ""
  388.  
  389. while (string.len(bv) < 32) do
  390. bv = "0000"..bv
  391. end
  392.  
  393. while (string.len(bm) < 32) do
  394. bm = "0000"..bm
  395. end
  396.  
  397.  
  398. for i = 1, 32 do
  399. cv = string.sub(bv, i, i)
  400. cm = string.sub(bm, i, i)
  401. if cv == cm then
  402. if cv == "1" then
  403. s = s.."0"
  404. else
  405. s = s.."1"
  406. end
  407. else
  408. s = s.."1"
  409.  
  410. end
  411. end
  412.  
  413. return Bin2Hex(s)
  414.  
  415. end
  416.  
  417.  
  418.  
  419. function BMOr(v, m)
  420.  
  421. -- v -> hex string to be masked
  422. -- m -> hex string mask
  423.  
  424. -- s -> hex string as masked
  425.  
  426. -- bv -> binary string of v
  427. -- bm -> binary string mask
  428.  
  429. local bv = Hex2Bin(v)
  430. local bm = Hex2Bin(m)
  431.  
  432. local i = 0
  433. local s = ""
  434.  
  435. while (string.len(bv) < 32) do
  436. bv = "0000"..bv
  437. end
  438.  
  439. while (string.len(bm) < 32) do
  440. bm = "0000"..bm
  441. end
  442.  
  443.  
  444. for i = 1, 32 do
  445. cv = string.sub(bv, i, i)
  446. cm = string.sub(bm, i, i)
  447. if cv == "1" then
  448. s = s.."1"
  449. elseif cm == "1" then
  450. s = s.."1"
  451. else
  452. s = s.."0"
  453. end
  454. end
  455.  
  456. return Bin2Hex(s)
  457.  
  458. end
  459.  
  460. function BMXOr(v, m)
  461.  
  462. -- v -> hex string to be masked
  463. -- m -> hex string mask
  464.  
  465. -- s -> hex string as masked
  466.  
  467. -- bv -> binary string of v
  468. -- bm -> binary string mask
  469.  
  470. local bv = Hex2Bin(v)
  471. local bm = Hex2Bin(m)
  472.  
  473. local i = 0
  474. local s = ""
  475.  
  476. while (string.len(bv) < 32) do
  477. bv = "0000"..bv
  478. end
  479.  
  480. while (string.len(bm) < 32) do
  481. bm = "0000"..bm
  482. end
  483.  
  484.  
  485. for i = 1, 32 do
  486. cv = string.sub(bv, i, i)
  487. cm = string.sub(bm, i, i)
  488. if cv == "1" then
  489. if cm == "0" then
  490. s = s.."1"
  491. else
  492. s = s.."0"
  493. end
  494. elseif cm == "1" then
  495. if cv == "0" then
  496. s = s.."1"
  497. else
  498. s = s.."0"
  499. end
  500. else
  501. -- cv and cm == "0"
  502. s = s.."0"
  503. end
  504. end
  505.  
  506. return Bin2Hex(s)
  507.  
  508. end
  509.  
  510.  
  511. function BMNot(v, m)
  512.  
  513. -- v -> hex string to be masked
  514. -- m -> hex string mask
  515.  
  516. -- s -> hex string as masked
  517.  
  518. -- bv -> binary string of v
  519. -- bm -> binary string mask
  520.  
  521. local bv = Hex2Bin(v)
  522. local bm = Hex2Bin(m)
  523.  
  524. local i = 0
  525. local s = ""
  526.  
  527. while (string.len(bv) < 32) do
  528. bv = "0000"..bv
  529. end
  530.  
  531. while (string.len(bm) < 32) do
  532. bm = "0000"..bm
  533. end
  534.  
  535.  
  536. for i = 1, 32 do
  537. cv = string.sub(bv, i, i)
  538. cm = string.sub(bm, i, i)
  539. if cm == "1" then
  540. if cv == "1" then
  541. -- turn off
  542. s = s.."0"
  543. else
  544. -- turn on
  545. s = s.."1"
  546. end
  547. else
  548. -- leave untouched
  549. s = s..cv
  550.  
  551. end
  552. end
  553.  
  554. return Bin2Hex(s)
  555.  
  556. end
  557.  
  558.  
  559. -- these functions shift right and left, adding zeros to lost or gained bits
  560. -- returned values are 32 bits long
  561.  
  562. -- BShRight(v, nb)
  563. -- BShLeft(v, nb)
  564.  
  565.  
  566. function BShRight(v, nb)
  567.  
  568. -- v -> hexstring value to be shifted
  569. -- nb -> number of bits to shift to the right
  570.  
  571. -- s -> binary string of v
  572.  
  573. local s = Hex2Bin(v)
  574.  
  575. while (string.len(s) < 32) do
  576. s = "0000"..s
  577. end
  578.  
  579. s = string.sub(s, 1, 32 - nb)
  580.  
  581. while (string.len(s) < 32) do
  582. s = "0"..s
  583. end
  584.  
  585. return Bin2Hex(s)
  586.  
  587. end
  588.  
  589. function BShLeft(v, nb)
  590.  
  591. -- v -> hexstring value to be shifted
  592. -- nb -> number of bits to shift to the right
  593.  
  594. -- s -> binary string of v
  595.  
  596. local s = Hex2Bin(v)
  597.  
  598. while (string.len(s) < 32) do
  599. s = "0000"..s
  600. end
  601.  
  602. s = string.sub(s, nb + 1, 32)
  603.  
  604. while (string.len(s) < 32) do
  605. s = s.."0"
  606. end
  607.  
  608. return Bin2Hex(s)
  609.  
  610. end
  611.  
  612.  
  613. print(Bin2Hex("11001010"))
Success #stdin #stdout 0.01s 5288KB
stdin
Standard input is empty
stdout
CA