Re: Converting Little Endian Byte Representation To a String

2017-04-21 Thread Cannon Smith via 4D_Tech
Hi Arnaud,

Interesting idea. In all actuality, even though we call the tag a “number", it 
is always treated as a string. This algorithm I needed help with was the first 
time in 10 years I needed to think of it as a number, and that was only a 
temporary step for the way the algorithm worked.

Thanks for the idea, though!

--
Cannon.Smith
Synergy Farm Solutions Inc.
Hill Spring, AB Canada
403-626-3236




> On Apr 21, 2017, at 6:29 AM, Arnaud de Montard via 4D_Tech 
> <4d_tech@lists.4d.com> wrote:
> 
> just thinking about the opposite option… 
> You store the number as a string because that number is "too big" for 4D 
> numeric fields. Now, 15 digits string means 15*2 chars in an alpha/text field 
> = 30 bytes, and not a number as a result. While a uuid field is a number, 16 
> bytes, represented by 32 chars. I'm wondering if a uuid field instead 
> wouldn't be better.  

**
4D Internet Users Group (4D iNUG)
FAQ:  http://lists.4d.com/faqnug.html
Archive:  http://lists.4d.com/archives.html
Options: http://lists.4d.com/mailman/options/4d_tech
Unsub:  mailto:4d_tech-unsubscr...@lists.4d.com
**

Re: Converting Little Endian Byte Representation To a String

2017-04-21 Thread Arnaud de Montard via 4D_Tech

> Le 17 avr. 2017 à 23:36, Cannon Smith via 4D_Tech <4d_tech@lists.4d.com> a 
> écrit :
> 
> I have a little problem I’m hoping someone can help me solve. First, a bit of 
> background. Our application deals with RFID tags for animals. These are 
> always 15 digit numbers like 124000123456789. Even though they are numbers, I 
> never have to deal with them as numbers. Instead, I always treat them as 
> strings. RFID readers are usually RS-232 devices that send the tag number 
> over as a string, so that works out well.[...]

Hi Cannon, 
just thinking about the opposite option… 
You store the number as a string because that number is "too big" for 4D 
numeric fields. Now, 15 digits string means 15*2 chars in an alpha/text field = 
30 bytes, and not a number as a result. While a uuid field is a number, 16 
bytes, represented by 32 chars. I'm wondering if a uuid field instead wouldn't 
be better.  

-- 
Arnaud de Montard 




**
4D Internet Users Group (4D iNUG)
FAQ:  http://lists.4d.com/faqnug.html
Archive:  http://lists.4d.com/archives.html
Options: http://lists.4d.com/mailman/options/4d_tech
Unsub:  mailto:4d_tech-unsubscr...@lists.4d.com
**

Re: Converting Little Endian Byte Representation To a String

2017-04-18 Thread Cannon Smith via 4D_Tech
Thanks again, Miyako! This is even more useful!

--
Cannon.Smith
Synergy Farm Solutions Inc.
Hill Spring, AB Canada
403-626-3236




> On Apr 18, 2017, at 3:39 PM, Keisuke Miyako via 4D_Tech 
> <4d_tech@lists.4d.com> wrote:
> 
> I think there was a bug in the carry-over logic,
> I've uploaded a cleaner version here:
> 
> https://github.com/miyako/4d-tips-text-integer-maths
> 
> 

**
4D Internet Users Group (4D iNUG)
FAQ:  http://lists.4d.com/faqnug.html
Archive:  http://lists.4d.com/archives.html
Options: http://lists.4d.com/mailman/options/4d_tech
Unsub:  mailto:4d_tech-unsubscr...@lists.4d.com
**

Re: Converting Little Endian Byte Representation To a String

2017-04-18 Thread Keisuke Miyako via 4D_Tech
Hello,

I think there was a bug in the carry-over logic,
I've uploaded a cleaner version here:

https://github.com/miyako/4d-tips-text-integer-maths

> 2017/04/19 2:03、Cannon Smith via 4D_Tech <4d_tech@lists.4d.com> のメール:
> Thanks, Miyako! That does indeed work and is plenty fast as well. I really 
> appreciate the code!




**
4D Internet Users Group (4D iNUG)
FAQ:  http://lists.4d.com/faqnug.html
Archive:  http://lists.4d.com/archives.html
Options: http://lists.4d.com/mailman/options/4d_tech
Unsub:  mailto:4d_tech-unsubscr...@lists.4d.com
**

Re: Converting Little Endian Byte Representation To a String

2017-04-18 Thread Cannon Smith via 4D_Tech
Thanks, Miyako! That does indeed work and is plenty fast as well. I really 
appreciate the code!

--
Cannon.Smith
Synergy Farm Solutions Inc.
Hill Spring, AB Canada
403-626-3236




> On Apr 18, 2017, at 12:43 AM, Keisuke Miyako via 4D_Tech 
> <4d_tech@lists.4d.com> wrote:
> 
> I think this will do it:
> 
> ASSERT("840003123456770"=Get_value ("02ABC877FAFB0200"))
> 
> using primary school maths only...
> 

**
4D Internet Users Group (4D iNUG)
FAQ:  http://lists.4d.com/faqnug.html
Archive:  http://lists.4d.com/archives.html
Options: http://lists.4d.com/mailman/options/4d_tech
Unsub:  mailto:4d_tech-unsubscr...@lists.4d.com
**

Re: Converting Little Endian Byte Representation To a String

2017-04-18 Thread Bruno LEGAY via 4D_Tech
Hi Cannon,

Here is a method using LPE on Mac OS X :

C_TEXT($vt_64bitLittleEndianHex;$vt_64bitBigEndianHex)
$vt_64bitLittleEndianHex:="02ABC877FAFB0200" // => "840003123456770"

  //convert the little endian hex to big endian hex
C_LONGINT($i)
For ($i;1;Length($vt_64bitLittleEndianHex);2)
$vt_64bitBigEndianHex:=Substring($vt_64bitLittleEndianHex;$i;2)+$vt_64bitBigEndianHex
End for

  // we want to execute "echo $((0x0002FBFA77C8AB02))" in a shell script on os x
C_TEXT($vt_cmd;$vt_in;$vt_out;$vt_err)
$vt_cmd:="/bin/sh -s"  // run a shell and run script from standard input
$vt_in:="/bin/echo -n $((0x"+$vt_64bitBigEndianHex+"))" // -n to specify "no LF 
at the end of the output string"
LAUNCH EXTERNAL PROCESS($vt_cmd;$vt_in;$vt_out;$vt_err)

C_TEXT($vt_64bitString)
$vt_64bitString:=$vt_out  // "840003123456770"

On windows there is a function toint64 in powershell.

HTH
**
4D Internet Users Group (4D iNUG)
FAQ:  http://lists.4d.com/faqnug.html
Archive:  http://lists.4d.com/archives.html
Options: http://lists.4d.com/mailman/options/4d_tech
Unsub:  mailto:4d_tech-unsubscr...@lists.4d.com
**

Re: Converting Little Endian Byte Representation To a String

2017-04-18 Thread Keisuke Miyako via 4D_Tech
I think this will do it:

ASSERT("840003123456770"=Get_value ("02ABC877FAFB0200"))

using primary school maths only...

--

C_TEXT($1;$0)

Case of
: (Count parameters=1)

C_TEXT(hex;$e;$total)
C_LONGINT($i)

$hex:=$1

For ($i;1;Length($hex);2)

  //hex byte code to integer string
$byte:=Substring($hex;$i;2)
$code:=""
PROCESS 4D TAGS($code;$code)

If ($i=1)
$value:=$code
$e:="1"
Else
  //exp beyond 256^5 is a bit hazy, might as well do it using text
EXECUTE METHOD(Current method name;$e;$e;"256";"*")
  //base-10 text multiplication
EXECUTE METHOD(Current method name;$value;$code;$e;"*")
End if
  //base-10 text addition
EXECUTE METHOD(Current method name;$total;$total;$value;"+")
End for

$0:=$total

: (Count parameters=3)

C_TEXT($a;$b;$r)

$a:=String($1)
$b:=String($2)
$operand:=$3

If ($a="")
$a:="0"
End if

If ($b="")
$b:="0"
End if

  //no checking for non-numbers, negative numbers, e

C_LONGINT($lena;$lenb)
$lena:=Length($a)
$lenb:=Length($b)

C_LONGINT($c;$d;$e;$f;$m1;$m2;$m3)
ARRAY LONGINT($mm;0)

Case of
: ($operand="*")

