⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 strutl.c

📁 一个瑞萨单片机的程序。。。供大家学习用。。。。。。。。。。。。。。。。。。
💻 C
字号:
/*""FILE COMMENT""*************************************************************
*  System Name : for eduction (NO TRANSFERRING)
*  File Name   : strutl.c
*  Contents    : deal with string
*  Model       : for OAKS8-LCD Board
*  CPU         : R8C/Tiny series
*  Compiler    : NC30WA(V.5.30 Release 1)
*  OS          : not be used
*  Programer   : RENESAS Semiconductor Training Center
*  Note        : for OAKS8-R5F21114FP(R8C/11 group,20MHz)
*******************************************************************************
* COPYRIGHT(C) 2004 RENESAS TECHNOLOGY CORPORATION
* AND RENESAS SOLUTIONS CORPORATION ALL RIGHTS RESERVED
*******************************************************************************
* History      : ---
*""FILE COMMENT END""*********************************************************/

/*===== include file =====*/
#include "defs.h"                   /* define common symbol               */
#include "strutl.h"                 /* definition file for function dealing with string       */


/*""FUNC COMMENT""*************************************************************
* ID              : ---
* function name   : int STRUTL__strlen(const unsigned char *p);
* function        : get length of string
* parameter       : const char *p    ; string
* return          : int  ; length of string
* function used   : none
* notice          : none
* History         : ---
*""FUNC COMMENT END""*********************************************************/
int STRUTL__strlen(const unsigned char *p)
{
    int len=0;  /* length of string */
    
    /* loop until end of string(null character) */
    for(; '\0' != *p; p++){
        len++;  /* count number of character          */
    }
    return len; /* return number of character     */
}


/*""FUNC COMMENT""*************************************************************
* ID              : ---
* function name   : void STRUTL__strcpy(unsigned char *dest, const unsigned char *src)
* function        : copy string
* parameter       : unsigned char *dest       ; destination string
*                 : const unsigned char *src  ; source string
* return          : none
* function used   : none
* notice          : none
* History         : ---
*""FUNC COMMENT END""*********************************************************/
void STRUTL__strcpy(unsigned char *dest, const unsigned char *src)
{
    /* loop until end of string(null character) */
    for(; '\0' != *src; src++){
        *dest++ = *src;     /* copy character            */
    }
    *dest = '\0';           /* make string(add null character at end)  */
}


/*""FUNC COMMENT""*************************************************************
* ID              : ---
* function name   : STRUTL__x2s(int hex, unsigned char *s, int len)
* function        : change hexadecimal number to string with decided length
* parameter       : int hex;             value from 0x0000 to 0x7fff
*                 : unsigned char *s;             string 
*                 : int len;             decided length
* return          : none
* function used   : STRUTL__x2a() ; change hexadecimal number to a character data
* notice          : pass pointer of buffer s that can save len length string +'\0'
*                 : higher digit at buffer start and lower digit at buffer end
*                 : add 0 from left
* History         : ---
*""FUNC COMMENT END""*********************************************************/
void STRUTL__x2s(int hex, unsigned char *s, int len)
{
    int i;
    
    /* add null character at buffer(s) end */
    s[len] = '\0';
    /* loop from end of buffer(s) to start */
    for(i = len-1; 0 <= i; i--){
        /*change hexadecimal number to a character data */
        STRUTL__x2a(hex % 0x10, &s[i]);     /* make character from lower digit  */
        hex /= 0x10;                        /* take 1/16  */
    }
}


