Posted: 03:36am 02 Oct 2019 |
Copy link to clipboard |
Print this post |
|
I've been playing around using unsigned integers as holders of bit flags and found trying to figure out which bit was which in a long binary string tiresome.
The two following routines break an integer down into groups of characters, either hex or binary for ease of use.
' Formatted hex and binary routines ' example nbr = 15616 chrs = 8 grp = 4
Print " Number ",nbr,"formatted in groups of ",grp,"characterss" Print FHex$(nbr,chrs,grp)
nbr = 299 chrs = 16 grp = 8
Print " Number ",nbr,"formatted in groups of ",grp,"bits" Print FBin$(nbr,chrs,grp)
Function FHex$(nbr,chrs,grp) ' formatted hex ' return 'nbr', as hex,'chrs' characters long in groups of 'grp' hex characters Local h$,fh$,hg,i h$ = Hex$(nbr,chrs) hg = Len(h$) For i = 1 To hg Step grp fh$ = (" "+Right$(h$,grp))+fh$ h$ = Left$(h$,Len(h$)-grp) Next i FHex$ = fh$ End Function
Function FBin$(nbr,chrs,grp) ' formatted binary ' return 'nbr', as binary, 'chrs' characters long in groups of 'grp' bits Local b$,fb$,bg,i b$ = Bin$(nbr,chrs) bg = Len(b$) For i = 1 To bg Step grp fb$ = (" "+Right$(b$,grp))+fb$ b$ = Left$(b$,Len(b$)-grp) Next i FBin$ = fb$ End Function
Hope they may be of some use to someone.
panky ... almost all of the Maximites, the MicromMites, the MM Extremes, the ArmMites, the PicoMite and loving it! |