$d:=0
For ($x;$lena;1;-1)
$c:=1
For ($y;$lenb;1;-1)
$m:=Num($a[[$x]])*Num($b[[$y]])
$e:=$m%10
$f:=$m\10
If (($c+$d+2)>Size of array($mm))
ARRAY LONGINT($mm;$c+$d+2)
End if
$m1:=$mm{$c+$d}+$e
$m2:=$mm{$c+$d+1}+$f+($m1\10)
$m3:=$mm{$c+$d+2}+($m2\10)
$mm{$c+$d}:=$m1%10
$mm{$c+$d+1}:=$m2%10
$mm{$c+$d+2}:=$m3
$c:=$c+1
End for
$d:=$d+1
End for

: ($operand="+")

$len:=Choose($lena>$lenb;$lena;$lenb)

$a:=Substring(("0"*$len)+$a;$lena+1)
$b:=Substring(("0"*$len)+$b;$lenb+1)
$c:=1

For ($x;$len;1;-1)
$m:=Num($a[[$x]])+Num($b[[$x]])
$e:=$m%10
$f:=$m\10
If (($c+2)>Size of array($mm))
ARRAY LONGINT($mm;$c+2)
End if
$m1:=$mm{$c}+$e
$m2:=$mm{$c+1}+$f+($m1\10)
$m3:=$mm{$c+2}+($m2\10)
$mm{$c}:=$m1%10
$mm{$c+1}:=$m2%10
$mm{$c+2}:=$m3
$c:=$c+1
End for

End case

  //filter preceeding zeros and stringify
$r:="0"
C_LONGINT($i;$len)
$len:=Size of array($mm)
For ($i;$len;1;-1)
If ($r="0")
$r:=""
End if
$r:=$r+String($mm{$i})
End for

$0:=$r

End case




**
4D Internet Users Group (4D iNUG)
FAQ:  http://lists.4d.com/faqnug.html
Archive:  http://lists.4d.com/archives.html
Options: http://lists.4d.com/mailman/options/4d_tech
Unsub:  mailto:4d_tech-unsubscr...@lists.4d.com
**

Re: Converting Little Endian Byte Representation To a String

2017-04-17 Thread Cannon Smith via 4D_Tech
Thanks, Tim! I’ll try that library out tomorrow and see how it goes.

Much appreciated.

--
Cannon.Smith
Synergy Farm Solutions Inc.
Hill Spring, AB Canada
403-626-3236




> On Apr 17, 2017, at 5:03 PM, Tim Nevels via 4D_Tech <4d_tech@lists.4d.com> 
> wrote:
> 
> There is a code library on 4D Forums called Math4D that might do the trick 
> for you. I think the method you are looking for is MATH_GrandsEntiers.

**
4D Internet Users Group (4D iNUG)
FAQ:  http://lists.4d.com/faqnug.html
Archive:  http://lists.4d.com/archives.html
Options: http://lists.4d.com/mailman/options/4d_tech
Unsub:  mailto:4d_tech-unsubscr...@lists.4d.com
**

Re: Converting Little Endian Byte Representation To a String

2017-04-17 Thread Tim Nevels via 4D_Tech
On Apr 17, 2017, at 5:19 PM,Cannon Smith wrote:

> Here is an example string coming across the wire: “02ABC877FAFB0200”. This 
> represents 8 bytes in little endian ordering. The algorithm that was given me 
> was to break the string into bytes (“02”, “AB”, “C8”, etc.) and then loop 
> through each byte, converting it to decimal and applying a little math. For 
> example:
> 
> Hex “02” = Decimal 2. SubValue = 2 * (256^0)  = 2
> Hex “AB" = Decimal 171.Subvalue = 171 * (256^1) =  43776
> Hex “C8" = Decimal 200.Subvalue = 200 * (256^2) =  13107200
> Hex “77" = Decimal 119. Subvalue = 119 * (256^3) =1996488704
> Hex “FA" = Decimal 250.Subvalue = 250 * (256^4) =  1073741824000
> Hex “FB" = Decimal 251.Subvalue = 251 * (256^5) =  275977418571776 (4D 
> gives 2.759774185718e+14)
> Hex “02" = Decimal 2.Subvalue = 2  * (256^6) = 562949953421312 
> (4D gives 5.629499534213e+14)
> Hex “00" = Decimal 0.Subvalue = 0  * (256^7) = 0
> 
> Once this is done, all the subvalue results are summed which should give the 
> RFID number. In this case it should be 840003123456770. But 4D gives this: 
> 8.400031234568e+14. As you can see, the last three digits are not correct.
> 
> So, does anyone know a way to convert the string “02ABC877FAFB0200” to the 
> string “840003123456770” within 4D’s limitations?

So what you need is a way to do math of very big integers. You only need to do 
basic math functions of add and multiple. Convert could the exponential to a 
series of multiplies. 

