Menu

Skip to content
AppleScriptの穴
  • Home
  • Products
  • Books
  • Docs
  • Events
  • Forum
  • About This Blog
  • License
  • 仕事依頼

AppleScriptの穴

Useful & Practical AppleScript archive. Click '★Click Here to Open This Script' Link to download each AppleScript

10進数とn進数文字列の相互変換

Posted on 11月 12, 2019 by Takaaki Naganoya

10進数の数値とn進数の文字列を相互変換するAppleScriptです。

もともと、AppleScriptの言語仕様上、10進数とn進数文字列との相互変換機能は存在しないのですが、サードパーティから機能拡張書類(OSAX)でそのような機能が提供され、さらにClassic MacOSからMac OS Xへの移行時にそれらの機能拡張書類が使えなくなって(一部は移行)、個別にそれらの機能をスクリプター側で実装してMailing ListやBBS上、Blogなどで共有してきたという経緯があります。

AppleScript Users MLに流れていたもののような気がしますが、自分で作ったものかもしれません。出どころ不明です。

ただ、2進数変換や16進数変換などの各種変換ルーチンが乱立していたところに、「これ、全部1つで済むんじゃね?」と思ってまとめたような気がしないでもありません。自分で作っても簡単なので。

Cocoaの機能を一切使っていません。たしかにCocoaの機能を用いての変換も行えるのですが、データのサイズがとても小さい(桁数が少ない)ので、Cocoaの機能を呼び出さないほうが高速です。

もしも、AppleScriptの予約語のようにこれらの機能を呼び出したい場合には、これらのScriptをAppleScript Librariesにして、AppleScript用語辞書をつけて書き出せば(辞書はなくても大丈夫ですが)、

use nthLib: script "nThConvLib"

のように呼び出すことができるようになっています(macOS 10.9以降)。

AppleScript名:10進数をn進数文字列に変換する
set a to 100

