如何创建和递增 2 位或 3 位十六进制数?

How do I create and increment a 2 or 3 digit hexadecimal number?

如何从十六进制数数组中递增最大的十六进制数?我对十六进制的了解有些参差不齐,因此我们将不胜感激。老实说,我不知道这些数字是否是十六进制的,因为它们前面有一个 "u",但如果你删除 "u",它们看起来就是那样。这些值来自 InDesign 片段文档。

示例:

var anArray = ["uf9","ufc","u111","u112","u136","u137"]; // actual values

var getUniqueID = getNextHigherNumber(anArray);

function getNextHigherNumber(anArray) {
   // sort array
   // create variable and add one
   // return variable 
   return variable;
}

XML 来自服务器(查看 Self 和 Source):

<Hyperlink Self="ufc" Name="is a  multiline hyperlink that terminates here" Source="uf9" Visible="false" Highlight="None" Width="Thin" BorderStyle="Solid" Hidden="false" DestinationUniqueKey="1">
    <Properties>
        <BorderColor type="enumeration">Black</BorderColor>
        <Destination type="object">HyperlinkURLDestination/http%3a//test.com#1stMultilineLink/</Destination>
    </Properties>
</Hyperlink>
<Hyperlink Self="u112" Name="hyperlink inline" Source="u111" Visible="false" Highlight="None" Width="Thin" BorderStyle="Solid" Hidden="false" DestinationUniqueKey="2">
    <Properties>
        <BorderColor type="enumeration">Black</BorderColor>
        <Destination type="object">HyperlinkURLDestination/http%3a//test.com</Destination>
    </Properties>
</Hyperlink>
<Hyperlink Self="u137" Name="another multline hyperlink" Source="u136" Visible="false" Highlight="Outline" Width="Thick" BorderStyle="Solid" Hidden="false" DestinationUniqueKey="3">
    <Properties>
        <BorderColor type="enumeration">Purple</BorderColor>
        <Destination type="object">HyperlinkURLDestination/http%3a//google.com#multilinehyperlink</Destination>
    </Properties>
</Hyperlink>

更多背景:

我有一个现有的 XML 文档,它的 ID 看起来像是使用十六进制数字系统,我需要能够为新节点创建一个唯一的 ID。 ID 值看起来类似于 HTML 网页颜色,例如“0xFF0000”(红色),但不同之处在于它使用 2 或 3 个字符而不是 6 个,例如 "ufc" 或 "u112"。

我从服务器收到一个 XML 文件,它有节点,每个节点都有一个具有唯一值的 ID(参见上面的 XML 示例)。如果我必须创建一个新的 "item",我需要为其创建一个尚未使用的唯一 ID。

我认为您在考虑十六进制时会使事情复杂化 ;) 您可以将十六进制值转换为十进制值,然后继续您正在做的事情,因此您可以这样做:

var anArray = ['u112', 'u136', 'uf9', 'u137', 'u111', 'ufc'];

var getUniqueID = getNextHigherNumber(anArray);

trace('u', getUniqueID);    // gives : u138

function getNextHigherNumber(anArray:Array):String {
    var max:int = 0;
    for(var i:int = 0; i<anArray.length; i++){
        // convert the hex value to an integer
        var num:int = int('0x' + String(anArray[i]).substr(1)); 
        // get the max value    
        if(num > max) max = num;
    }
    // return the hex value of (max value + 1)
    return (max + 1).toString(16);
}

希望能帮到你。

首先,十六进制只是数字的一种表示。 数字本身保持不变。

加2,就是取这个数的值,加上2的值。 十六进制只是另一种记下数字的方式。

非常感谢您努力弄清楚这些值是什么,但为什么不查看文档来确定呢?

您有一个 XML 并收到了 with/from InDesign。 只是搜索那个 "IDML" 给我,这似乎是 Adob​​e 给你试图解析的格式的名称。

Adobe 提供了描述此格式的文档: https://wwwimages2.adobe.com/content/dam/Adobe/en/devnet/indesign/cs55-docs/IDML/idml-specification.pdf

第 10.1.1 节是关于 Self 属性的:

The Self attribute contains a unique identifier for the elements that contain it. This identifier is used elsewhere in the IDML package to refer to the element, as discussed in the “Object Reference Format” section of this specification. Schema Example 2. Self attribute Self { xsd:string }

如您所见,该值是一个字符串,而不是数字。

在第 31 页,您可以找到有关 Adob​​e 如何为 Self 创建值的说明,包括以下语句:

The only requirement of the value of the Self attribute is that it is unique within the IDML package. If you are writing the IDML yourself, you do not need to observe the above pattern— you can change the value of the Self attribute to anything you want as long as it is unique (within the IDML package) and as long as all references to the element are also changed to match.

你标记了 JavaScriptActionScript(我不知道 ActionScript),但你问题中的代码对我来说看起来像 JavaScript,而另一个答案在这里看起来像是在 ActionScript 中;所以,假设你仍然想这样做(看起来用户 null 正在做某事),我将 post 等同于 JavaScript:

var anArray = ["uf9","ufc","u111","u112","u136","u137"];

function getNextHigherNumber(theArray) {
    var maxNum = 0;
    theArray.forEach(function(num) {
        num = parseInt(num.substr(1), 16);
        if(num > maxNum) maxNum = num;
    });
    return 'u' + (maxNum + 1).toString(16);
}

// Run the function to see if it works
console.log(getNextHigherNumber(anArray));

它在 forEach 中做了什么 function/loop:

  • 从每个数组元素中删除 "u"。
  • 将不带 "u" 的字符串转换为其 int 十进制等效值。
  • 如果当前数大于之前存储的maxNum.
  • ,则将数存储在maxNum

然后它 returns 最大的数字加一,通过 .toString(16) 再次转换回十六进制(并在其前面加上 'u')。