There is a code library on 4D Forums called Math4D that might do the trick for 
you. I think the method you are looking for is MATH_GrandsEntiers.

http://forums.4d.fr/Post/EN/4010516/1/4021906#4021906

If if that doesn’t work out, I’d look at using a Javascript implementation. 
Like:  https://www.npmjs.com/package/big-integer

I did notice this on the above web page:

Note that Javascript numbers larger than 9,007,199,254,740,992 and smaller than 
-9007199254740992 are not precisely represented numbers and will not produce 
exact results. If you are dealing with numbers outside that range, it is better 
to pass in strings.

Maybe you could use native Javascript. It has 15 digit integer precision. Maybe 
an offscreen web area and WA EXECUTE JAVASCRIPT FUNCTION with a custom function 
could be written to return the result.

That’s all the ideas I could come up with in 10 minutes.

Tim


Tim Nevels
Innovative Solutions
785-749-3444
timnev...@mac.com


**
4D Internet Users Group (4D iNUG)
FAQ:  http://lists.4d.com/faqnug.html
Archive:  http://lists.4d.com/archives.html
Options: http://lists.4d.com/mailman/options/4d_tech
Unsub:  mailto:4d_tech-unsubscr...@lists.4d.com
**

Re: Converting Little Endian Byte Representation To a String

2017-04-17 Thread Cannon Smith via 4D_Tech
Hi Julio,

Not a stupid question at all! The hex string representation is simply the way 
this reader has decided to encode and transfer the RFID number. The actual RFID 
number has to be interoperable with other systems that expect it in the 
“regular” format. Also, users will sometimes hand enter the number which is 
printed on the tag and that would be in the “regular” format as well and would 
need to match what is in the database.

The real question is why this particular RFID reader has decided to encode the 
number in this way when every single reader I’ve dealt with in the past decade 
all do it the same “regular” way. Sigh.

--
Cannon.Smith
Synergy Farm Solutions Inc.
Hill Spring, AB Canada
403-626-3236




> On Apr 17, 2017, at 3:59 PM, Julio Carneiro via 4D_Tech 
> <4d_tech@lists.4d.com> wrote:
> 
> Forgive my stupid question: why do you need to convert the hex string to 
> number? Can’t you simply use it as your RFID? (of course you’d have to 
> increase your key field to 16 chars). You’re already handling it as a string, 
> why change?

**
4D Internet Users Group (4D iNUG)
FAQ:  http://lists.4d.com/faqnug.html
Archive:  http://lists.4d.com/archives.html
Options: http://lists.4d.com/mailman/options/4d_tech
Unsub:  mailto:4d_tech-unsubscr...@lists.4d.com
**

Re: Converting Little Endian Byte Representation To a String

2017-04-17 Thread Julio Carneiro via 4D_Tech
Cannon,

Forgive my stupid question: why do you need to convert the hex string to 
number? Can’t you simply use it as your RFID? (of course you’d have to increase 
your key field to 16 chars). You’re already handling it as a string, why change?

> On Apr 17, 2017, at 6:36 PM, Cannon Smith via 4D_Tech <4d_tech@lists.4d.com> 
> wrote:
> 
> I have a little problem I’m hoping someone can help me solve. First, a bit of 
> background. Our application deals with RFID tags for animals. These are 
> always 15 digit numbers like 124000123456789. Even though they are numbers, I 
> never have to deal with them as numbers. Instead, I always treat them as 
> strings. RFID readers are usually RS-232 devices that send the tag number 
> over as a string, so that works out well.
> 
> Now I’m being asked to support a new kind of reader that works differently. I 
> have the algorithm to convert from the reader byte structure to a 15 digit 
> number. Unfortunately, 4D’s C_REAL variables are only precise to 13 digits, 
> so my last 2-3 digits are incorrect. I’m hoping someone knows how to convert 
> what I’m getting into a 15 digit string in some other way.
> 
> Here is an example string coming across the wire: “02ABC877FAFB0200”. This 
> represents 8 bytes in little endian ordering. The algorithm that was given me 
> was to break the string into bytes (“02”, “AB”, “C8”, etc.) and then loop 
> through each byte, converting it to decimal and applying a little math. For 
> example:
> 
> Hex “02” = Decimal 2. SubValue = 2 * (256^0)  = 2
> Hex “AB" = Decimal 171.Subvalue = 171 * (256^1) =  43776
> Hex “C8" = Decimal 200.Subvalue = 200 * (256^2) =  13107200
> Hex “77" = Decimal 119. Subvalue = 119 * (256^3) =1996488704
> Hex “FA" = Decimal 250.Subvalue = 250 * (256^4) =  1073741824000
> Hex “FB" = Decimal 251.Subvalue = 251 * (256^5) =  275977418571776 (4D 
> gives 2.759774185718e+14)
> Hex “02" = Decimal 2.Subvalue = 2  * (256^6) = 562949953421312 
> (4D gives 5.629499534213e+14)
> Hex “00" = Decimal 0.Subvalue = 0  * (256^7) = 0
> 
> Once this is done, all the subvalue results are summed which should give the 
> RFID number. In this case it should be 840003123456770. But 4D gives this: 
> 8.400031234568e+14. As you can see, the last three digits are not correct.
> 
> So, does anyone know a way to convert the string “02ABC877FAFB0200” to the 
> string “840003123456770” within 4D’s limitations?
> 
> Thanks.
> 
> --
> Cannon.Smith
> Synergy Farm Solutions Inc.
> Hill Spring, AB Canada
> 403-626-3236
> 
> 
> 
> 
> **
> 4D Internet Users Group (4D iNUG)
> FAQ:  http://lists.4d.com/faqnug.html
> Archive:  http://lists.4d.com/archives.html
> Options: http://lists.4d.com/mailman/options/4d_tech
> Unsub:  mailto:4d_tech-unsubscr...@lists.4d.com
> **

