Tip: Forcing fixed-width characters

There are times when you want to use fixed-width characters when you don’t know or can’t control the font, or you want to mix with proportional-width easily. The Unicode character set has you covered.

The Unicode codes &uFF01-&uFF5E are fixed-width versions of the visible ASCII range, and conversion is easy. Simple apply the formula s.Asc + &hFF00 - 32 to get the code. For example:

var s as string = "A"
s = Chr( s.Asc + &hFF00 - 32 ) // A fixed-width "A"

The space is less obvious as it’s not in that range, but you can use &u3000 for a fixed-width space.

6 Likes

Yes, I’ve seen some of these chars in spam mails - they look like ordinary chars but are used to beat spam detection. Each is 3 bytes long in UTF8, so be aware.

Nice to know, thanks.
Since accented characters are varying in different encodings, which range would work for them as fixed-width characters?

I don’t think there is one.

Sad. Only english benefits from this, then.

It has limited uses to be sure. I have used this to print debug info that needs to line up.

1 Like

Yes, these are still valid uses.

I’ve created this function as a convenience:

Public Function ToFixedWidth(Extends s As String) As String
  var chars() as string = s.Split( "" )
  
  for i as integer = 0 to chars.LastIndex
    var char as string = chars( i )
    var code as integer = char.Asc
    
    if code > 32 and code < 127 then
      chars( i ) = Chr( code + &hFF00 - 32 )
    end if
  next
  
  s = String.FromArray( chars, "" )
  s = s.ReplaceAll( &u0020, &u3000 )
  
  return s
  
End Function
5 Likes