Our World Statistics Day conversations have been a great reminder of how much statistics can inform our lives. Do you have an example of how statistics has made a difference in your life? Share your story with the Community!
Sep 26, 2019 9:30 AM
| Last Modified: Dec 9, 2019 6:32 AM
Converting a value from one base or radix to another is often required with data in external formats such as sensor or network data. JMP has supported converting from hexadecimal to decimal and back using the Hex and Hex to Number functions. For example:
With JMP 15, these functions have been expanded to support any base from 2 to 36. By specifying the Base option on the Hex function, the decimal number will be converted to that base rather than the default base of 16.
Often with binary data, you want the values to be padded to byte boundaries for consistency. The new Pad To option allows that.
Padded to 2-bytes:
Hex(1234, Base(2), Pad To(16));
Padded to 4-bytes:
Hex(1234, Base(2), Pad To(32));
Hex To Number provides the reverse functionality when the Base option is specified.
Convert from binary to decimal:
binaryToDec = Hex To Number("10011010010", Base(2));
octalToDec = Hex To Number("2322", Base(8));
binaryToDec = 1234;
octalToDec = 1234;
QR codes (Quick Response codes) are two-dimensional bar codes that store small amounts of binary data that can be read using a scanner or smartphone. These codes were developed for inventory tracking, but they are often used now to encode location data for websites or applications. For example, here is a QR code for the JMP Discovery Summit in Tucson that links to the main web page of the conference.
QR Code for Discovery Summit Tucson 2019
The QR code consists of several fixed patterns (such as the three large square blocks for alignment) and the cell/content area for the data plus the error detection/correction information. QR codes come in a variety of sizes and have varying levels of error correction. Those details are not important for this exercise.
Let’s imagine that you wanted to encode the URL for Discovery Summit Tucson 2019 into the bits needed for a QR code.
QR codes support four types of encoding: Numeric, Alphanumeric, Byte and Kanji. Since our URL is a combination of letters, numbers and symbols, we can use the Alphanumeric encoding. Alphanumeric coding only supports uppercase letters, but that’s OK since the URL is case-insensitive.
Unfortunately, the QR encodings for characters aren’t just the ASCII values. If they were, we could just use the Hex function to get the encoding. Instead, we will write a short JSL function to do the conversion based on the table below.
Here is the JSL function to do the conversion from a character to the alphanumeric encoded value.
The If statement maps the ASCII value to the encoded value using ranges. For example, the characters 0-9 are ASCII 48-57 and are encoded as 0-9. Therefore, those can be transformed from ASCII by simply subtracting 48. There are similar ranges for A-Z and a-z. The lowercase a-z are mapped to the values for the uppercase A-Z. The symbols jump around, so most are handled on an individual basis.
To save space, the QR code doesn’t directly write out the encoded value. It breaks the string into pairs of characters. For our URL, it would look like this:
ht tp s: // di sc ov er ys um mi t. jm p/ en /2 01 9/ us a/ ho me .h tm l
For each pair of characters, the encoded value for the first character is multiplied by 45 and added to the encoded value of the second character.
Our first pair is ‘ht’:
H = 17
T = 29
The result would be 17 * 45 + 29 = 794. This is then converted into an 11-bit binary value which is padded with zeros if necessary.
749 = 01011101101
If there is an odd number of characters, as in our case, the last character is just encoded and converted to a 6-bit binary value. In our case, we have a single ‘l’ at the end.
L = 21
21 = 010101
Converting numbers to binary and applying padding is exactly what the Hex function can do now. Let’s start by writing a function to calculate the encoded number for a pair of characters.
Now we can encode each pair from our URL to create the data for our QR code.
qrData = "";
For( i = 1, i <= Length( url ), i = i + 2,
charPair = Substr( url, i, 2 );
encodedValue = encodePair(charPair);
asBinary = If(Length(charPair) == 1,
Hex(encodedValue, Base( 2 ), Pad To (6)),
Hex(encodedValue, Base( 2 ), Pad To (11));
qrData = qrData || asBinary || " ";
In this JSL, two characters are taken from the URL with each pass of the For loop and the alphanumeric encoding is calculated on that pair. The Hex function converts the number to its binary representation. The value is padded to either 11-bits or 6-bits depending on whether there are 1 or 2 characters. The binary value is appended to the previous value. We added a space between the pairs to make the output easier to read. The space wouldn’t be included in the actual QR data.
There is a lot more that goes into creating the final output for a QR code like the encoding mode, the character count indicator, and error correction coding. That’s more than we need to cover to show the new features. Hopefully, this helped explain some of the new Hex and Hex To Number features and how they can be applied.