/*""FUNC COMMENT""*************************************************************
* ID              : ---
* function name   : BOOL STRUTL__x2a(int hex, unsigned char *c);
* function        : change hexadecimal number to a character
* parameter       : int hex; number to be changed(from 0x0 to 0xf)
*                 : unsigned char *c; pointer to save character
* return          : BOOL(int) TRUE  ; normal end
*                 :           FALSE ; abnormal end
* function used   : none
* notice          : when value out of 0x0 to 0xf passed, not change, return FALSE
* History         : ---
*""FUNC COMMENT END""*********************************************************/
BOOL STRUTL__x2a(int hex, unsigned char *c)
{
    /* hexadecimal character table */
    static const unsigned char Hexunsigned characterTable[]={
        '0', '1', '2', '3',
        '4', '5', '6', '7',
        '8', '9', 'A', 'B',
        'C', 'D', 'E', 'F'
    };
    
    /* if value from 0x0 to 0xf passed */
    if( (0 <= hex) && (hex <= 0xf) ){
        /* change hexadecimal number to hexadecimal character */
        *c = Hexunsigned characterTable[hex];
        return TRUE;
    }
    /* others */
    return FALSE;  /* return error */ 
}


/*""FUNC COMMENT""*************************************************************
* ID              : ---
* function name   : BOOL STRUTL__s2x(const unsigned char *s, int *hex, int len)
* function        : change string with decided length to hexadecimal number
* parameter       : const unsigned char *src ; string from 0000 to 7FFF
*                 : int *hex        ; changed value
*                 : int len         ; length of string to be changed
* return          : BOOL(int) TRUE  ; normal end
*                 :           FALSE ; abnormal end
* function used   : STRUTL__a2x() ; change a character to hexadecimal number
* notice          : change character of decided length to hexadecimal number from s
*                 : not add ' '(SPACE) from left(no problem to add 0 from left)
*                 : exit changing if find character except hexadecimal number
* History         : ---
*""FUNC COMMENT END""*********************************************************/
BOOL STRUTL__s2x(const unsigned char *s, int *hex, int len)
{
    int i;
    int x;                  /* variable to save hexadecimal number changed from character  */
    int ans = 0;            /* temporary of variable *hex for saving final result */

    /* loop with times of length of string to be changed */
    for(i = 0; i < len; i++){
        if('\0' == *s){
            return FALSE;               /* if find null character in string   */
        }
        /* change a character to hexadecimal number */
        if( ! STRUTL__a2x(*s++, &x) ){
            /* change failed */
            return FALSE;                   /* find character out of hexadecimal character */
        }
        /* get final result */
        ans = ans * 0x10 + x;
    }
    /* return final result */
    *hex = ans;
    /* normal end */
    return TRUE;
}


/*""FUNC COMMENT""*************************************************************
* ID              : ---
* function name   : BOOL STRUTL__a2x(unsigned char c, int *hex);
* function        : change a character to hexadecimal number
* parameter       : unsigned char c;      character to be changed
*                 : int *hex;    pointer to save hexadecimal number 
* return          : BOOL(int) TRUE  ; normal end
*                 :           FALSE ; abnormal end
* function used   : none
* notice          : not change except hexadecimal character, return FALSE
* History         : ---
*""FUNC COMMENT END""*********************************************************/
BOOL STRUTL__a2x(unsigned char c, int *hex)
{
    /* character to be changed is from '0' to '9' */
    if(('0' <= c) && (c <= '9')){
        *hex = c - '0';                     /* change to hexadecimal number */
        return TRUE;
    }
    /* character to be changed is from 'a' to 'f' */
    else if(('a' <= c) && (c <= 'f')){
        *hex = c - 'a' + 0xa;               /* change to hexadecimal number */
        return TRUE;
    }
    /* character to be changed is from 'A' to 'F' */
    else if(('A' <= c) && (c <= 'F')){
        *hex = c - 'A' + 0xa;               /* change to hexadecimal number */
        return TRUE;
    }
    /* others */
    return FALSE;
}


