Anomalies, black strings and the charged Cardy formula