【问题标题】:Convert HEX iTunes Persistent ID to High and Low 32-bit forms将 HEX iTunes 持久 ID 转换为高和低 32 位形式
【发布时间】:2011-01-26 17:09:27
【问题描述】:

在 Mac 上,我可以从“iTunes Music Library.xml”文件中提取特定歌曲的“Persistent ID”,然后使用 Applescript 播放该歌曲,如下所示:

tell application "iTunes"   
    set thePersistentId to "F040658A7687B12D"
    set theSong to (some track of playlist "Music" whose persistent ID is thePersistentId)  
    play theSong with once  
end tell  

在 PC 上,我可以以同样的方式从 XML 文件中提取“持久 ID”。 iTunes COM 接口的文档说函数“ItemByPersistentId”有两个参数:“highID”(64 位持久 ID 的高 32 位)和“lowID”(64 位持久 ID 的低 32 位) .但我不知道如何将基于十六进制的值转换为 ItemByPersistentId 函数想要的高低 32 位值。

var thePersistentId = "F040658A7687B12D";  
var iTunes = WScript.CreateObject("iTunes.Application"); 
var n = parseInt(thePersistentId);  
var high = (do something with n?);  
var low = (do something else with n?);  
iTunes.LibraryPlaylist.tracks.ItemByPersistentId(high,low).play();  

【问题讨论】:

  • 您是否有特定的示例 ID 以便可以试用和测试代码?
  • iTunes 中的持久 ID 示例:F040658A7687B12D、9CA9C40E86124232、1489F2F36DA31F95(它们都是 16 个字符)

标签: javascript itunes hex


【解决方案1】:

不知道windows脚本宿主是什么情况,但是有的js客户端是32位的int,所以你的var n = parseInt(thePersistentId);就麻烦了。使用 parseInt 时应始终包含基数(例如 parseInt(hexValue, 16)parseInt(octValue, 8))。

为避免 32 位脚本解释器的限制,您可以分别提取低 32 位和高 32 位。每个十六进制数字是 4 位,因此低 32 位是持久 ID 的最后 8 个字符,高 32 位是剩余 8 位(除非它有 0x 前缀,那么它是下一个最右边的 8 个字符块)。

var hexId = "XXXXX";   
var iTunes = WScript.CreateObject("iTunes.Application");  

//the following two statements assume you have a valid 64 bit hex, 
//you may want to verify the length of the string

//grab and parse the last 8 characters of your string
var low = parseInt(hexId.substr(hexId.length - 8), 16);
//grab and parse the next last 8 characters of your string
var high = parseInt(hexId.substr(hexId.length - 16, 8), 16);
iTunes.LibraryPlaylist.tracks.ItemByPersistentId(high,low).play(); 

编辑:从我在iTuner source code 中读到的内容来看,highID 实际上是高 4 个字节,lowID 是低 4 个字节,而不是 8 个字节(因此丢弃了中间 8 个字节) persistentID...)。这是一个修改后的尝试:

//assumes hex strings with no "0x" prefix
//grab and parse the last 4 characters of your string
var low = parseInt(hexId.substr(hexId.length - 4), 16);
//grab, pad and parse the first 4 characters of your string
var high = parseInt(hexId.substr(0, 4) + "0000", 16);

【讨论】:

  • 谢谢。好主意。不幸的是,ItemByPersistentId 以这种方式计算的高/低返回“溢出”错误。我使用 hexId="F040658A7687B12D" 作为我的测试用例。还有其他想法吗?
  • @cloudbrain,我挖了一圈,看起来 highID 是高 4 字节作为 8 字节整数中的最高位,lowID 是最低 4 字节。试试我上面的修改,然后告诉我结果如何。
  • 如何将 high 和 low 转换为字符串?
  • String.Format("{0}{1}", _high.ToString("X"), _low.ToString("X"));
  • @mike,好吧,它是 javascript,所以要么简单地将它们附加到一个字符串,要么调用 .toString。请参阅What's the best way to convert a number to a string? 了解更多信息。
猜你喜欢
  • 2014-01-30
  • 2013-11-03
  • 1970-01-01
  • 1970-01-01
  • 2011-12-29
  • 2017-04-19
  • 1970-01-01
  • 1970-01-01
  • 2021-08-15
相关资源
最近更新 更多