Vous ne trouvez pas de réponse à votre problème ? Alors posez la question dans le forum. Souvenez-vous qu'il n'y a jamais de question bête, mais rester dans l'ignorance parce que l'on n'ose pas poser une question, ça c'est une erreur !

CLASSE DE TYPE UNSIGNED BYTE


Information sur la source

Catégorie :Api Classé sous : unsigned, byte, ubyte Niveau : Débutant Date de création : 06/08/2003 Date de mise à jour : 06/08/2003 16:54:48 Vu / téléchargé: 4 103 / 84

Note :
8 / 10 - par 1 personne
8,00 / 10

  • 1

  • 2

  • 3

  • 4

  • 5

  • 6

  • 7

  • 8

  • 9

  • 10

Commentaire sur cette source (0)
Ajouter un commentaire et/ou une note

Description

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

Source

  • 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());
  • }
  • }
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.
 

Fichier Zip

Pour les "Membres Club", vous pouvez télécharger directement un fichier contenu dans le zip sans télécharger le zip en entier !

Télécharger le zip

Commentaires et avis

Aucun commentaire pour le moment.

Ajouter un commentaire

Discussions en rapport avec ce code source dans le forum

unsigned byte [ par dudulebzh35 ] bonjour a tous,je souhaite utiliser un byte nom signé (unsigned byte) or ca n'existe pas, comment faire ?en vous remercianta++ Convertir un byte en unsigned byte [ par FiReTiTi ] Bonjour,j'ai un byte et je souhaiterai avoir sa valeur en unsigned byte...Ou comment convertir mon byte en unsigned byte ?Comment faire ?Merci... Lire une video à partir d'un tableau de byte [ par shindara38 ] Bonjour à tous,je suis actuellement en train de développer une application java, qui doit se connecter à une base de données postgre. J'aimerai pouvoi Problème de conversion String --> Object [ par ploxien ] Hello, Mon problème: L'idée, c'est qu'en entrée j'ai une liste de String, et que je désire utiliser cette liste pour setter la valeur d'un attribut da Convertir un String en Byte [ par theneo55 ] Bonjour à tous,dans le projet que j'effectue je dois convertir un string (ex : 7e000100000107f) en un objet byte, sachant que 7e en byte devrait donne java UDP audio [ par eikichi ] Bonjour a tous, Je cherche à transférer un flux audio en utilisant des sockets en UDP. Le flux est déjà subdivisé en plusieurs petits paquets UDP que classe MessageDigest [ par makdand ] convertion de Word vers Byte [ par dunith ] bonjour;j'ai créer une méthode qui converti mon tableau de word en byte.......//La méthode qui permet de convertir en byte    private void ConvertiInB


Nos sponsors

Sondage...

CalendriCode

Décembre 2008
LMMJVSD
1234567
891011121314
15161718192021
22232425262728
293031    

Consulter la suite du CalendriCode

Téléchargements

Logiciels à télécharger sur le même thème :



Développement réalisé par Nicolas SOREL (Nix) avec l'aide de : Cyril DURAND et Emmanuel BAÏSE, Merci à Vincent pour ses précieux conseils
CodeS-SourceS.com© Toute reproduction même partielle est interdite sauf accord écrit du Webmaster
CodeS-SourceS.com© est une marque déposée tous droits réservés
Temps d'éxécution de la page : 0,343 sec

Google Coop CodeS-SourceS Google Coop CodeS-SourceS


Certaines images présentes sur le site (notament certains avatars) sont issues des collections IconShock, donc si vous souhaitez utiliser ces icons vous devez les acheter, ne les copiez pas et ne utilisez pas dans vos sites et applications sans les avoir commandé.