View previous topic :: View next topic |
Author |
Message |
TSEYFARTH
Joined: 01 Jul 2006 Posts: 1054
|
Posted: Tue Feb 11, 2014 1:58 am Post subject: Printbin problem switching from 4 byte integer to 8byte inte |
|
|
Hello all,
Can anyone help me fix this problem? I am trying to send 4 bytes of an 8 byte integer using Printbin. I can get this code to work, when using a LONG, but when I try to switch to a Double and send the lower 4 bytes, it does not work.
4 Byte code:
Code: |
Dim Gatecode As Long
Dim Sndpin(4) As Byte At Gatecode Overlay
Printbin #1 , Sndpin(4) ; 1
Printbin #1 , Sndpin(3) ; 1
Printbin #1 , Sndpin(2) ; 1
Printbin #1 , Sndpin(1) ; 1 '<------ WORKS 4 byte integer
|
8 byte code:
Code: |
Dim Gatecode As Double
Dim Sndpin(8) As Byte At Gatecode Overlay
Printbin #1 , Sndpin(4) ; 1
Printbin #1 , Sndpin(3) ; 1
Printbin #1 , Sndpin(2) ; 1
Printbin #1 , Sndpin(1) ; 1 '<------ Does not work
|
Can anyone help me to solve this problem?
Thank you,
Tim
(BASCOM-AVR version : 2.0.7.7 ) |
|
Back to top |
|
|
MWS
Joined: 22 Aug 2009 Posts: 2262
|
Posted: Tue Feb 11, 2014 7:28 am Post subject: |
|
|
Sending the lower bytes should work in both cases, for printbin the overlaid variable is just a piece of memory. Internal representation of any assigned number to a Double variable however is much different to a Long. A simple test is to assign a number to sndpin(1), don't assign anything to the Double and check what you get out from printbin then. |
|
Back to top |
|
|
TSEYFARTH
Joined: 01 Jul 2006 Posts: 1054
|
Posted: Tue Feb 11, 2014 6:54 pm Post subject: |
|
|
Thank you for your reply MWS.
I tested as you suggested, setting Sndpin(1) = 254. The result was correct.
This might be worth noting too, I use the Val function to set the Double variable.
So, can you suggest a way to fix this?
Thank you,
Tim |
|
Back to top |
|
|
MWS
Joined: 22 Aug 2009 Posts: 2262
|
Posted: Tue Feb 11, 2014 7:41 pm Post subject: |
|
|
TSEYFARTH wrote: | I tested as you suggested, setting Sndpin(1) = 254. The result was correct. |
I've expected that.
Quote: | This might be worth noting too, I use the Val function to set the Double variable. |
You want to convert a longer String into single bytes?
I can't suggest based on that little information, show a short sample or explain some more.
For representation of singles/doubles, look up Language Fundamentals -> Floating Point |
|
Back to top |
|
|
TSEYFARTH
Joined: 01 Jul 2006 Posts: 1054
|
Posted: Tue Feb 11, 2014 8:04 pm Post subject: |
|
|
What is do now, is collect a string of touchpad inputs (0 to 9). Then using the val command to set as an integer
Code: |
Pin = Pin + Str(key)
Gatecode = Val(pin)
|
The only reason to use a Double, is for its 64 bit capacity. Is there a way to use two Longs instead?
Thanks again!
Tim |
|
Back to top |
|
|
AdrianJ
Joined: 16 Jan 2006 Posts: 2483 Location: Queensland
|
Posted: Tue Feb 11, 2014 10:56 pm Post subject: |
|
|
Tim,
I think you a missing something very fundamental in your understanding of how number systems and representations work, that you even ask this sort of question.
You are confusing the data you get from the keypad ( a list of binary codes, like a byte array ) with the 'value' you might assign to that array. How you assign a value depends dramatically on how you agree to assign a meaning to each byte in the array. For us people who work little-endian, we assume a a byte array 'means' an integer with the low significant bytes first. People who work big-endian assume high byte first, people who think in floats assume the IEEE ( or other ) representations for Single and Dounble precision. Others who think in strings use binary to ASCII codes and convert to alphanumerics, or binary to UNICODE and do the same ( but with a different, and expanded, set of codes ).
But all these are merely conventions as a way of storing and handling numerics and characters. The primary data is in the byte array itself, and if you choose to represent that as a string of digits 0-9, then you are using yet another convention to convert to a human readable form.
Its totally irrelevant what form you use, as long as you and your customers all use the same convention.
Since Basic has no 64 bit ( 8 byte ) number representation, you cannot ( easily ) use that, but why would it matter ? Just use the bytes as an array, and represent it as a string of digits, as you are now. At least that way you are not tied to a specific number length as a construction.
I thought I pointed this out before, that if you limit yourself to a specific length, just because it fits a certain language specific type, tthen as soon as someone ( inevitably ) wants something different, you break an entire design.
You should remember always that we humans represent a 'number' as a string of characters, 1234 for instance, in English, using Arabic based numerals. Other languages, including machines, have many different representations, but the 'number' 1234, meaning a certain size count of objects remains exactly the same.
Its no wonder a couple of guys, Russell and Whitehead, in attempting to write a complete logical definition of mathematics back in the early 1900s or so, started a book which ran to 15 volumes. They got around to defining a number at about volume 7. Worse still, just after it went to publication, someone ( Godel ) wrote a paper showing that it was inherently impossible to define a useful mathematics in which every statement could be decided as true or false. _________________ Adrian Jansen
Computer language is a framework for creativity |
|
Back to top |
|
|
TSEYFARTH
Joined: 01 Jul 2006 Posts: 1054
|
Posted: Tue Feb 11, 2014 11:27 pm Post subject: |
|
|
Hi Adrian,
I just finished re-reading your post made a couple of weeks ago when I first brought up "How to create an 8 byte integer". Your thoughts/comments have become clearer now that I have tried to implement this and have seen (some) of the issues it presents!
The problem I have (had) with sending a string of bytes is that they are being decoded by existing software that is already in the field. It is decoded as 4 bytes then set/saved/processed as an Long Integer:
Code: |
Remote_Response_Data = (Add1 * 256 * 256 * 256) + (Add2 * 256 * 256) + (Add3 * 256) + Add4
|
I wanted to continue to use that same process, but it seems that is not going to work. Further consideration of your suggestion and deeper thought says that your argument is the best solution. In this case as well, we *can* control both ends. So even with the (windows) software in the field, we can either force before use or set this as a new feature with an enable switch - we I have as well....
More consideration reveals that by utilizing your suggestion, we could also make the code an alpha, or alpha-numeric code.
Again, thank you all for your suggestions and pointing out what should have been obvious....
Tim |
|
Back to top |
|
|
AdrianJ
Joined: 16 Jan 2006 Posts: 2483 Location: Queensland
|
Posted: Wed Feb 12, 2014 12:09 am Post subject: |
|
|
Windows handles strings perfectly well, why anyone would choose to convert to number for this is beyond me. Its not as though you are ever going to do
N1 = N2 + N3
where N2 and N3 are a couple of PIN numbers !
I have seen the same wierdiosities in databases where people thought it was a good idea to store phone numbers as numeric fields. Its bad enough when people wonder why they get strange things when they enter a number like 07 4564 5620. What will happen to that leading zero, and the spaces ? What happens when the operating system converts the number to floating point and back, and represents the number as 7.456456199999999999 e+8. Do they think that might cause a problem ? _________________ Adrian Jansen
Computer language is a framework for creativity |
|
Back to top |
|
|
Visovian
Joined: 31 Oct 2007 Posts: 584 Location: Czech
|
Posted: Wed Feb 12, 2014 3:19 am Post subject: |
|
|
Quote: | What is do now, is collect a string of touchpad inputs (0 to 9). Then using the val command to set as an integer
...
Is there a way to use two Longs instead? |
Yes, you can do
Code: | Dim Ar As String * 8
Dim Ar1 As String * 4
Dim Ar2 As String * 4
Dim Long1 As Long
Dim Long2 As Long
Ar = "12345678"
Do
Ar1 = Mid(ar , 1 , 4 )
Long1 = Val(ar1) '1234
Ar2 = Mid(ar , 5 , 4 )
Long2 = Val(ar2) '5678
Loop
End |
But is it not less complicated to transfer the string Ar[]
and then compare it with the password string ?
Code: | if Ar = "11223344" then open gate |
|
|
Back to top |
|
|
TSEYFARTH
Joined: 01 Jul 2006 Posts: 1054
|
Posted: Wed Feb 12, 2014 5:13 am Post subject: |
|
|
Part of the reason for the conversion Adrian, is that there were legacy (DOS) components to deal with. But as you and Visovian have pointed out so well, it is time to change.
So, thank you all again for your help! It is official. We move forward with a modified protocol and incorporate code to manage that. It will prove more usable, adjustable and just plain better as time marches on.
Tim |
|
Back to top |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum You cannot attach files in this forum You cannot download files in this forum
|
|