|
Post by alexfish on Dec 8, 2015 19:44:30 GMT 1
Hi Peter
Not sure about later versions of BaCon > 3.2
Can check this
BR Alex
' test for string or number char str[30] = "20.30300 This is test" LOCAL * str2 TYPE char
PROTO strod ret# = strtod(str,&str2)
PRINT ret# FORMAT "%lf\n" PRINT str2 FORMAT "%s\n"
PRINT "char test number : " , ret# FORMAT "%s %c\n" PRINT "char test string : " , str2 FORMAT "%s %c\n"
PRINT "char test number : "; PRINT ret# FORMAT " %c\n" PRINT "char test string : "; PRINT str2 FORMAT "%c\n"
|
|
|
Post by Pjot on Dec 8, 2015 20:33:50 GMT 1
Hi Alex,
If you use explicit formatting then you have to make sure the format string matches the variable type.
Your program cannot compile because "%c" cannot show a 'double' type.
The below should work:
' test for string or number char str[30] = "20.30300 This is test" LOCAL * str2 TYPE char
PROTO strod ret# = strtod(str,&str2)
PRINT ret# FORMAT "%lf\n" PRINT str2 FORMAT "%s\n"
PRINT "char test number : " , ret# FORMAT "%s %f\n" PRINT "char test string : " , str2 FORMAT "%s %s\n"
PRINT "char test number : "; PRINT ret# FORMAT " %f\n" PRINT "char test string : "; PRINT str2 FORMAT "%s\n"
HTH Peter
|
|
|
Post by alexfish on Dec 8, 2015 20:48:21 GMT 1
Hi Peter
on my system Raspberry PI2 there seems to be a problem with the print , yes your demo works ,
look at the two examples ,
' test for string or number char str[30] = "20.30300 This is test" LOCAL * str2 TYPE char
PROTO strod ret# = strtod(str,&str2)
PRINT ret# FORMAT "%lf\n" PRINT str2 FORMAT "%s\n"
PRINT "char test number : " , ret# FORMAT "%s %f\n" PRINT "char test string : " , str2 FORMAT "%s %s\n"
PRINT "char test number : "; PRINT ret# FORMAT " %f\n" PRINT "char test string : "; PRINT str2 FORMAT "%s\n"
Yes the above works on Raspy with the %f & %s :: Results'::
20.303000 This is test char test number : 20.303000 char test string : This is test char test number : 20.303000 char test string : This is test
Now My code
' test for string or number char str[30] = "20.30300 This is test" LOCAL * str2 TYPE char
PROTO strod ret# = strtod(str,&str2)
PRINT ret# FORMAT "%lf\n" PRINT str2 FORMAT "%s\n"
PRINT "char test number : " , ret# FORMAT "%s %c\n" PRINT "char test string : " , str2 FORMAT "%s %c\n"
PRINT "char test number : "; PRINT ret# FORMAT " %c\n" PRINT "char test string : "; PRINT str2 FORMAT "%c\n"
20.303000 This is test char test number : � <<< this bit ' �' does not show visually in the terminal char test string : char test number : ! char test string :
so can compare the inline format ; to the single print format all things been = then the inline Format should show at least something
Yet can see that the single print shows that that float is Number And that the String is not a Number By the presence of '!' and nothing as in NULL for the String.
BR Alex
|
|
|
Post by Pjot on Dec 8, 2015 21:10:07 GMT 1
Hi Alex,
Please note that C is a type-sensitive language, and needs to match formatting correctly during compile time (so not on-the-fly, during runtime).
Forcing a float value to be printed as unsigned char is literally asking for problems.
What will happen is that the first byte of the float value will be printed, but this first byte can contain any value between 0-255 and therefore may produce some bad random output.
Similar problems will occur when printing a pointer to a sequence character (=string). Because you print the first byte of the pointer, and not the first byte of the string itself.
On higher level, what is it that you try to achieve?
Thx Peter
|
|
|
Post by alexfish on Dec 8, 2015 21:32:32 GMT 1
Hi Peter
as the dem shows 'detect if number or string'
yes the Pointer = string but both will show as a number it is suggested the 'strtod( )' can be used as a method check that string is not a number. in this case can be seen. as in ! . IE the number can be of any value , but mod(255) . and the string = Null since it be a Pointer.
at the other high level , as seen by the results of the code
PRINT with Format is different is different to the single PRINT x , yet as mentioned @ Not sure if it be a BaCon Problem I would have thought the Two results be ==
Or is it System problem. or compiler problem. on Raspy.
how do your results stand.
BR Alex
|
|
|
Post by Pjot on Dec 9, 2015 20:17:06 GMT 1
Hi Alex, As far as I know there is no way in plain C to find out which variable contains a number, and which contains a string. Actually, I have the same problem in BaCon. During conversion time, BaCon registers all variables and their types. So when needed, it can lookup the type when needed (see also TYPEOF$ which makes use of the internal BaCon registration). Next to that, BaCon can check the '$' suffix. So if you find a good solution, let me know! Best regards Peter
|
|
|
Post by alexfish on Dec 9, 2015 22:33:07 GMT 1
Hi Peter
do not Know for sure . here hacked up a little bit to test address and the physical BR Alex
SUB test(long testbit)
PRINT testbit REM "can do something here maybe" ENDSUB
' test for string or number LOCAL *str1 = " TYP 20.30300 This is test" TYPE char LOCAL * str2 TYPE char test((long)&str1) test((long)str1) PRINT "-----------------------------" str1= "56"
test((long)&str1) test((long)str1)
PRINT "-----------------------------" str1= "567858585858858858585858585858858558858585555555555555555555555555555555555555555555555555555555555555"
test((long)&str1) test((long)str1)
PRINT "-----------------------------" str1= ""
test((long)&str1) test((long)str1)
REM now we know SIZE OF double and float that be easy
REM test the integer long will it be the same
LOCAL testint TYPE int PRINT "-----------------------------"
test((long)&testint) test((long)testint)
|
|
|
Post by alexfish on Dec 9, 2015 22:50:29 GMT 1
Hi Peter here a slight reverse . hope U compare & hope U be seeing the same as me BR Alex
SUB test(long testbit)
PRINT testbit REM "can do something here" ENDSUB LOCAL testint TYPE int PRINT "-----------------------------"
test((long)&testint) test((long)testint) ' test for string or number LOCAL *str1 = " TYP 20.30300 This is test" TYPE char LOCAL * str2 TYPE char test((long)&str1) test((long)str1) PRINT "-----------------------------" str1= "56"
test((long)&str1) test((long)str1)
PRINT "-----------------------------" str1= "567858585858858858585858585858858558858585555555555555555555555555555555555555555555555555555555555555"
test((long)&str1) test((long)str1)
PRINT "-----------------------------" str1= ""
test((long)&str1) test((long)str1)
ADDED :: think there be two left to sort out:: on these basic bits
|
|
|
Post by alexfish on Dec 9, 2015 23:23:55 GMT 1
Here have cleaned up the code
although there is a variation of the numbers , here on the Raspy there is a defined pattern to them
SUB test(long testbit)
PRINT testbit REM "can do something here"
ENDSUB
PRINT "---STRING -------------------"
LOCAL *str = " TYP 20.30300 This is test" TYPE char
test((long)&str) test((long)str)
LOCAL testint TYPE int LOCAL testlong TYPE long
PRINT "----INT---------------------"
test((long)&testint) test((long)testint)
PRINT "----LONG----------------------"
test((long)&testlong) test((long)testlong)
PRINT "---STRINGS -------------------"
LOCAL *str1 = " TYP 20.30300 This is test" TYPE char LOCAL * str2 TYPE char test((long)&str1) test((long)str1)
PRINT "-----------------------------"
str1= "56"
test((long)&str1) test((long)str1)
PRINT "-----------------------------" str1= "567858585858858858585858585858858558858585555555555555555555555555555555555555555555555555555555555555"
test((long)&str1) test((long)str1)
PRINT "-----------------------------" str1= ""
test((long)&str1) test((long)str1)
|
|
|
Post by alexfish on Dec 10, 2015 14:17:46 GMT 1
Hi Peter
can also test literal , but also mentioned can doubles and float
test((long)"Hello World")
test((long)9)
test((long) 1.9)
test((long) 1.0f)
PRINT SIZEOF(9) PRINT SIZEOF(10.9)
|
|
|
Post by Pjot on Dec 10, 2015 19:57:01 GMT 1
Hi Alex,
Not sure if this really means something - what is the variables actually contain a value?
E.g.:
LOCAL testint = 12345678 TYPE int LOCAL testlong = 34251678 TYPE long
PRINT "----INT---------------------"
test((long)&testint) test((long)testint)
Also, the SIZEOF is architecture dependent can can give similar results for different types. For example, a SIZEOF(char*) will give the same result as SIZEOF(void*).
BR Peter
|
|
|
Post by alexfish on Dec 10, 2015 20:17:37 GMT 1
Hi Peter
here is code run + the results of Raspberry PI2 of Note as regards the string the numbers IE 73320, 39504
look through the different values esp on first two digits . then as mention the second part of the string + the literal string value
although not tested :: are certain areas set aside for allocating the different TYPES , as shown by the number scheme , if one could call it then here on these bits , a string could be detected , and an integer is different to the long and as is the double to the float
like U mention things could be different on other systems , but sitting here I know what I be looking at , Yes further testing would be required. esp with other vars forgot to mention have done two string set at LOCAL *str1 = " TYP 20.30300 This is test" TYPE char , check the four numbers esp the second of each as in the short numbers ==. BR Alex
SUB test(long testbit)
PRINT testbit
REM "can do something here"
ENDSUB
PRINT "---STRING -------------------"
LOCAL *str = " TYP 20.30300 This is test" TYPE char
test((long)&str) test((long)str)
LOCAL testint TYPE int LOCAL testlong TYPE long
PRINT "----INT---------------------"
test((long)&testint) test((long)testint)
PRINT "----LONG----------------------"
test((long)&testlong) test((long)testlong)
PRINT "---STRINGS -------------------"
LOCAL *str1 = " TYP 20.30300 This is test" TYPE char LOCAL * str2 TYPE char test((long)&str1) test((long)str1)
PRINT "-----------------------------"
str1= "56"
test((long)&str1) test((long)str1)
PRINT "-----------------------------" str1= "567858585858858858585858585858858558858585555555555555555555555555555555555555555555555555555555555555"
test((long)&str1) test((long)str1)
PRINT "-----------------------------" str1= ""
test((long)&str1) test((long)str1) PRINT "-----------------------------" LOCAL dub TYPE double PRINT "-Double------------------------\n" test((long)&dub) test((long)dub) LOCAL flt TYPE double PRINT "-Float------------------------\n" test((long)&flt) test((long)flt) PRINT "-----------------------------" test((long)"Hello World") PRINT "-----------------------------" test((long)9) PRINT "-----------------------------" test((long) 1.9) PRINT "-----------------------------" test((long) 1.0f) PRINT "-----------------------------" PRINT SIZEOF(9) PRINT SIZEOF(10.9) PRINT "DONE"
/*
tests an Rasberry PI2 ---STRING ------------------- 73316 39504 ----INT--------------------- 88492 0 ----LONG---------------------- 75376 0 ---STRINGS ------------------- 73320 39504 ----------------------------- 73320 39760 ----------------------------- 73320 39764 ----------------------------- 73320 39528 ----------------------------- -Double------------------------
88904 0 -Float------------------------
75384 0 ----------------------------- 39936 ----------------------------- 9 ----------------------------- 1 ----------------------------- 1 ----------------------------- 4 8 DONE /*
|
|
|
Post by alexfish on Dec 10, 2015 22:09:42 GMT 1
Hi Peter here is an interesting one
SUB test(long testbit)
PRINT testbit
ENDSUB
A$ = "2500TTY" test((long)&A$)' Check this one out see bottom test((long)A$) : ' Check this one out see bottom
PRINT "*********************************" REM can check these with GCC like so
FOR t = 1 TO LEN(A$)-1 PRINT A$[t] FORMAT "%c\n" test((long)&A$[t]) test((long)A$[t]) PRINT "---------------------------" NEXT
LOCAL * A= "2500TTY" TYPE char test((long)&A) test((long)A) PRINT "---------------------------" LOCAL AB= "2500TTY" TYPE char * test((long)&AB) test((long)AB)
71728 8298592 ********************************* 5 8298593 53 --------------------------- 0 8298594 48 --------------------------- 0 8298595 48 --------------------------- T 8298596 84 --------------------------- T 8298597 84 --------------------------- Y 8298598 89 --------------------------- 71676 38164 --------------------------- 2127440180 38164
|
|
|
Post by alexfish on Dec 10, 2015 22:30:29 GMT 1
A separate test of the last AB string
LOCAL AB= "2500TTY" TYPE char * test((long)&AB) test((long)AB) PRINT "---------------------------" FOR t = 1 TO LEN(AB)-1 PRINT AB[t] FORMAT "%c\n" test((long)&AB[t]) test((long)AB[t]) PRINT "---------------------------" NEXT
2123519108 38564 --------------------------- 5 38565 53 --------------------------- 0 38566 48 --------------------------- 0 38567 48 --------------------------- T 38568 84 --------------------------- T 38569 84 --------------------------- Y 38570 89 ---------------------------
|
|