How to convert String (or Double) to SerialNumber
-
- 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?
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?
Re: How to convert String (or Double) to SerialNumber
There are several possible methods.
See at Date Serials.
See at Date Serials.
-
- 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 tst
End Function
-
- Site Admin
- Posts: 6323
- 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:
Otherwise, I'd recommend CInt(Mid(...)) instead of Val(Mid(...)), to avoid the floating-point math.
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"))
Re: How to convert String (or Double) to SerialNumber
Why not use ValInt or in this case ValUInt directly?counting_pine wrote:Otherwise, I'd recommend CInt(Mid(...)) instead of Val(Mid(...)), to avoid the floating-point math.
Re: How to convert String (or Double) to SerialNumber
cint uses valint/vallng for strings anyway.
-- and it is easier to type in.
-- 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
Re: How to convert String (or Double) to SerialNumber
The keyword-name ValInt / ValLng can be misleading because the conversion is done to Long / LongInt (and not to Int / Long), whereas the name is consistent for CInt / CLng (and the others Cx).
-
- Site Admin
- Posts: 6323
- 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.)
Re: How to convert String (or Double) to SerialNumber
Here is a summary
Proposed verse:
Change int to long
Change long to int
and to add a final hint . . .
valulng does ulongint
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
Change int to long
Change long to int
and to add a final hint . . .
valulng does ulongint
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.
-
- Site Admin
- Posts: 6323
- 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.
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.
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.