## 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

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: 9408
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

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

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

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 tstEnd Function`
counting_pine
Posts: 6176
Joined: Jul 05, 2005 17:32
Location: Manchester, Lancs

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

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 tstEnd Functionprint now()print Str2SerNum(format(now, "yyyymmddhhmmss"))`

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

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

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: 6084
Joined: Jan 10, 2006 20:30
Location: Scotland

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

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: 9408
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

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

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
Posts: 6176
Joined: Jul 05, 2005 17:32
Location: Manchester, Lancs

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

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: 6084
Joined: Jan 10, 2006 20:30
Location: Scotland

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

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: 836
Joined: Oct 19, 2017 15:00
Location: 't Zand, NL
Contact:

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

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
Posts: 6176
Joined: Jul 05, 2005 17:32
Location: Manchester, Lancs

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

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: 836
Joined: Oct 19, 2017 15:00
Location: 't Zand, NL
Contact:

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

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