in reply to string, or integer?

pack 'n', length($msg)
(Replace n with N or S or whatever depending on what order (if any) you need the two bytes in