Advertisement
Guest User

Scriptable iOS Decimal to Hexadecimal Widget

a guest
Sep 27th, 2020
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // written by /u/Worish
  2. const pretitle = "DecToHex"
  3. const title = "Input: "
  4. const input = String(args.widgetParameter)
  5. const title2 = "Output: "
  6. const color = "#53d769"
  7.  
  8. // if we're a widget
  9. if (config.runsInWidget) {
  10.     // create widget object
  11.     let widget = createWidget(pretitle, title, input, title2, color)
  12.     // show widget
  13.     Script.setWidget(widget)
  14.     //  end script
  15.     Script.complete()
  16. } else {
  17.     output = decToHex(String(input))
  18.    
  19.     //  make table
  20.     let table = new UITable()
  21.    
  22.     let row = new UITableRow()
  23.     row.isHeader = true
  24.     row.addText('Decimal to Hex Conversion')
  25.     table.addRow(row)
  26.             table.addRow(createRow("Input","Output"))
  27.     table.addRow(createRow(input,output))
  28.    
  29.     if (config.runsWithSiri)
  30.         Speech.speak("The decimal number "+input+", in hexadecimal is "+output)
  31.  
  32.     table.present()
  33.    
  34.     //  end script
  35.     Script.complete()
  36. }
  37.  
  38. function createRow(a,b) {
  39.     let row = new UITableRow()
  40.     row.addText(a)
  41.     row.addText(b).rightAligned
  42.    
  43.     return row
  44. }
  45.  
  46. function createWidget(pretitle, title, input, title2, color) {
  47.     let w = new ListWidget()
  48.    
  49.     output = decToHex(String(input))
  50.    
  51.     w.backgroundColor = new Color(color)
  52.    
  53.     txtColor = Color.white()
  54.    
  55.     formatWidgetRow(0, w, pretitle, txtColor, 0.8, 16)
  56.    
  57.     formatWidgetRow(5, w, title+input, txtColor, 0.8, 20)
  58.    
  59.     formatWidgetRow(5, w, title2+output, txtColor, 0.8, 20)
  60.    
  61.     return w
  62. }
  63.  
  64. function formatWidgetRow(prespacing, widget, text, color, opacity, size) {
  65.     widget.addSpacer(prespacing)
  66.     let theTxt = widget.addText(text)
  67.     theTxt.textColor = color
  68.     theTxt.textOpacity = opacity
  69.     theTxt.font = Font.systemFont(size)
  70. }
  71.  
  72. function decToHex(input) {
  73.     inNum = Number(input)
  74.     outString=""
  75.    
  76.     for (i=input.length;i>0;i--) {
  77.         appendix=intDiv(inNum,i-1)
  78.         inNum=modSixteen(inNum,i-1)
  79.         outString=outString+appendix
  80.         if (Number(outString) == 0) {
  81.             outString=""
  82.         }
  83.     }
  84.    
  85.     return String(outString)
  86. }
  87.  
  88. // written by /u/Worish
  89.  
  90. function intDiv(input, degree) {
  91.     digit = Math.floor(input/(16**degree))
  92.     output = digitChanger(digit)
  93.     return output
  94. }
  95.  
  96. function modSixteen(input,degree) {
  97.     output = input % (16**degree)
  98.     return output
  99. }
  100.  
  101. function digitChanger(digit) {
  102.     if (digit == 10) {return "A"}
  103.     else if (digit == 11) {return "B"}
  104.     else if (digit == 12) {return "C"}
  105.     else if (digit == 13) {return "D"}
  106.     else if (digit == 14) {return "E"}
  107.     else if (digit == 15) {return "F"}
  108.     else {return String(digit)}
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement