I don't know of any dress code not allowing either one, unless that is the only article of clothing that you are wearing than there may be a problem. The officers seem to always wear hats and pirates wear bandanas so they are both are of naval background. The worst that might happen in that you are asked to remove either one at dinner.
What about wearing a bandana off of the ship when we port. Will the natives be offended? I just feel most comfortable witha bandana on. My head burns in the sun quickly. Will I have a problem with this. And a bandana is not all I will be wearing. Trust me, if it was I would offend more than just the natives (ooohhh yuk).