Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

13
LINES

< > BotCompany Repo | #356 // num2hex

Lua code

1  
-- Returns HEX representation of num
2  
-- minlen is optional 
3  
function num2hex(num, minlen)
4  
  minlen = minlen or 1
5  
  local hexstr = '0123456789abcdef'
6  
  local s = ''
7  
  while num > 0 or #s < minlen do
8  
    local mod = math.fmod(num, 16)
9  
    s = string.sub(hexstr, mod+1, mod+1) .. s
10  
    num = math.floor(num / 16)
11  
  end
12  
  return s
13  
end

test run  test run with input  download  show line numbers   

Travelled to 12 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #356
Snippet name: num2hex
Eternal ID of this version: #356/1
Text MD5: 32f9b49efbc5d929b47cd7c61c10f568
Author: stefan
Category: string functions
Type: Lua code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2015-01-28 20:47:53
Source code size: 342 bytes / 13 lines
Pitched / IR pitched: No / Yes
Views / Downloads: 1075 / 485
Referenced in: [show references]