2013年11月14日 星期四

sscanf with Android NDK

It seems that there are some bug in sscanf() with Android NDK-r9.

When read a line from a text file, i use the strtok() to tokenize the string, and call sscanf() to convert the token into hex value. There are some chance to get 0 from sscanf() , although it is not always occurred.

To solve the problem in NDK, i write a simple function to convert the token into hex,  the code is as follow listing:

// sscanf has bug in Android NDK
static unsigned char convertToHex( char c ) {
    if ( c >= '0' && c <= '9' ) {
        return c - '0';
    }
    if ( c >= 'A' && c <= 'F' ) {
        return c - 'A' + 10;
    }
    if ( c >= 'a' && c <= 'f' ) {
        return c - 'a' + 10;
    }
    return 0;
}
static unsigned char bytesToHex( char *token ) {
    unsigned char tmp ;
    unsigned char tmp1;
    unsigned char tmp2;
    tmp1 = convertToHex( token[0] );
    tmp2 = convertToHex( token[1] );
    tmp = tmp1 << 4 | tmp2;
    return tmp ;
};

沒有留言: