Exemple #1
0
//--------------------------------------------------------------------------------------------------
int32_t le_hex_BinaryToString
(
    const uint8_t *binaryPtr,  ///< [IN] binary array to convert
    uint32_t       binarySize, ///< [IN] size of binary array
    char          *stringPtr,  ///< [OUT] hex string array, terminated with '\0'.
    uint32_t       stringSize  ///< [IN] size of string array
)
{
    int32_t idxString,idxBinary;

    if (stringSize < 2*binarySize+1)
    {
        LE_DEBUG("Hex string array (%u) is too small (%u)",stringSize, binarySize);
        return -1;
    }

    for(idxBinary=0,idxString=0;
        idxBinary<binarySize;
        idxBinary++,idxString=idxString+2)
    {
        stringPtr[idxString]   = DecToHex( (binaryPtr[idxBinary]>>4) & 0x0F);
        stringPtr[idxString+1] = DecToHex(  binaryPtr[idxBinary]     & 0x0F);
    }
    stringPtr[idxString] = '\0';

    return idxString;
}
Exemple #2
0
int main()
{
  int n;
  scanf("%d", &n);

  while (n--) {
    int x;
    scanf("%d", &x);

    printf("%d %d\n", CountOneBinary(x), CountOneBinary(DecToHex(x)));
  }

  return 0;
}
Exemple #3
0
// read the device ID
static int get_device_id()
{
	char id[10];

#if( GET_DEVICE_ID_WAY == GET_DEVICE_ID_FORM_CHIP )
{
	static char get108ACnt ;

	get108ACnt = 0 ;
	do
	{
		int init_108Ast  = init_execute_cmd() ;

		if (init_108Ast < 0)
		{
			get108ACnt++ ;
			usleep(50000) ;
		}
		else
		{
			break; //init successed.
		}
	}while( get108ACnt < 10 );

	if( get108ACnt >= 10)
	{
		Display_log_E("<NET LOG>init_execute_cmd error!");
		return -1;
	}

	get108ACnt = 0 ;
	char buf[48];
	do
	{
		int ret = execute_cmd(0x02, 0x80, 0, 2, 0, 0, 0, buf, 48, 50 * 1000);
		if (ret < 2)
		{
			get108ACnt++ ;
			usleep(50000) ;
		}
		else
		{
			break;//Get device code successed.
		}
	}while( get108ACnt < 10 );

	if( get108ACnt >= 10)
	{
		Display_log_E("<NET LOG> - execute_cmd errorerror!");
		return -1;
	}

	memcpy(id, buf, 4);
	memcpy(&id[4], &buf[8], 5);
	DecToHex(device_code, id, 9);
}
#elif( GET_DEVICE_ID_WAY == GET_DEVICE_ID_FORM_CHIP )
{
    int Dev_fd,Dev_size ;
    char buf[48];

    Dev_size = 18 ;

    Dev_fd = open(DEVICE_CODE_FILE,O_RDONLY );

    if(Dev_fd < 0)
    {
        Display_log_E("<Net log> - Get device code from chip error");
    }
    else
    {
    	int static read_cnt = 0 ;
    	int bytes_read = 0 ;

    	do
		{
    		char cnt = 0 ;
    		bytes_read = read(Dev_fd, device_code , Dev_size) ;
    		if( bytes_read >= 1 )
    		{
    			for(unsigned char i =0 ;i<Dev_size; i++ )
    			{
    				if( device_code[i] == 0xFF )
    				{
    					cnt++ ;
    				}
    			}

    			if(cnt < Dev_size)
    			{
    				break;
    			}
    		}

    		read_cnt++;
    		usleep(1000);
    	}while( read_cnt < 10 );

    	if( read_cnt >= 10 )
    	{
    		Display_log_E("<Net log> - Get device code from file error");
    	}
    }

    close(Dev_fd);
}
#else
{
	memcpy(id, device_code, 9);
	id[9] = 0;
	DecToHex(device_code, id, 9);
}
#endif

return 0;
}
string NASCToolBox::DecToHex( unsigned long i ) const
{
	const char Table[ 16 ] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
	if( i > 0 ) return DecToHex( i / 16 ) + Table[ i % 16];
	else return "";
}
Exemple #5
0
bool wxXPMHandler::SaveFile(wxImage * image,
                            wxOutputStream& stream, bool WXUNUSED(verbose))
{
    wxString tmp;
    char tmp_c;

    // 1. count colours:
    #define MaxCixels  92
    static const char Cixel[MaxCixels+1] =
                         " .XoO+@#$%&*=-;:>,<1234567890qwertyuipasdfghjk"
                         "lzxcvbnmMNBVCZASDFGHJKLPIUYTREWQ!~^/()_`'][{}|";
    int chars_per_pixel;
    int cols;
    int i, j, k;

    cols = image->CountColours();
    chars_per_pixel = 1;
    for ( k = MaxCixels; cols > k; k *= MaxCixels)
        chars_per_pixel++;

    // 2. write the header:
    wxString sName;
    if ( image->HasOption(wxIMAGE_OPTION_FILENAME) )
    {
        wxSplitPath(image->GetOption(wxIMAGE_OPTION_FILENAME),
                    NULL, &sName, NULL);
        sName << wxT("_xpm");
    }

    if ( !sName.IsEmpty() )
        sName = wxString(wxT("/* XPM */\nstatic char *")) + sName;
    else
        sName = wxT("/* XPM */\nstatic char *xpm_data");
    stream.Write( (const char*) sName.ToAscii(), sName.Len() );

    char tmpbuf[200];
    // VS: 200b is safe upper bound for anything produced by sprintf below
    //     (<101 bytes the string, neither %i can expand into more than 10 chars)
    sprintf(tmpbuf,
               "[] = {\n"
               "/* columns rows colors chars-per-pixel */\n"
               "\"%i %i %i %i\",\n",
               image->GetWidth(), image->GetHeight(), cols, chars_per_pixel);
    stream.Write(tmpbuf, strlen(tmpbuf));

    // 3. create color symbols table:
    wxImageHistogram histogram;
    image->ComputeHistogram(histogram);

    char *symbols_data = new char[cols * (chars_per_pixel+1)];
    char **symbols = new char*[cols];

    // 2a. find mask colour:
    unsigned long mask_key = 0x1000000 /*invalid RGB value*/;
    if (image->HasMask())
        mask_key = (image->GetMaskRed() << 16) |
                   (image->GetMaskGreen() << 8) | image->GetMaskBlue();

    // 2b. generate colour table:
    for (wxImageHistogram::iterator entry = histogram.begin();
         entry != histogram.end(); ++entry )
    {
        unsigned long index = entry->second.index;
        symbols[index] = symbols_data + index * (chars_per_pixel+1);
        char *sym = symbols[index];

        k = index % MaxCixels;
        sym[0] = Cixel[k];
        for (j = 1; j < chars_per_pixel; j++)
        {
            k = ((index - k) / MaxCixels) % MaxCixels;
            sym[j] = Cixel[k];
        }
        sym[j] = '\0';

        unsigned long key = entry->first;

        if (key == 0)
            sprintf( tmpbuf, "\"%s c Black\",\n", sym);
        else if (key == mask_key)
            sprintf( tmpbuf, "\"%s c None\",\n", sym);
        else
        {
            char rbuf[3];
            DecToHex( (unsigned char)(key >> 16), rbuf );
            char gbuf[3];
            DecToHex( (unsigned char)(key >> 8), gbuf );
            char bbuf[3];
            DecToHex( (unsigned char)(key), bbuf );
            sprintf( tmpbuf, "\"%s c #%s%s%s\",\n", sym, rbuf, gbuf, bbuf );
        }
        stream.Write( tmpbuf, strlen(tmpbuf) );
    }

    tmp = wxT("/* pixels */\n");
    stream.Write( (const char*) tmp.ToAscii(), tmp.Length() );

    unsigned char *data = image->GetData();
    for (j = 0; j < image->GetHeight(); j++)
    {
        tmp_c = '\"'; stream.Write(&tmp_c, 1);
        for (i = 0; i < image->GetWidth(); i++, data += 3)
        {
            unsigned long key = (data[0] << 16) | (data[1] << 8) | (data[2]);
            stream.Write(symbols[histogram[key].index], chars_per_pixel);
        }
        tmp_c = '\"'; stream.Write(&tmp_c, 1);
        if ( j + 1 < image->GetHeight() )
        {
            tmp_c = ','; stream.Write(&tmp_c, 1);
        }
        tmp_c = '\n'; stream.Write(&tmp_c, 1);
    }
    tmp = wxT("};\n");
    stream.Write( (const char*) tmp.ToAscii(), 3 );

    // Clean up:
    delete[] symbols;
    delete[] symbols_data;

    return true;
}