/*""FUNC COMMENT""*************************************************************
* ID              : ---
* function name   : void STRUTL__d2s(int dec, unsigned char *s, int len)
* function        : change decimal number to string with decided length
* parameter       : int dec;             value from 0 to 32767
*                 : unsigned char *s;             string
*                 : int len;             decided length
* return          : none
* function used   : STRUTL__d2a() ; change a character to decimal number
* notice          : pass buffer pointer to s that can save string len length + '\0'
*                 : higher digit at begin of buffer and lower digit at end of buffer
*                 : make character 0 from left
* History         : ---
*""FUNC COMMENT END""*********************************************************/
void STRUTL__d2s(int dec, unsigned char *s, int len)
{
    int i;
    
    /* add null character at end of buffer(s) */
    s[len] = '\0';
    /* loop from begin of buffer(s) to end */
    for(i = len-1; 0 <= i; i--){
        /*change decimal number to a character */
        STRUTL__d2a(dec % 10, &s[i]);           /* change to character from lower digit */
        dec /= 10;                              /* 1/10                 */
    }
}


/*""FUNC COMMENT""*************************************************************
* ID              : ---
* function name   : BOOL STRUTL__d2a(int dec, unsigned char *c)
* function        : change decimal number to a character
* parameter       : int dec;     value to be changed
*                 : unsigned char *c;     pointer to save decimal character
* return          : BOOL(int) TRUE  ; normal end
*                 :           FALSE ; abnormal end
* function used   : none
* notice          : not change except decimal character, return FALSE
* History         : ---
*""FUNC COMMENT END""*********************************************************/
BOOL STRUTL__d2a(int dec, unsigned char *c)
{
    /* decimal character table */
    static const unsigned char Decunsigned characterTable[]={
        '0', '1', '2', '3',
        '4', '5', '6', '7',
        '8', '9'
    };
    
    /* value from 0 to 9 passed*/
    if( (0 <= dec) && (dec <= 9) ){
        *c = Decunsigned characterTable[dec];        /* change it to decimal character   */
        return TRUE;
    }
    /* others */
    return FALSE;                           /* return error           */ 
}


/*""FUNC COMMENT""*************************************************************
* ID              : ---
* function name   : BOOL STRUTL__s2d(const unsigned char *s, int *dec, int len)
* function        : change string with decided length to decimal number
* parameter       : const unsigned char *s;       string from 0000 to 7FFF
*                 : int *dec;            changed value
*                 : int len;             length of string to be changed
* return          : BOOL(int) TRUE  ; normal end
*                 :           FALSE ; abnormal end
* function used   : STRULT__a2d();   change a character to decimal number
* notice          : change character of decided length to decimal number from s
*                 : not add ' '(SPACE) from left(no problem to add 0 from left)
*                 : exit changing if find character except decimal number
* History         :
*""FUNC COMMENT END""*********************************************************/
BOOL STRUTL__s2d(const unsigned char *s, int *dec, int len)
{
    int i;
    int d;                  /* variable to save decimal number changed from character */
    int ans = 0;            /* temporary of variable *dec for saving final result */

    /* loop with times of length of string to be changed */
    for(i = 0; i < len; i++){
        if('\0' == *s){
            return FALSE;           /* if find null character in strin   */
        }
        /* change a character to decimal number */
        if( ! STRUTL__a2d(*s++, &d) ){
            /* change failed */
            return FALSE;                       /* find character out of decimal character  */
        }
        /* get final result */
        ans = ans * 10 + d;
    }
    /* return final result */
    *dec = ans;
    /* normal end */
    return TRUE;
}


/*""FUNC COMMENT""*************************************************************
* ID              : ---
* function name   : BOOL STRUTL__a2d(unsigned char c, int *dec);
* function        : change a character to decimal number
* parameter       : unsigned char c;      character to be changed
*                 : int *dec;    pointer to save decimal number
* return          : BOOL(int) TRUE  ; normal end
*                 :           FALSE ; abnormal end
* function used   : none
* notice          : not change except decimal character, return FALSE
* History         : ---
*""FUNC COMMENT END""*********************************************************/
BOOL STRUTL__a2d(unsigned char c, int *dec)
{
    /* character to be changed is from '0' to '9' */
    if( ('0' <= c) && (c <= '9') ){
        *dec = c - '0';                 /* change to decimal number */
        return TRUE;
    }
    /* others */
    return FALSE;
}


/******************************************************************************
    end of file
******************************************************************************/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -