I'm working on a CAN bus project, and trying to send a message to set the time and date. I've worked out how the message needs to be formatted, grabbed the date/time and stored in variables. I've converted them to hex just fine, but I need to add leading 0s to pad to the required space required by the message.
I've tried methods seen online for a bash script, but I have a problem: The year 2019 is 7E3 in hex. I need this displayed as 07E3. When using awk to add the leading 0s, the E3 is being interpreted as Engineering notation *10^3, and therefore printing as 7000. My script is below, along with an image showing the CAN bus message format. Any help is appreciated.
#!/bin/bash
#Store Date/Time in Variables
Year=`date '+%Y'`
Month=`date '+%m'`
Day=`date '+%d'`
Hour=`date '+%H'`
Minute=`date '+%M'`
Second=`date '+%S'`
#Convert Date/Time to Hexadecimel
HexYear=`echo "ibase=10;obase=16;$Year"| bc | awk '{ printf("%04d\n", $1) }'`
HexMonth=`echo "ibase=10;obase=16;$Month"| bc | awk '{ printf("%02d\n", $1) }'`
HexDay=`echo "ibase=10;obase=16;$Day"| bc | awk '{ printf("%02d\n", $1) }'`
HexHour=`echo "ibase=10;obase=16;$Hour"| bc | awk '{ printf("%02d\n", $1) }'`
HexMinute=`echo "ibase=10;obase=16;$Minute"| bc | awk '{ printf("%02d\n", $1) }'`
HexSecond=`echo "ibase=10;obase=16;$Second"| bc | awk '{ printf("%02d\n", $1) }'`
echo "The following is Decimel > Hex"
echo "$Year > $HexYear"
echo "$Month > $HexMonth"
echo "$Day > $HexDay"
echo "$Hour > $HexHour"
echo "$Minute > $HexMinute"
echo "$Second > $HexSecond"