Home
JAQForum Ver 20.06
Log In or Join  
Active Topics
Local Time 08:56 20 Apr 2024 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : Formatted hex and binary display

Author Message
panky

Guru

Joined: 02/10/2012
Location: Australia
Posts: 1094
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!
 
CaptainBoing

Guru

Joined: 07/09/2016
Location: United Kingdom
Posts: 1985
Posted: 06:32am 02 Oct 2019
Copy link to clipboard 
Print this post

Morning Panky. Yes I have found that too.

My solution was to simply print a "ruler" above the bit field but grouping is a nice idea


12-09-2019 07:22:57    6         5         4         3         2         1
12-09-2019 07:22:57 3210987654321098765432109876543210987654321098765432109876543210  Flags
12-09-2019 07:22:57 ----------------------------------------------------------------
12-09-2019 07:22:57 0000000000000000001000010001000000000000001000000000000000000000
 
Print this page


To reply to this topic, you need to log in.

© JAQ Software 2024