How do I create and increment a 2 or 3 digit hexadecimal number?
How do I increment the highest hexidecimal number from an array of hexidecimal numbers? My knowledge of hexidecimal is somewhat spotty so any help would be appreciated. And to be perfectly honest I don't know if the numbers are hexadecimal or not because there is a "u" in front of them but they look that way if you remove the "u". The values are from an InDesign snippet document.
Example:
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 from the server (look at Self and 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>
More background:
I have an existing XML document that looks like it's using hexidecimal number system for it's IDs and I need to be able to create a unique ID for new nodes. The ID values look similar to HTML web colors like, "0xFF0000" (which is red) but the difference is that it is using 2 or 3 characters instead of 6, for example, "ufc" or "u112".
I receive an XML file from the server and it has nodes and each node has an ID with a unique value (see XML example above). If I have to create a new "item" I need to create a unique ID for it that isn't already used.
First off, when a string consists of a hexadecimal number prefixed with a "u", it usually represents a Unicode Code Point. A code point is a numeric value that refers to a unique character in the Unicode standard, which contains the usual letters A-Z and a-z as well as special characters like the euro sign (€), the joined "ae" (æ), and many others. Using a code point makes it possible for special characters to be displayed correctly when they are shared on many different platforms and operating systems since the Unicode standard is available pretty much everywhere. I'm not sure how InDesign generates XML, but it might be worth it to make sure those ids don't have a special meaning before you append to/alter them in any way.
Second, though it sounds like your id generating scheme might be enough for your needs, you might want to consider using a library to generate unique ids that don't depend on the other ids in the XML. It's not always easy to guarantee that ids you generate in a script will remain unique. If, for example, you merge in nodes that you add with other new ones from the server, there could be clashes. If you're using javascript to process the XML, there are npm libraries like uuid or uniqid that can help. Something to consider.
Finally, if you really need to generate the ids yourself in javascript, you'll want to extract the hex numbers from the "u"-prefixed values, convert to integers, find the highest integer, generate a new integer incremented by 1 (or more), then convert the new integer back to hex and format it with the "u" prefix. This can be done by using map and sort methods on the array, as well as some slightly tricky hex-to-int-to-hex conversion accomplished by converting hex strings to numbers and vice-versa. I'll point you in the right direction by suggesting you look into the parseInt function and the Number.prototype.toString method. A great resource to find info about all of these functions and methods is the MDN Javascript Docs (https://developer.mozilla.org/en-US/docs/Web/JavaScript).
I hope this gives you a good start toward solving your unique ID issue. Let me know if you need any more help.