Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // written by /u/Worish
- const pretitle = "DecToHex"
- const title = "Input: "
- const input = String(args.widgetParameter)
- const title2 = "Output: "
- const color = "#53d769"
- // if we're a widget
- if (config.runsInWidget) {
- // create widget object
- let widget = createWidget(pretitle, title, input, title2, color)
- // show widget
- Script.setWidget(widget)
- // end script
- Script.complete()
- } else {
- output = decToHex(String(input))
- // make table
- let table = new UITable()
- let row = new UITableRow()
- row.isHeader = true
- row.addText('Decimal to Hex Conversion')
- table.addRow(row)
- table.addRow(createRow("Input","Output"))
- table.addRow(createRow(input,output))
- if (config.runsWithSiri)
- Speech.speak("The decimal number "+input+", in hexadecimal is "+output)
- table.present()
- // end script
- Script.complete()
- }
- function createRow(a,b) {
- let row = new UITableRow()
- row.addText(a)
- row.addText(b).rightAligned
- return row
- }
- function createWidget(pretitle, title, input, title2, color) {
- let w = new ListWidget()
- output = decToHex(String(input))
- w.backgroundColor = new Color(color)
- txtColor = Color.white()
- formatWidgetRow(0, w, pretitle, txtColor, 0.8, 16)
- formatWidgetRow(5, w, title+input, txtColor, 0.8, 20)
- formatWidgetRow(5, w, title2+output, txtColor, 0.8, 20)
- return w
- }
- function formatWidgetRow(prespacing, widget, text, color, opacity, size) {
- widget.addSpacer(prespacing)
- let theTxt = widget.addText(text)
- theTxt.textColor = color
- theTxt.textOpacity = opacity
- theTxt.font = Font.systemFont(size)
- }
- function decToHex(input) {
- inNum = Number(input)
- outString=""
- for (i=input.length;i>0;i--) {
- appendix=intDiv(inNum,i-1)
- inNum=modSixteen(inNum,i-1)
- outString=outString+appendix
- if (Number(outString) == 0) {
- outString=""
- }
- }
- return String(outString)
- }
- // written by /u/Worish
- function intDiv(input, degree) {
- digit = Math.floor(input/(16**degree))
- output = digitChanger(digit)
- return output
- }
- function modSixteen(input,degree) {
- output = input % (16**degree)
- return output
- }
- function digitChanger(digit) {
- if (digit == 10) {return "A"}
- else if (digit == 11) {return "B"}
- else if (digit == 12) {return "C"}
- else if (digit == 13) {return "D"}
- else if (digit == 14) {return "E"}
- else if (digit == 15) {return "F"}
- else {return String(digit)}
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement