Classe de type unsigned byte

Description

Permet de manipuler des unsigned byte, plusieurs operations logiques sont implementees.

Source / Exemple :


class Pi_byte
{
	private int pi_byte;
	private char[] codehexa;
	
	public Pi_byte( int value )
	{
		pi_byte=value;
		codehexa = new char[2];
	}
	
	public Pi_byte()
	{
		pi_byte=0;
		codehexa = new char[2];
	}

	public Pi_byte( String hexa )
	{	
		codehexa = new char[2];
		
		codehexa = hexa.toCharArray();
		
		pi_byte = ( charToHex( codehexa[0] ) << 4 ) & 0xf0;
		pi_byte = pi_byte | ( charToHex( codehexa[1]) & 0x0f);
	}
	
	private int charToHex( char c )
	{
		switch( c )
		{
			case '0': return(0);
			case '1': return(1);
			case '2': return(2);
			case '3': return(3);
			case '4': return(4);
			case '5': return(5);
			case '6': return(6);
			case '7': return(7);
			case '8': return(8);
			case '9': return(9);
			case 'A': return(10);
			case 'B': return(11);
			case 'C': return(12);
			case 'D': return(13);
			case 'E': return(14);
			case 'F': return(15);
			default : return(0);
		}
	}
	
	private char hexToChar( int value )
	{
		value = value & 0xf;
		
		switch( value )
		{
			case 0: return('0');
			case 1: return('1');
			case 2: return('2');
			case 3: return('3');
			case 4: return('4');
			case 5: return('5');
			case 6: return('6');
			case 7: return('7');
			case 8: return('8');
			case 9: return('9');
			case 10: return('A');
			case 11: return('B');
			case 12: return('C');
			case 13: return('D');
			case 14: return('E');
			case 15: return('F');
			default : return('0'); /* dead code */
		}
	}
	
	int getValue()
	{
		return( pi_byte );
	}
	
	void setValue( int value )
	{
		pi_byte = value;
	}

	void setValue( String hexa )
	{	
		codehexa = hexa.toCharArray();
		
		pi_byte = ( charToHex( codehexa[0] ) << 4 ) & 0xf0;
		pi_byte = pi_byte | ( charToHex( codehexa[1]) & 0x0f);
	}

	void swap()
	{
		int swp;
		
		swp=((pi_byte & 0xf)<<4);
		pi_byte=(pi_byte>>4 ) & 0xf;
		pi_byte |= swp;
	}
	
	void shiftL( int value )
	{
		pi_byte = (pi_byte<<(((value-1) & 0x7)+1 ) & 0xFF );
	}
	
	void shiftR( int value )
	{
		pi_byte = (pi_byte>>(((value-1) & 0x7)+1 ) & 0xFF );
	}
	
	String toHexString()
	{	
		codehexa[1] = hexToChar( pi_byte & 0xf );
		codehexa[0] = hexToChar( (pi_byte>>4) & 0xf );
		
		return( String.valueOf( codehexa ));
	}
			
	void xor( Pi_byte pb2 )
	{
		pi_byte = pb2.getValue() ^ pi_byte;
	}
	
	void xor( int value )
	{
		pi_byte = ( value & 0xff )^ pi_byte;
	}

	void xor( String hexa )
	{
		int value;
		codehexa = hexa.toCharArray();
		
		value = ( charToHex( codehexa[0] ) << 4 ) & 0xf0;
		value = value | ( charToHex( codehexa[1]) & 0x0f);
		
		pi_byte = (value & 0xff ) ^ pi_byte;
	}
	
	void and( Pi_byte pb2 )
	{
		pi_byte = pb2.getValue() & pi_byte;
	}
	
	void and( int value )
	{
		pi_byte = (value & 0xff ) & pi_byte; 
	}
	
	void and( String hexa )
	{
		int value;
		codehexa = hexa.toCharArray();
		
		value = ( charToHex( codehexa[0] ) << 4 ) & 0xf0;
		value = value | ( charToHex( codehexa[1]) & 0x0f);
		
		pi_byte = (value & 0xff ) & pi_byte;
	}
	
	void or( Pi_byte pb2 )
	{
		pi_byte = pb2.getValue() | pi_byte;
	}
	
	void or( int value )
	{
		pi_byte = ( value & 0xff ) | pi_byte;
	}
	
	void or( String hexa )
	{
		int value;
		codehexa = hexa.toCharArray();
		
		value = ( charToHex( codehexa[0] ) << 4 ) & 0xf0;
		value = value | ( charToHex( codehexa[1]) & 0x0f);
		
		pi_byte = (value & 0xff ) | pi_byte;		
	}
	
	void not()
	{
		pi_byte = ( ~pi_byte ) & 0xff;
	}
	
	void setBit( int value )
	{
		pi_byte = pi_byte | ( 1<<(value & 0x7) );
	}
	
	void clrBit( int value )
	{
		pi_byte = pi_byte & (~( 1<<(value & 0x7 ) ));
	}
	
	public static void main( String argv[] )
	{
		Pi_byte v1, v2;
		
		v1 = new Pi_byte( 100 );
		v2 = new Pi_byte();
		
		System.out.println( "Value v1 = "+v1.getValue());
		System.out.println( "Value v2 = "+v2.getValue());
		
		v2.setValue( 224 );
		
		System.out.println( "Value v2 = "+v2.getValue());
		System.out.println( "V1 to hexa = "+v1.toHexString());
		System.out.println( "V2 to hexa = "+v2.toHexString());
		v2.swap();
		System.out.println( "Value of V2 swap = "+v2.getValue());
		v1.shiftL(1);
		System.out.println( "Value of V1 shiftleft 1 = "+v1.getValue());
		v1.shiftR(1);
		System.out.println( "Value of V1 shiftright 1 = "+v1.getValue());
		v2.setValue( "C0" );
		System.out.println( "Value of v2 set C0 = "+v2.getValue());
		v1.setValue( "55" );
		v2.setValue( "AA" );
		System.out.println( "Value of v1 = "+v1.toHexString()+" v2 = "+v2.toHexString());
		v1.and( v2 );
		System.out.println( "Value of v1 = "+v1.getValue());
		v1.or( v2 );
		System.out.println( "Value of v1 = "+v1.getValue());
		v1.xor( "55" );
		System.out.println( "Value of v1 = "+v1.getValue());
		v1.shiftL(4);
		System.out.println( "Value of v1 = "+v1.getValue());
		v1.shiftR(4);
		System.out.println( "Value of v1 = "+v1.getValue());
		v1.setBit(7);
		System.out.println( "Value of v1 = "+v1.getValue());
		v1.clrBit(0);
		System.out.println( "Value of v1 = "+v1.getValue());
		v1.clrBit(1);
		v1.not();
		System.out.println( "Value of v1 = "+v1.toHexString());
		
	}
}

Conclusion :


Bientot une version 16 bits pour manipuler des unsigned word.

Codes Sources

A voir également

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.

Du même auteur (selligattangip)