If Statement for Converting HEX to BYTE.

Thread Starter

DaveInNevada

Joined Apr 15, 2021
7
I have 58 Sound FX, Music, and Voice files of 2-Digit HEX Audio data that I have stored into Flash Memory(W25Q64-JV) for a 2nd version of a Game I'm building. My program, using Arduino Nano, converts the HEX to Binary and writes it to the Flash. Then, during game-play, it reads from the W25Q64 and streams the Sound to a resistor-ladder, then into my OpAmp. Everything works great.

I found this code(below) for the conversion.

Example Data --> 7F7B808182..... (This is just a txt file.)
(I grab 2 Bytes at a time) Example Data --> hexValue[0]='7' and hexValue[1]='F'.

hexValue[0]=dataFile.read(); // Read 1st Hex Digit.
hexValue[1]=dataFile.read(); // Read 2nd Hex Digit.

Using code that works is one thing, but understanding it is another. I want to understand this below, but can't find any explanation online.

byte tens = (hexValue[0] < '9') ? hexValue[0] - '0' : hexValue[0] - '7'; (Why does this work? )
byte ones = (hexValue[1] < '9') ? hexValue[1] - '0' : hexValue[1] - '7'; (Why does this work? )
byte number = (16 * tens) + ones; (I understand this.)

I understand the last line, simple HEX math. But the 2 above that confuse me. It appears to be some kind of conditional statement, but It confuses me. Any feedback would be appreciated. Thanks.
 

MrChips

Joined Oct 2, 2009
23,528
? : is called a ternary conditional operator.

Reference: https://en.wikipedia.org/wiki/?:

Here is the syntax.

expression ? value1 : value2

expression is evaluated resulting in a boolean TRUE or FALSE.
If the result is TRUE then the operator returns value1.
If the result is FALSE then the operator returns value2.

This is a compact way of saying

if expression
then
value1
else
value2
 

Thread Starter

DaveInNevada

Joined Apr 15, 2021
7
? : is called a ternary conditional operator.

Reference: https://en.wikipedia.org/wiki/?:

Here is the syntax.

expression ? value1 : value2

expression is evaluated resulting in a boolean TRUE or FALSE.
If the result is TRUE then the operator returns value1.
If the result is FALSE then the operator returns value2.

This is a compact way of saying

if expression
then
value1
else
value2
Ah.. Thanks for your reply. I like the compactness of that, and will certainly use it. Part of my confusion, however, is in this part of the statement: (hexValue[0] < '9') . Shouldn't it be <=9 ? And for a HEX value of say, 'F', why would you subtract '7'?
 

MrChips

Joined Oct 2, 2009
23,528
Look up the ASCII Table.

"0123456789ABCDEF" are stored as text.
"0" is 48
:
"9" is 57
:
"A" is 65
:
"F" is 70

You will notice that there are seven additional characters between "9" and "A" that are not hex characters.
For "0" to "9" we simply subtract 48.
For "A" to "F" we subtract 48 + 7 (i.e. 55).
 

Thread Starter

DaveInNevada

Joined Apr 15, 2021
7
Look up the ASCII Table.

"0123456789ABCDEF" are stored as text.
"0" is 48
:
"9" is 57
:
"A" is 65
:
"F" is 70

You will notice that there are seven additional characters between "9" and "A" that are not hex characters.
For "0" to "9" we simply subtract 48.
For "A" to "F" we subtract 48 + 7 (i.e. 55).
That is very clever. I understand the math now, except for '9'. If we are only subtracting 48 for characters '0' through '9', shouldn't the code be "<=9 " rather than "<9"? If the character is a '9', it would subtract 55 rather than 48?
 

BobaMosfet

Joined Jul 1, 2009
1,776
I have 58 Sound FX, Music, and Voice files of 2-Digit HEX Audio data that I have stored into Flash Memory(W25Q64-JV) for a 2nd version of a Game I'm building. My program, using Arduino Nano, converts the HEX to Binary and writes it to the Flash. Then, during game-play, it reads from the W25Q64 and streams the Sound to a resistor-ladder, then into my OpAmp. Everything works great.

I found this code(below) for the conversion.

Example Data --> 7F7B808182..... (This is just a txt file.)
(I grab 2 Bytes at a time) Example Data --> hexValue[0]='7' and hexValue[1]='F'.

hexValue[0]=dataFile.read(); // Read 1st Hex Digit.
hexValue[1]=dataFile.read(); // Read 2nd Hex Digit.

Using code that works is one thing, but understanding it is another. I want to understand this below, but can't find any explanation online.

byte tens = (hexValue[0] < '9') ? hexValue[0] - '0' : hexValue[0] - '7'; (Why does this work? )
byte ones = (hexValue[1] < '9') ? hexValue[1] - '0' : hexValue[1] - '7'; (Why does this work? )
byte number = (16 * tens) + ones; (I understand this.)

I understand the last line, simple HEX math. But the 2 above that confuse me. It appears to be some kind of conditional statement, but It confuses me. Any feedback would be appreciated. Thanks.
Hex isn't hard (follow the bit columns vertically- the heavier lines divide the 8-bit byte into it's nybble equivalents for each bae (decimal, hex, or octal):

1621258539128.png

The code you're looking at is using bit shifting (ie <) and a terniary (ie ? and : ) operators to do the operation. The value of each bit is shown in the upper block, according to its base (shown on the right). The binary shows an 8-bit example value, and the bottom block shows that binary value converted, according to base, using the bit assigns from the upper block.

Here

Title: Standard C [Quick Ref]
Author(s): P.J.Plauger, Jim Brodie
ISBN: 1-55615-158-6

Just remember, the computer doesn't know anything about base. Bases are solely a convenience for humans. All the computer knows is binary. Get the binary right, and the data is right. Period.
 

MrChips

Joined Oct 2, 2009
23,528
That is very clever. I understand the math now, except for '9'. If we are only subtracting 48 for characters '0' through '9', shouldn't the code be "<=9 " rather than "<9"? If the character is a '9', it would subtract 55 rather than 48?
Yes, you are correct.
It should read

character <= '9'
 

Thread Starter

DaveInNevada

Joined Apr 15, 2021
7
Yes, you are correct.
It should read

character <= '9'
I ran a test file using my original code. When a char '9' appears, it produced bad data. All other HEX char worked. I changed the '<9' to '<=9', and that corrected that particular issue.
Thanks you so much for your help. Very instructive. I will be using ternary conditional operators in the future.
I am fairly new to this community; Do I need to close out this post somehow, or give you credit in some way. Please let me know, otherwise Thanks again for your help.
 

Thread Starter

DaveInNevada

Joined Apr 15, 2021
7
Hex isn't hard (follow the bit columns vertically- the heavier lines divide the 8-bit byte into it's nybble equivalents for each bae (decimal, hex, or octal):

View attachment 238757

The code you're looking at is using bit shifting (ie <) and a terniary (ie ? and : ) operators to do the operation. The value of each bit is shown in the upper block, according to its base (shown on the right). The binary shows an 8-bit example value, and the bottom block shows that binary value converted, according to base, using the bit assigns from the upper block.

Here

Title: Standard C [Quick Ref]
Author(s): P.J.Plauger, Jim Brodie
ISBN: 1-55615-158-6

Just remember, the computer doesn't know anything about base. Bases are solely a convenience for humans. All the computer knows is binary. Get the binary right, and the data is right. Period.
I'm not using Bit shifting in this scenario. '<', not '<<'. I do appreciate your time and response, however :) "MrChips" was able to explain the use of ternary conditional operators to me. I am thankful for this community. Thanks again.
 

MrChips

Joined Oct 2, 2009
23,528
You don't have to close a thread. This allows others to chime in with fresh comments. Posting a Reply to a thread months or years later has to be done with due consideration to the age of the thread.

Simply click on the Like button to show your appreciation.
 
Top