How to convert String (or Double) to SerialNumber

New to FreeBASIC? Post your questions here.
newbieforever
Posts: 117
Joined: Jun 21, 2018 11:14

How to convert String (or Double) to SerialNumber

Postby newbieforever » Feb 17, 2019 13:28

Hi!

If tst0 is a timestamp of a file, e.g. 20190217142630, defined as String (or as Double), how to convert it to tst as SerialNumber?
fxm
Posts: 9062
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: How to convert String (or Double) to SerialNumber

Postby fxm » Feb 17, 2019 14:53

There are several possible methods.
See at Date Serials.
newbieforever
Posts: 117
Joined: Jun 21, 2018 11:14

Re: How to convert String (or Double) to SerialNumber

Postby newbieforever » Feb 17, 2019 20:07

Thank you, fxm!

Code: Select all

Function Str2SerNum(ss As String) As Double
  Dim As Integer ye = Val(Mid(ss, 1, 4))
  Dim As Integer mo = Val(Mid(ss, 5, 2))
  Dim As Integer da = Val(Mid(ss, 7, 2))
  Dim As Integer ho = Val(Mid(ss, 9, 2))
  Dim As Integer mi = Val(Mid(ss, 11, 2))
  Dim As Integer se = Val(Mid(ss, 13, 2))
  Dim As Double  tst = DATESERIAL(ye, mo, da) + TIMESERIAL(ho, mi, se)
  Return tst
End Function
counting_pine
Site Admin
Posts: 6170
Joined: Jul 05, 2005 17:32
Location: Manchester, Lancs

Re: How to convert String (or Double) to SerialNumber

Postby counting_pine » Feb 17, 2019 21:42

If you're happy to use the C runtime, you could use sscanf:

Code: Select all

#include "crt.bi"
#include "vbcompat.bi"

Function Str2SerNum(ss As String) As Double
  dim as long ye, mo, da, ho, mi, se
  sscanf(ss, "%4d%2d%2d%2d%2d%2d", @ye,@mo,@da,@ho,@mi,@se)
  Dim As Double  tst = DATESERIAL(ye, mo, da) + TIMESERIAL(ho, mi, se)
  Return tst
End Function

print now()
print Str2SerNum(format(now, "yyyymmddhhmmss"))

Otherwise, I'd recommend CInt(Mid(...)) instead of Val(Mid(...)), to avoid the floating-point math.
Munair
Posts: 834
Joined: Oct 19, 2017 15:00
Location: 't Zand, NL
Contact:

Re: How to convert String (or Double) to SerialNumber

Postby Munair » Feb 18, 2019 6:17

counting_pine wrote:Otherwise, I'd recommend CInt(Mid(...)) instead of Val(Mid(...)), to avoid the floating-point math.

Why not use ValInt or in this case ValUInt directly?
dodicat
Posts: 5879
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: How to convert String (or Double) to SerialNumber

Postby dodicat » Feb 18, 2019 12:41

cint uses valint/vallng for strings anyway.
-- and it is easier to type in.

Code: Select all

#undef valint
#undef vallng

#ifdef valint
#undef valint
#endif

#ifdef vallng
#undef vallng
#endif

  'print valint("12.34")  'gone
  'print vallng("12.34") 'gone
 print cint("12.34")    'also gone
 sleep 
fxm
Posts: 9062
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: How to convert String (or Double) to SerialNumber

Postby fxm » Feb 18, 2019 13:07

The keyword-name Val[U]Int / Val[U]Lng can be misleading because the conversion is done to [U]Long / [U]LongInt (and not to [U]Int / [U]Long), whereas the name is consistent for C[U]Int / C[U]Lng (and the others Cx).
counting_pine
Site Admin
Posts: 6170
Joined: Jul 05, 2005 17:32
Location: Manchester, Lancs

Re: How to convert String (or Double) to SerialNumber

Postby counting_pine » Feb 18, 2019 13:47

Yeah, I used to recommend Valint(), but the legacy naming issues give some uncertainty about the size of the type returned. So Cint/Clng/Clngint/... is clearer, if you're happy with the idea of using them for conversions from string. That's something QB didn't support. (But then it didn't support Valint/... either.)
dodicat
Posts: 5879
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: How to convert String (or Double) to SerialNumber

Postby dodicat » Feb 18, 2019 16:31

Here is a summary

Code: Select all

 #undef valint
#undef vallng
#undef valuint
#undef valulng

#ifdef valint
#undef valint
#endif

#ifdef vallng
#undef vallng
#endif

#ifdef valuint
#undef valuint
#endif


#ifdef valulng
#undef valulng
#endif

 
 print cint("12.34")     'uses vallng
 print cuint("12.34")    'uses valulng
 print culng("12.34")    'uses valuint
 print clngint("12.34")  'uses vallng
 print clng("12.34")     'uses valint
 print culngint("12.34") 'uses valulng
 sleep 


Proposed verse:

Change int to long
Change long to int
and to add a final hint . . .
valulng does ulongint
Munair
Posts: 834
Joined: Oct 19, 2017 15:00
Location: 't Zand, NL
Contact:

Re: How to convert String (or Double) to SerialNumber

Postby Munair » Feb 18, 2019 18:40

Coming from QuickBASIC I find CInt() much more misleading for string to number conversion as in QB its only function was to round numeric expressions. In this respect Val() is more consistent in that it is traditionally associated with string to number conversion.
counting_pine
Site Admin
Posts: 6170
Joined: Jul 05, 2005 17:32
Location: Manchester, Lancs

Re: How to convert String (or Double) to SerialNumber

Postby counting_pine » Feb 18, 2019 19:42

Cint() in QB wasn’t simply used for mathematical rounding, but for type conversion. It was limited to results between -32768 and 32767. Clng() had a wider type, but still couldn’t convert/round every possible double-precision value.
I agree that val(mid()) is the most familiar way for QB users, but I can’t recommend the habitual use of it in FB, due to its inefficiency. Cint(mid()) may not feel right to a QB veteran, but it’s a better choice for a conversion idiom in FB.
Munair
Posts: 834
Joined: Oct 19, 2017 15:00
Location: 't Zand, NL
Contact:

Re: How to convert String (or Double) to SerialNumber

Postby Munair » Feb 19, 2019 6:56

Yes, I forgot to include that QB's CInt in the first place converts numeric expressions to integer.

Return to “Beginners”

Who is online

Users browsing this forum: No registered users and 7 guests