SuperH-based fx calculators
fx legacy: LongToAscHex

0x467: int LongToAscHex(int value, char *dest, int digits);


The function reads value, and writes a null-terminated string with the hexadecimal representation of value to the buffer pointed to by dest. Only the number of nibbles specified will be written to this buffer, starting with the least significant digit. Signs are not handled.
LongToAscHex will zero-terminate the supplied buffer, even if digits = 0.
The function may not intend to return anything, though digits is left in r0 in OS 1.03.

 

Code

Example C-implementation of 'algorithm';

int LongToAscHex(int value, char *dest, int digits) {
        if (length == 0) { goto finish; }
       
        char *destend = dest+(digits-1);
        char nibble;
       
        int i;
        for (i = 0; i < length; i++) {
                nibble = (value & 0xF)    /* strip nibble */
                if (nibble < 10) { nibble += 0x30; }    /* is in range '0'-'9' */
                else { nibble += 0x37; }                /* is in range 'A'-'F' */
               
                *destend-- = nibble;        /* write the character */
                value = value >> 4;          /* advance to next nibble */
        }

finish:
        dest[digits] = 0;                            /* zero-terminate */
        return length;
}



Example use:

char stringbuffer[9];
int mynumber 0xDEADBEEF;
LongToAscHex(mynumber, stringbuffer, 8);
Print(stringbuffer);                /* should print "DEADBEEF" */



 

0x15F: int atoi(char*value);

The function converts the string *value into a 4-byte integer. Signs are handled. Leading spaces are ignored.

 

0x160: int LongToAsc(int value, unsigned char *dest, int digits);

The function reads value, and writes a null-terminated string with the ASCII representation of value to the buffer pointed to by dest. Only the number of digits specified will be written to this buffer, starting with the least significant digit. Signs are handled.
LongToAsc will zero-terminate the supplied buffer, even if digits = 0.
In case of an overflow, t. i. if digits is too small, a "<" will be inserted before the number and the most significant digits will be omitted.
 

 

(22.05.2012 06:24:06)