Format string with some values like sprintf (C) - Update

Share your advanced PureBasic knowledge/code with the community.
User avatar
DoubleDutch
Addict
Addict
Posts: 3219
Joined: Thu Aug 07, 2003 7:01 pm
Location: United Kingdom
Contact:

Post by DoubleDutch »

Wow! I didn't know about that using the single quotes automatically converted to the ASCII of the character. :D

Is it in the help file?
https://deluxepixel.com <- My Business website
https://reportcomplete.com <- School end of term reports system
User avatar
Demivec
Addict
Addict
Posts: 4091
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Post by Demivec »

DoubleDutch wrote:Wow! I didn't know about that using the single quotes automatically converted to the ASCII of the character. :D

Is it in the help file?
The only place I could find it mentioned was under the topic of the "Asc()" function. It is a function that operates at compile-time and is used with constants.
User avatar
mk-soft
Always Here
Always Here
Posts: 5409
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Post by mk-soft »

Thank´s ebs.

Opps, i have found big bug by "\n" and "\r", "\n" is newline not carrie line :oops:

New:
- Added hexadecimal out "%x" and "%X"

All Updates at first site.

GT :wink:
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
Little John
Addict
Addict
Posts: 4527
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Post by Little John »

mk-soft wrote:Opps, i have found big bug by "\n" and "\r", "\n" is newline not carrie line :oops:
Maybe you are meaning: "\n is line feed not carriage return."?
That's right, but "\n" does not always mean new line. ;)
For denoting a new line, Mac uses "\r", *nix uses "\n", and windows uses "\r\n".

Regards, Little John
User avatar
mk-soft
Always Here
Always Here
Posts: 5409
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Post by mk-soft »

Update v1.06
- compatible for X86 and X64
- added type integer (%i)

GT :wink:
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
IdeasVacuum
Always Here
Always Here
Posts: 6425
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: Format string with some values like sprintf (C) - Update

Post by IdeasVacuum »

Very nice work - would be great to see it formally added to PB.
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
User avatar
mk-soft
Always Here
Always Here
Posts: 5409
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Format string with some values like sprintf (C) - Update

Post by mk-soft »

Update v2.02
complete new code 8)

Update v2.03
- Bugfix: hex types

Code: Select all

valuelong.l = $12341FE0
valueword.w = $EEEE
valueinteger.i = $12345

Debug Format("%X", @valueinteger) ; Fehler
Debug Format("%12.8X", @valuelong) ; Breite 12, Type Long
Debug Format("%'#12.8X", @valuelong) ; Auffüllung mit '#' ; Breite 12, Type Long
Debug Format("%4X", @valueword) ; Ohne Breite ist Type ; Type Word
Debug Format("%8X", @valuelong)  ; Ohne Breite ist Type ; Type Long
Debug Format("%'#12.4X", @valueword)  ; Auffüllung mit '#' ; Breite 12, Type Word
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
User avatar
pcfreak
User
User
Posts: 75
Joined: Sat May 22, 2004 1:38 am

Re: Format string with some values like sprintf (C) - Update

Post by pcfreak »

I'd replace the hex value check with

Code: Select all

                Select num2
                  Case 0 To 2  : help = RSet ( Hex ( PeekB ( *value ), #PB_Byte), num2, "0" )
                  Case 3 To 4  : help = RSet ( Hex ( PeekW ( *value ), #PB_Word), num2, "0" )
                  Case 5 To 8  : help = RSet ( Hex ( PeekL ( *value ), #PB_Long), num2, "0" )
                  Default      : help = RSet ( Hex ( PeekQ ( *value ), #PB_Quad), num2, "0" )
                EndSelect
to make it possible to use other lengths.
User avatar
mk-soft
Always Here
Always Here
Posts: 5409
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Format string with some values like sprintf (C) - Update

Post by mk-soft »

@pcfreak

updated with your code

Thanks
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
VoSs2o0o
User
User
Posts: 24
Joined: Fri Aug 06, 2010 11:46 pm

Re: Format string with some values like sprintf (C) - Update

Post by VoSs2o0o »

I use your sprintf in a lot of codes.

For more flexibility please add

Code: Select all

Case '%' : result + "%"
so we can mask the percent-char, e.g.

Code: Select all

this is a testcode works \%100\% 
IdeasVacuum
Always Here
Always Here
Posts: 6425
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: Format string with some values like sprintf (C) - Update

Post by IdeasVacuum »

I think this sprintf function should be added to PB - how about it Fred?
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
Rinzwind
Enthusiast
Enthusiast
Posts: 638
Joined: Wed Mar 11, 2009 4:06 pm
Location: NL

Re: Format string with some values like sprintf (C) - Update

Post by Rinzwind »

It's really nothing... but a handy small partner procedure

Code: Select all

Procedure.s FormatQ ( text.s, *value1 = 0, *value2 = 0, *value3 = 0, *value4 = 0, *value5 = 0, *value6 = 0, *value7 = 0, *value8 = 0, *value9 = 0, *value10 = 0, *value11 = 0 )
  ReplaceString(text, "'", #DOUBLEQUOTE$, #PB_String_InPlace)
  ProcedureReturn Format(text, *value1, *value2, *value3, *value4, *value5, *value6, *value7, *value8, *value9, *value10, *value11)
EndProcedure
Makes handling " characters easier.
User avatar
mk-soft
Always Here
Always Here
Posts: 5409
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Format string with some values like sprintf (C) - Update

Post by mk-soft »

Handle ["] over [\']
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
User avatar
mk-soft
Always Here
Always Here
Posts: 5409
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Format string with some values like sprintf (C) - Update

Post by mk-soft »

Hello,

after a long time a new update

Update v2.07
- added: unsigned byte
- added: unsigned word
- added: character '\%'
- change: remove most peek functions

Example

Code: Select all

IncludeFile "Format-v207.pbi"

bVal.b = 100
wVal.w = 200
lVal.l = 300
iVal.i = 400
fVal.f = 500.0
dVal.d = 600.0
sVal.s = "Hello World"
aVal.a = 255
uVal.u = 65535

temp.s = "Result: \nByte = %b, \nWord = %w, \nLong = %l, \nInteger = %i, \nFloat = %10.2f, \nDouble = %+10.2d, \n"
temp + "String = %s, \nUnsigned Byte = %a, \nUnsigned Word = %u,\n"
temp + "Hex = %2x, \nHex = %4X\n"

result.s = format(temp, @bVal, @wVal, @lVal, @iVal, @fVal, @dVal, @sVal, @aVal, @uVal, @aVal, @uVal)
MessageRequester("Format like sprintf", result)
:wink:

P.S. New Ref v2.07 Ref 2
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
said
Enthusiast
Enthusiast
Posts: 342
Joined: Thu Apr 14, 2011 6:07 pm

Re: Format string with some values like sprintf (C) - Update

Post by said »

Thanks for sharing. I think there is a bug with hex!

trying your example:

Code: Select all

temp.s = "Result: \nByte = %b, \nWord = %w, \nLong = %l, \nInteger = %i, \nFloat = %10.2f, \nDouble = %+10.2d, \n"
temp + "String = %s, \nUnsigned Byte = %a, \nUnsigned Word = %u,\n"
temp + "Hex = %2x, \nHex = %8X\n"   ; <--- chaning to 8X

i am getting 00C8FFFF instead of 0000FFFF ?!
Post Reply