–10進→16進数変換
set b to aNumToNthDecimal(a, 16, 4, {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F"}, true) of me
–> "64"

–10進→8進数変換
set c to aNumToNthDecimal(a, 8, 4, {"0", "1", "2", "3", "4", "5", "6", "7"}, true) of me
–> "144"

–10進→2進数変換
set d to aNumToNthDecimal(a, 2, 8, {"0", "1"}, false) of me
–> "01100100"

–10進数をn進数文字列に変換する

–origNum:変換対象の数字
–nTh: n進数のn
–stringLength: 出力指定桁(上位桁のアキはゼロパディングされる)
–zeroSuppress: 上位桁がゼロになった場合に評価を打ち切るかのフラグ、ゼロパディングを行わない(ゼロサプレスする)場合に指定
on aNumToNthDecimal(origNum, nTh, stringLength, stringSetList, zeroSuppress)
  set resString to {}
  
repeat with i from stringLength to 1 by -1
    if {origNum, zeroSuppress} = {0, true} then exit repeat
    
set resNum to (origNum mod nTh)
    
set resText to contents of item (resNum + 1) of stringSetList
    
set resString to resText & resString
    
set origNum to origNum div nTh
  end repeat
  
return (resString as string)
end aNumToNthDecimal

★Click Here to Open This Script 

AppleScript名:n進数文字列を10進数に変換する v2a
–2進数文字列→10進数数値変換
set a to "11"
set b to aNthToDecimal(a, {"0", "1"}) of me
–> 3

–8進数文字列→10進数数値変換
set a to "144"
set c to aNthToDecimal(a, {"0", "1", "2", "3", "4", "5", "6", "7"}) of me
–> 100

–16進数文字列→10進数数値変換
set a to "64"
set b to aNthToDecimal(a, {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F"}) of me
–> 100

–n進数文字列を10進数に変換する
on aNthToDecimal(origStr as string, nTh as list)
  set resNumber to 0
  
set sList to reverse of (characters of origStr)
  
set aLen to length of nTh
  
set digitCount to 0
  
  
repeat with i in sList
    set j to contents of i
    
set aRes to (offsetInList(j, nTh) of me)
    
    
set resNumber to resNumber + (aLen ^ digitCount) * (aRes – 1)
    
set digitCount to digitCount + 1
  end repeat
  
  
return resNumber as integer
end aNthToDecimal

–元はCocoaの機能を利用していたが、処理対象のデータが小さいのでOld Style AppleScriptで書き直した
on offsetInList(aChar as string, aList as list)
  set aCount to 1
  
repeat with i in aList
    set j to contents of i
    
if aChar is equal to j then
      return aCount
    end if
    
set aCount to aCount + 1
  end repeat
  
return 0
end offsetInList

★Click Here to Open This Script 

(Visited 60 times, 1 visits today)

More from my site

  • checkboxで項目選択するありふれたScriptにsdefをつけてみるcheckboxで項目選択するありふれたScriptにsdefをつけてみる
  • sdef(AppleScript用語辞書)に画像やムービーを入れるsdef(AppleScript用語辞書)に画像やムービーを入れる
  • display youtube Script Libraryをアップデートdisplay youtube Script Libraryをアップデート
  • JavaScriptCore経由で関数計算を行うcalcLibASをアップデートJavaScriptCore経由で関数計算を行うcalcLibASをアップデート
  • 数値演算ライブラリ「calcLibAS」v1.3(28倍速)数値演算ライブラリ「calcLibAS」v1.3(28倍速)
  • 数値演算ライブラリ「calcLibAS」v1.2(6倍速)数値演算ライブラリ「calcLibAS」v1.2(6倍速)
Posted in Number Text | Tagged 10.12savvy 10.13savvy 10.14savvy 10.15savvy | Leave a comment

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

電子書籍(PDF)をオンラインストアで販売中!

Google Search

Popular posts

  • AppleScriptによるWebブラウザ自動操縦ガイド
  • macOS 13, Ventura(継続更新)
  • ドラッグ&ドロップ機能の未来?
  • macOS 12.x上のAppleScriptのトラブルまとめ
  • PFiddlesoft UI Browserが製品終了に
  • macOS 12.3 beta 5、ASの障害が解消される(?)
  • SF Symbolsを名称で指定してPNG画像化
  • 新刊発売:AppleScriptによるWebブラウザ自動操縦ガイド
  • macOS 12.3 beta4、まだ直らないASまわりの障害
  • macOS 12.3上でFinder上で選択中のファイルをそのままオープンできない件
  • Safariで表示中のYouTubeムービーのサムネイル画像を取得
  • macOS 12のスクリプトエディタで、Context Menu機能にバグ
  • Pixelmator Pro v2.4.1で新機能追加+AppleScriptコマンド追加
  • 人類史上初、魔導書の観点から書かれたAppleScript入門書「7つの宝珠」シリーズ開始?!
  • CotEditor v4.1.2でAppleScript系の機能を追加
  • macOS 12.5(21G72)がリリースされた!
  • UI Browserがgithub上でソース公開され、オープンソースに
  • Pages v12に謎のバグ。書類上に11枚しか画像を配置できない→解決
  • 新発売:AppleScriptからSiriを呼び出そう!
  • iWork 12.2がリリースされた

Tags

10.11savvy (1102) 10.12savvy (1243) 10.13savvy (1391) 10.14savvy (586) 10.15savvy (434) 11.0savvy (274) 12.0savvy (174) 13.0savvy (34) CotEditor (60) Finder (47) iTunes (19) Keynote (97) NSAlert (60) NSArray (51) NSBezierPath (18) NSBitmapImageRep (21) NSBundle (20) NSButton (34) NSColor (51) NSDictionary (27) NSFileManager (23) NSFont (18) NSImage (42) NSJSONSerialization (21) NSMutableArray (62) NSMutableDictionary (21) NSPredicate (36) NSRunningApplication (56) NSScreen (30) NSScrollView (22) NSString (118) NSURL (97) NSURLRequest (23) NSUTF8StringEncoding (30) NSUUID (18) NSView (33) NSWorkspace (20) Numbers (55) Pages (36) Safari (41) Script Editor (20) WKUserContentController (21) WKUserScript (20) WKWebView (23) WKWebViewConfiguration (22)

カテゴリー

  • 2D Bin Packing
  • 3D
  • AirDrop
  • AirPlay
  • Animation
  • AppleScript Application on Xcode
  • beta
  • Bluetooth
  • Books
  • boolean
  • bounds
  • Bug
  • Calendar
  • call by reference
  • Clipboard
  • Code Sign
  • Color
  • Custom Class
  • dialog
  • drive
  • exif
  • file
  • File path
  • filter
  • folder
  • Font
  • Font
  • GAME
  • geolocation
  • GUI
  • GUI Scripting
  • Hex
  • History
  • How To
  • iCloud
  • Icon
  • Image
  • Input Method
  • Internet
  • iOS App
  • JavaScript
  • JSON
  • JXA
  • Keychain
  • Keychain
  • Language
  • Library
  • list
  • Locale
  • Machine Learning
  • Map
  • Markdown
  • Menu
  • Metadata
  • MIDI
  • MIME
  • Natural Language Processing
  • Network
  • news
  • Noification
  • Notarization
  • Number
  • Object control
  • OCR
  • OSA
  • PDF
  • Peripheral
  • PRODUCTS
  • QR Code
  • Raw AppleEvent Code
  • Record
  • recursive call
  • regexp
  • Release
  • Remote Control
  • Require Control-Command-R to run
  • REST API
  • Review
  • RTF
  • Sandbox
  • Screen Saver
  • Script Libraries
  • sdef
  • search
  • Security
  • selection
  • shell script
  • Shortcuts Workflow
  • Sort
  • Sound
  • Spellchecker
  • Spotlight
  • SVG
  • System
  • Tag
  • Telephony
  • Text
  • Text to Speech
  • timezone
  • Tools
  • Update
  • URL
  • UTI
  • Web Contents Control
  • WiFi
  • XML
  • XML-RPC
  • イベント(Event)
  • 未分類

アーカイブ

  • 2023年3月
  • 2023年2月
  • 2023年1月
  • 2022年12月
  • 2022年11月
  • 2022年10月
  • 2022年9月
  • 2022年8月
  • 2022年7月
  • 2022年6月
  • 2022年5月
  • 2022年4月
  • 2022年3月
  • 2022年2月
  • 2022年1月
  • 2021年12月
  • 2021年11月
  • 2021年10月
  • 2021年9月
  • 2021年8月
  • 2021年7月
  • 2021年6月
  • 2021年5月
  • 2021年4月
  • 2021年3月
  • 2021年2月
  • 2021年1月
  • 2020年12月
  • 2020年11月
  • 2020年10月
  • 2020年9月
  • 2020年8月
  • 2020年7月
  • 2020年6月
  • 2020年5月
  • 2020年4月
  • 2020年3月
  • 2020年2月
  • 2020年1月
  • 2019年12月
  • 2019年11月
  • 2019年10月
  • 2019年9月
  • 2019年8月
  • 2019年7月
  • 2019年6月
  • 2019年5月
  • 2019年4月
  • 2019年3月
  • 2019年2月
  • 2019年1月
  • 2018年12月
  • 2018年11月
  • 2018年10月
  • 2018年9月
  • 2018年8月
  • 2018年7月
  • 2018年6月
  • 2018年5月
  • 2018年4月
  • 2018年3月
  • 2018年2月

https://piyomarusoft.booth.pm/items/301502

メタ情報

  • 登録
  • ログイン
  • 投稿フィード
  • コメントフィード
  • WordPress.org

Forum Posts

  • 人気のトピック
  • 返信がないトピック

メタ情報

  • 登録
  • ログイン
  • 投稿フィード
  • コメントフィード
  • WordPress.org
Proudly powered by WordPress
Theme: Flint by Star Verte LLC