Revision: 1791
Author: wattymage
Date: Mon Mar 15 10:55:08 2010
Log: initial alpha sample
http://code.google.com/p/jallib/source/detail?r=1791

Added:
 /trunk/catpad/bcd_utils.jal

=======================================
--- /dev/null
+++ /trunk/catpad/bcd_utils.jal Mon Mar 15 10:55:08 2010
@@ -0,0 +1,152 @@
+--Title bcd_utils
+
+
+
+function StringToBCD4(byte in numText[], byte out bcd[4]) return bit is
+--returns packed BCD
+
+var byte txtIdx, bcdIdx
+var bit ok
+    bcd[0] = 0
+    bcd[1] = 0
+    bcd[2] = 0
+    bcd[3] = 0
+    txtIdx = 0
+    ok = true
+    if (count (numText) > 7) then
+        for 4 using bcdIdx loop
+            if (numText[txtIdx] >= "0") & (numText[txtIdx] <= "9") then
+               bcd[bcdIdx] = (numText[txtIdx] - "0") *16
+            else
+                ok = false
+            end if
+            txtIdx = txtIdx+1
+            if (numText[txtIdx] >= "0") & (numText[txtIdx] <= "9") then
+               bcd[bcdIdx] = bcd[bcdIdx] + (numText[txtIdx] - "0")
+            else
+                ok = false
+            end if
+            txtIdx = txtIdx+1
+        end loop
+    else
+        ok = false
+    end if
+    return ok
+end function
+
+function BCD4toString8( byte in bcd[4], byte out numText[8] ) return bit is
+var byte txtIdx, bcdIdx
+var bit ok, leadingZero
+    leadingZero = true
+    txtIdx = 0
+    ok = true
+    if (count (numText) > 7) then
+    for 4 using bcdIdx loop
+        numText[txtIdx] = (bcd[bcdIdx] >> 4)   + "0"
+        if leadingZero then
+            if numText[txtIdx] =="0" then
+                numText[txtIdx] =" "
+            else
+                leadingZero = false
+            end if
+        end if
+        txtIdx = txtIdx+1
+        numText[txtIdx] = (bcd[bcdIdx] & 15)   + "0"
+        if leadingZero then
+            if numText[txtIdx] =="0" then
+                numText[txtIdx] =" "
+            else
+                leadingZero = false
+            end if
+        end if
+        txtIdx = txtIdx+1
+    end loop
+    else
+        ok = false
+    end if
+    return ok
+End Function
+
+function BCD4toString10( byte in bcd[4], byte out numText[10] ) return bit is
+var byte txtIdx, bcdIdx
+var bit ok, leadingZero
+    leadingZero = true
+    txtIdx = 0
+    ok = true
+    if (count (numText) > 7) then
+    for 4 using bcdIdx loop
+        numText[txtIdx] = (bcd[bcdIdx] >> 4)   + "0"
+        if leadingZero then
+            if numText[txtIdx] =="0" then
+                numText[txtIdx] =" "
+            else
+                leadingZero = false
+            end if
+        end if
+        txtIdx = txtIdx+1
+        if (txtIdx ==3) & (Count(numText) > 9) then
+             numText[txtIdx] = "."
+             txtIdx = txtIdx+1
+        end if
+        numText[txtIdx] = (bcd[bcdIdx] & 15)   + "0"
+        if leadingZero then
+            if numText[txtIdx] =="0" then
+                numText[txtIdx] =" "
+            else
+                leadingZero = false
+            end if
+        end if
+        txtIdx = txtIdx+1
+        if (txtIdx ==2) & (Count(numText) > 9) then
+           leadingZero = false
+        elsif (txtIdx ==7) & (Count(numText) > 9) then
+             numText[txtIdx] = ","
+             txtIdx = txtIdx+1
+        end if
+    end loop
+    else
+        ok = false
+    end if
+    return ok
+End Function
+
+function BCD4toString11( byte in bcd[4], byte out numText[11] ) return bit is
+var byte txtIdx, bcdIdx
+var bit ok, leadingZero
+    leadingZero = true
+    txtIdx = 0
+    ok = true
+    for 4 using bcdIdx loop
+        numText[txtIdx] = (bcd[bcdIdx] >> 4)   + "0"
+        if leadingZero then
+            if numText[txtIdx] =="0" then
+                numText[txtIdx] =" "
+            else
+                leadingZero = false
+            end if
+        end if
+        txtIdx = txtIdx+1
+        if txtIdx ==3 then
+             numText[txtIdx] = "."
+             txtIdx = txtIdx+1
+        end if
+        numText[txtIdx] = (bcd[bcdIdx] & 15)   + "0"
+        if leadingZero then
+            if numText[txtIdx] =="0" then
+                numText[txtIdx] =" "
+            else
+                leadingZero = false
+            end if
+        end if
+        txtIdx = txtIdx+1
+        if txtIdx ==2 then
+           leadingZero = false
+        elsif txtIdx ==7 then
+             numText[txtIdx] = ","
+             txtIdx = txtIdx+1
+        end if
+    end loop
+    numText[txtIdx] = "0"
+    return ok
+End Function
+

--
You received this message because you are subscribed to the Google Groups 
"jallib" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/jallib?hl=en.

Reply via email to