Script to Convert Base-10 numeric to Base-X
Description
This article presents a script that will convert a base-10 numeric (decimal) to any base between base-2 (binary) to base-36 (0-9,A-Z).
Steps:
- Create three keyboard data source fields on the label (Input, Output, & Base).
- Create a script data source field on the label (Script).
- Use the following script:
Copy
Sample Script
<pre class="syntaxhighlighter-pre" xml:space="preserve">// pull input data from label
var input=Number(label.fields.field.(@name=='Input').@data);
var x=Number(label.fields.field.(@name=='Base').@data);
// initialize variables and set base36 array
var base36 = new Array("0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z");
var output=new String;
var y=0;
// error handling (not a number)
if(isNaN(input)==true || isNaN(x) ==true)
{
output="Value entered is not a number";
}
else
{
// error handling (not an integer)
if (Math.floor(input) !=input || Math.floor(x)!=x)
{
output="Value entered is not an integer";
}
else
{
// error handling (not a valid integer)
if (input<0 || x<0 || x>36)
{
output="Value entered is not a valid integer";
}
else
{
// determine how many digits will be required
while (Math.pow(x,y)<=input)
{
y++;
}
y-=1;
// calculate digits
for (y=y;y>=1;y--)
{
var digit=Math.floor(input/Math.pow(x,y));
output+=String(base36[digit]);
input-=digit*Math.pow(x,y);
}
output+=String(base36[input]);
}
}
}
// ouptut to label
label.fields.field.(@name=='Output').@data=output;
</pre>
Article Number
2016014
Versions
Loftware Label A label is a design area on the computer screen where a label format is created or edited. Manager / Loftware Print Server versions 9.5.2.0 - 11.1
Environment
All supported installation environments.