--
Julio Carneiro
jjfo...@gmail.com

**
4D Internet Users Group (4D iNUG)
FAQ:  http://lists.4d.com/faqnug.html
Archive:  http://lists.4d.com/archives.html
Options: http://lists.4d.com/mailman/options/4d_tech
Unsub:  mailto:4d_tech-unsubscr...@lists.4d.com
**

Converting Little Endian Byte Representation To a String

2017-04-17 Thread Cannon Smith via 4D_Tech
I have a little problem I’m hoping someone can help me solve. First, a bit of 
background. Our application deals with RFID tags for animals. These are always 
15 digit numbers like 124000123456789. Even though they are numbers, I never 
have to deal with them as numbers. Instead, I always treat them as strings. 
RFID readers are usually RS-232 devices that send the tag number over as a 
string, so that works out well.

Now I’m being asked to support a new kind of reader that works differently. I 
have the algorithm to convert from the reader byte structure to a 15 digit 
number. Unfortunately, 4D’s C_REAL variables are only precise to 13 digits, so 
my last 2-3 digits are incorrect. I’m hoping someone knows how to convert what 
I’m getting into a 15 digit string in some other way.

Here is an example string coming across the wire: “02ABC877FAFB0200”. This 
represents 8 bytes in little endian ordering. The algorithm that was given me 
was to break the string into bytes (“02”, “AB”, “C8”, etc.) and then loop 
through each byte, converting it to decimal and applying a little math. For 
example:

Hex “02” = Decimal 2. SubValue = 2 * (256^0)  = 2
Hex “AB" = Decimal 171.Subvalue = 171 * (256^1) =  43776
Hex “C8" = Decimal 200.Subvalue = 200 * (256^2) =  13107200
Hex “77" = Decimal 119. Subvalue = 119 * (256^3) =1996488704
Hex “FA" = Decimal 250.Subvalue = 250 * (256^4) =  1073741824000
Hex “FB" = Decimal 251.Subvalue = 251 * (256^5) =  275977418571776 (4D 
gives 2.759774185718e+14)
Hex “02" = Decimal 2.Subvalue = 2  * (256^6) = 562949953421312 (4D 
gives 5.629499534213e+14)
Hex “00" = Decimal 0.Subvalue = 0  * (256^7) = 0

Once this is done, all the subvalue results are summed which should give the 
RFID number. In this case it should be 840003123456770. But 4D gives this: 
8.400031234568e+14. As you can see, the last three digits are not correct.

So, does anyone know a way to convert the string “02ABC877FAFB0200” to the 
string “840003123456770” within 4D’s limitations?

Thanks.

--
Cannon.Smith
Synergy Farm Solutions Inc.
Hill Spring, AB Canada
403-626-3236




**
4D Internet Users Group (4D iNUG)
FAQ:  http://lists.4d.com/faqnug.html
Archive:  http://lists.4d.com/archives.html
Options: http://lists.4d.com/mailman/options/4d_tech
Unsub:  mailto:4d_tech-unsubscr...@lists.4d.com
**