Scite Convert Dec Hex

lua-users home
wiki

How many times do you convert decimal-to-hex or hex-to-decimal when you are in coding. SciteConvertDecHex converts the selected text to decimal or hex. You don't need external programmer's calculator application and copy&paste jobs anymore.

-- SciteConvertDecHex
-- Convert the selected text to decimal or hex
-- 2013.04.06 by lee.sheen at gmail dot com

scite_Command {
  'Convert Dec/Hex|ConvertDecHex|Alt+Shift+C',
}

function IsHexString (s)
  local header = string.sub(s, 1, 2)
  if "0x" == header or "0X" == header then
    return true
  else
    return false
  end
end

function ConvertDecHex ()
  local current_selected_text, current_selected_length = editor:GetSelText()
  local converted_number = tonumber(current_selected_text)
  if  not (converted_number == nil) then
    local converted_text = nil
    if IsHexString(current_selected_text) then
      converted_text = tostring(converted_number)
    else
      converted_text = string.format("0x%X", converted_number)
    end
    editor:ReplaceSel(converted_text)
  end
end

RecentChanges · preferences
edit · history
Last edited April 6, 2013 9:27 am GMT (diff)