macros for fixed length string from integer, or short string

Share your advanced PureBasic knowledge/code with the community.
eck49
Enthusiast
Enthusiast
Posts: 153
Joined: Sat Nov 14, 2020 10:08 pm
Location: England

macros for fixed length string from integer, or short string

Post by eck49 »

Three simple macros (again just to save typing/typos) to create a string of a fixed length whatever the size or length of the integer/string fed in.
This helps to create a neat and tidy lay out when writing data to a file or in simple printing. The source code does not look as messy, either!
NB There is no check to make sure the fixed length is actually long enough, it is assumed the user knows the range of the input.

Code: Select all

;2021-01-24 new
;macro to deliver integer right-justified in fixed length string
;i is the integer and n the fixed length
Macro UStrX(i, n)
  Right(Space(n) + Str(i), n)
EndMacro

;2021-01-27 new
;macro to deliver string left justified in fixed length string
;st is the string and n the fixed length
Macro UStrLS(st,n)
  Left(st + Space(n), n)
EndMacro

;2021-01-27 new
;macro to deliver string right-justified in fixed length string
;st is the string and n the fixed length
Macro UStrS(st, n)
  Right(Space(n) + st, n)
EndMacro
Ubuntu 22.04 64-bit
Purebasic 6.00 (as of 5 Sep 2022)
(native tongue: English)
User avatar
STARGÅTE
Addict
Addict
Posts: 2234
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: macros for fixed length string from integer, or short st

Post by STARGÅTE »

What is wrong with PB native functions: RSet() and LSet()? Did you miss them?
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and moreTypeface - Sprite-based font include/module
eck49
Enthusiast
Enthusiast
Posts: 153
Joined: Sat Nov 14, 2020 10:08 pm
Location: England

Re: macros for fixed length string from integer, or short st

Post by eck49 »

@STARGATE
I did, but it just shows how hard it is to frame a search. The terms I used did not turn these up at the time.
Ubuntu 22.04 64-bit
Purebasic 6.00 (as of 5 Sep 2022)
(native tongue: English)
User avatar
Tenaja
Addict
Addict
Posts: 1959
Joined: Tue Nov 09, 2010 10:15 pm

Re: macros for fixed length string from integer, or short st

Post by Tenaja »

You need to hang out in the "library" indexes; in this case:

https://www.purebasic.com/documentation ... index.html
Post Reply