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 469 / 93

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

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... 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++ Sérialisation [ par saispasq ] salut &#224; tou, je veu savoir comen s&#233;rialiser des param&#233;tres dans une m&#233;thode Java; exple: une methode drawsolidline(int , int , by Convertion Object ou byte[] en jpeg [ par oliversleep ] Salut tout le monde, voil&#224; je commence en Java, et je r&#233;alise actuellement un client/serveur d'image. Actuellement, tout le transfert est passer de Image a Byte[] [ par chocho14 ] bonjour,j'aimerais savoir comment recuperer une image de type Image dans un buffer de type Bytes[].Par contre l'inconvenient est que ca doit etre fait Aide scannet [ par anen ] Salut ,Je suis en train d'elaborer un projet(pfe) "NETBROWSER" ,utilisant Jpcap et un programme "scannet" qui me permet de capter tout les adresses ip String & byte[] [ par DarkSchneider ] Bonjour tout le monde, &nbsp;&nbsp;&nbsp; J'aurais aimer savoir s'il &#233;tait possible de copier un string dans un byte[] sans perte de donne&#233; Comparaison de tableaux de byte... [ par Foub12 ] Bonjour,comment puis-je faire pour effectuer une comparaison de tableaux de bytes : byte[]J'ai bien essay&#233; de faire &#171; == &#187; ou .equals() transformer une image en byte[] [ par obigero ] bon ben je fais appel a vous en dernier recours, g pas reussi a transformer une image (jpeg, gif ou tif) en byte[]. y'a bcp de prog qui font l'inverse Problème avec les données signées en JAVA [ par Gwillherm ] Bonjour, je suis en train de coder en JAVA un programme permettant l'utilisation du protocole de communication MODBUS et dont les trames sont une suit


Nos sponsors

Sondage...

CalendriCode

Juillet 2009
LMMJVSD
  12345
6789101112
13141516171819
20212223242526
2728293031  

Consulter la suite du CalendriCode

Téléchargements

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

Comparez les prix Nouvelle version

Photothèque Nouveau !



Développement réalisé par Nicolas SOREL (Nix) avec l'aide de : Cyril DURAND et Emmanuel (EBArtSoft), 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 : 1,014 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é.