The reason why hydrogen gas has a much higher energy of combustion per gram than octane is because hydrogen gas has a much lower molecular weight than octane.
The energy of combustion per mole is a measure of the energy released when one mole of a substance is completely burned in oxygen. However, the energy released during combustion is also related to the mass of the substance being burned.
Hydrogen gas has a molecular weight of 2.02 g/mol, while octane has a molecular weight of 114.23 g/mol. This means that for the same number of moles of hydrogen gas and octane, the mass of hydrogen gas will be much lower than the mass of octane.
For example, if we consider 1 mole of each substance, the mass of hydrogen gas will be 2.02 g, while the mass of octane will be 114.23 g. If we burn both substances completely, the energy released by the combustion of hydrogen gas will be distributed across a much lower mass than the energy released by the combustion of octane. This means that the energy released per gram of hydrogen gas will be much higher than the energy released per gram of octane.
Therefore, even though octane has a higher energy of combustion per mole than hydrogen gas, hydrogen gas has a much higher energy of combustion per gram because of its lower molecular weight.