Searching \ for 'Convert byte to ascii' in subject line. ()
Make payments with PayPal - it's fast, free and secure! Help us get a faster server
FAQ page: techref.massmind.org/techref/index.htm?key=convert+byte+ascii
Search entire site for: 'Convert byte to ascii'.

Truncated match.
PICList Thread
'Convert byte to ascii'
1998\10\08@072859 by Justin Grimm

flavicon
face
Hi piclisters
I need a short routine that will convert a single byte
to 3 ascii bytes

For example; if the number 127 (decimal) is in file x,
how can I convert it to 3 registers (hundreds,tens and units).
This is what the 3 files should have in them after the conversion
 register1 = 31,   register2 = 32,   register3 = 37  (all in ascii
notation)

Thankyou


Justin Grimm
Eclipse Energy Systems
spam_OUTeclipseTakeThisOuTspamaccessin.com.au

1998\10\08@090540 by Gordon Couger

flavicon
face
-----Original Message-----
From: Justin Grimm <.....eclipseKILLspamspam@spam@ACCESSIN.COM.AU>


>Hi piclisters
>I need a short routine that will convert a single byte
>to 3 ascii bytes
>
>For example; if the number 127 (decimal) is in file x,
>how can I convert it to 3 registers (hundreds,tens and units).
>This is what the 3 files should have in them after the conversion
>  register1 = 31,   register2 = 32,   register3 = 37  (all in ascii
>notation)

a = 127
b = a mod 10 +48
a = a /10
c = a mod 10 + 48
a = a /10
d = a+48
Is the way it is done. The bad part is the number is in reverse order. The
way I do it is to start at the right hand side of the string and return a
pointer
to the first character.

Here it is in C
char *i2a(int n, int base, char delim, int format)
  {
  char *s;
  char *cp;
  int count;
  s = scratch;
   count =0;
  cp = s + 7; /* start at end of string */
  *cp-- = 0;          /* terminate string */
  *cp-- = delim;
  while(n)
     {
     *cp-- = hex_str[n % base];
     n =n / base;
     count++;
     }
   while(count< format)
     {
     *cp-- = '0';
     count++;
     }
   s=cp+1;
   return s;
   }

good luck
Gordon

Gordon Couger gcougerspamKILLspamcouger.com
Stillwater, OK        405 624-2855   GMT -6:00

1998\10\08@160429 by Scott Dattalo

face
flavicon
face
Gordon Couger wrote:
{Quote hidden}

I was going to point you to the Archive, but for some reason it stops at
April '98. Here's something that I posted last May. It only converts the
numbers to BCD, but going to ascii is trivial. (Does any one have this
stuff referenced on a web page)?


--------------------------------------------------------------

Here's a 24-instruction solution with a little looping:
Explanation:
anick.simplenet.com/piclist/Oct97/0369.html
Code:
http://anick.simplenet.com/piclist/Oct97/0312.html

It's based on what Payson affectionately calls his 'wonderful 16-bit
binary to BCD algorithm':

www.iversoft.com/cgi-bin/lwgate/PICLIST/archives/March97/date/article-0.h
tml

(This link is from the archive and takes a while to download.
It's been a year since it's been posted, perhaps it's time
again?)



Here's the 28-cycle (non-looping) version that I promised yesterday.
It's based on binary comparisons. It's one of those routines that
is very difficult to comment. So I didn't. However it takes advantage
of this little trick to quickly ascertain the ones' digit:

If you look at the ones' digit for 2^N you see this pattern:
      n = 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11 ...
2^n % 10 = 1, 2, 4, 8, 6, 2, 4, 8, 6, 2, 4, 8 ...

If it wasn't for the annoying 6's, you could simply sum the nibbles to
get and get the ones' digit (after a relatively simple binary to BCD
conversion). However, it's simple enough to test if bit 4, 8,...
are high and to subtract 1 and then add 6 (or simply add 5) if they
are.

The second observation is that the sum of all of the tens' digits is
less than 16, and thus can fit into one nibble. This simplifies having
to deal with overflow until after all of the digits have been added
together.

The third observation is that the BCD result is greater than 200 only
if the most significant bit is set.


;********************************
;binary_to_bcd - 8-bits
;
;Input
;  bin  - 8-bit binary number
;Outputs
; hundreds - the hundreds digit of the BCD conversion
; tens_and_ones - the tens and ones digits of the BCD conversion

binary_to_bcd:

       CLRF    hundreds
       SWAPF   bin,W
       ADDWF   bin,W
       ANDLW   00001111b
       SKPNDC
        ADDLW  0x16
       SKPNDC
        ADDLW  0x06
       ADDLW   0x06
       SKPDC
        ADDLW  -0x06

       BTFSC   bin,4
        ADDLW   0x16 - 1 + 0x6
       SKPDC
        ADDLW  -0x06

       BTFSC   bin,5
        ADDLW  0x30

       BTFSC   bin,6
        ADDLW  0x60

       BTFSC   bin,7
        ADDLW  0x20

       ADDLW   0x60
       RLF     hundreds,F
       BTFSS   hundreds,0
        ADDLW  -0x60

       MOVWF   tens_and_ones
       BTFSC   bin,7
        INCF   hundreds,F

Scott

1998\10\08@162204 by Joao Batista

flavicon
face
this a little fragmetn of code to ASCII convert

movfw   ind             ; get value from register
 xorlw   0x30            ; format to ASCII
 movwf   _char          ; put to char_register
 call    lcd_char        ; send to display

and here a routine for conversion of value in L_byte to 5 digits in R0( MSD ), R
1, R2
(LSD )

bin_bcd  bcf     status,0
         movlw   .16
         movwf   count
         clrf    R0
         clrf    R1
         clrf    R2
loop16   rlf     L_byte
         rlf     H_byte
         rlf     R2
         rlf     R1
         rlf     R0
         decfsz  count
         goto    adjDEC
         retlw   0
adjDEC   clrwdt
         movlw   R2
         movwf   fsr
         call    adjBCD
         movlw   R1
         movwf   fsr
         call    adjBCD
         movlw   R0
         movwf   fsr
         call    adjBCD
         goto    loop16
adjBCD   movlw   3
         addwf   indf,w
         movwf   temp
         btfsc   temp,3
         movwf   indf
         movlw   30
         addwf   indf,w
         movwf   temp
         btfsc   temp,7
         movwf   0
         retlw   0

good luke

Scott Dattalo wrote:

> Gordon Couger wrote:
> >
> > {Original Message removed}

More... (looser matching)
- Last day of these posts
- In 1998 , 1999 only
- Today
- New search...