Real-world Conversion Tricks
Network Address Conversion
MAC Address Decoding
## Convert MAC address hex to readable format
mac_hex="001122334455"
mac_formatted=$(echo $mac_hex | sed 's/\(..\)/\1:/g' | sed 's/:$//')
echo $mac_formatted ## Outputs: 00:11:22:33:44:55
Color Manipulation
Hex Color Conversion
## Convert hex color to RGB
hex_color="FF6600"
r=$((16#${hex_color:0:2}))
g=$((16#${hex_color:2:2}))
b=$((16#${hex_color:4:2}))
echo "RGB: $r, $g, $b" ## Outputs: RGB: 255, 102, 0
Cryptographic Encoding
Hash Conversion
## Convert hex hash to binary
hex_hash="a1b2c3d4e5f6"
binary_hash=$(xxd -r -p <<< $hex_hash | xxd -b -c 1)
echo "$binary_hash"
Decoding System Identifiers
## Convert system UUID
system_uuid=$(cat /sys/class/dmi/id/product_uuid)
hex_uuid=$(echo $system_uuid | tr -d '-')
echo $hex_uuid
Conversion Workflow
graph TD
A[Hex Input] --> B{Conversion Type}
B --> |Network| C[MAC Address]
B --> |Color| D[RGB Values]
B --> |Crypto| E[Binary Representation]
B --> |System| F[Unique Identifiers]
Advanced Conversion Techniques
Conversion Type |
Command |
Purpose |
Hex to Base64 |
echo $hex_value | xxd -r -p | base64 |
Encoding |
Hex to ASCII |
echo $hex_value | xxd -r -p |
String Decoding |
Hex Padding |
printf "%08x" $decimal_value |
Formatting |
Bulk Hex Conversion Script
#!/bin/bash
## Efficient hex conversion utility for LabEx environments
convert_hex() {
local input_type=$1
local hex_value=$2
case $input_type in
decimal) printf "%x\n" $hex_value ;;
ascii) echo $hex_value | xxd -r -p ;;
binary) echo "obase=2; ibase=16; $hex_value" | bc ;;
*) echo "Unsupported conversion" ;;
esac
}
## Example usage
convert_hex decimal 255
convert_hex ascii "48656C6C6F"
Error Handling and Validation
validate_hex() {
[[ "$1" =~ ^[0-9A-Fa-f]+$ ]] && return 0 || return 1
}
safe_convert() {
local hex_input=$1
if validate_hex "$hex_input"; then
echo $((16#$hex_input))
else
echo "Invalid hex input"
fi
}