IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Langage Java Discussion :

Cast int into byte


Sujet :

Langage Java

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Battosaiii
    Invité(e)
    Par défaut Cast int into byte
    Bonjour,

    J'aimerais transformer une variable int en byte. Comment faire ?

    J'ai tente :

    int c;
    byte[][][] x;
    x[1][1][1]=(byte)c;

    mais mon programme ne marche pas.
    Comment faire ?

  2. #2
    Membre éprouvé
    Avatar de mavina
    Homme Profil pro
    Développeur Java
    Inscrit en
    Octobre 2004
    Messages
    1 812
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 40
    Localisation : Chine

    Informations professionnelles :
    Activité : Développeur Java
    Secteur : Conseil

    Informations forums :
    Inscription : Octobre 2004
    Messages : 1 812
    Par défaut
    salut,

    Déja tu n'initialise pas ton tableau dynamiquement, ni ton entier :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
     
    int c=1;
    byte[][][] x=new byte[taille1][taille2][taille3];
    Ensuite, il faut utiliser la fonction de Integer byteValue() :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
    byte[1][1][1]=new Integer(c).byteValue();
    Bye

    Fred

  3. #3
    Battosaiii
    Invité(e)
    Par défaut
    ca marche pas non plus, mon probleme vient d'autre part peut etre ...

    Mon programme ne m'affiche pas d'erreur il est juste en train de reflechir eternellement.

    si jet mets x[1][1][1]=(byte)0xff; ca marche.
    en revanche avec
    short[][][] voxels;
    x[1][1][1]=(byte)voxels[1][1][1];
    mon programme ne s'acheve pas.

    Devrais je renommer short[][][] voxels; par
    byte[][][] voxels ;????

    Des idees ? Quelles sont mes erreurs ?

  4. #4
    Membre éprouvé
    Avatar de mavina
    Homme Profil pro
    Développeur Java
    Inscrit en
    Octobre 2004
    Messages
    1 812
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 40
    Localisation : Chine

    Informations professionnelles :
    Activité : Développeur Java
    Secteur : Conseil

    Informations forums :
    Inscription : Octobre 2004
    Messages : 1 812
    Par défaut
    es-tu sur que tu n'as pas d'exception levée ?

    Pourrais tu poster ta classe entiere ?

    merci

    Fred

  5. #5
    in
    in est déconnecté
    Membre Expert Avatar de in
    Profil pro
    Inscrit en
    Avril 2003
    Messages
    1 612
    Détails du profil
    Informations personnelles :
    Localisation : France, Finistère (Bretagne)

    Informations forums :
    Inscription : Avril 2003
    Messages : 1 612
    Par défaut
    j'ai pas lu le topic mais je pense que tout ce dont tu as besoin se trouve ici :
    http://gfx.developpez.com/tutoriel/java/binaire/

  6. #6
    Battosaiii
    Invité(e)
    Par défaut
    mavina j'apprecie ton aide et voici les classes utiles de mon programme :

    Voici la classe RegionGrowing :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
     
     public class RegionGrowing {
     
     
    	  /** Source pixels */
    	  protected static short[][][] srcPixels = null;
     
    	  public VoxelData Voxelsrc;
     
    	  /**  Value of a member pixel in output image */
    	  public final static byte MARKER = (byte) 0xff;
     
    	  /**  Value of background (not member) pixel in output image */
    	  public final static byte BACKGROUND = (byte) 0x00;
    //marker= -1 not member=1 BACKGROUND=0
    	  /**
               *  Used to mark pixels that were determined to not be members. This value is
               *  changed to BACKGROUND in the output image.
               */
    	  protected final static byte NOT_MEMBER = (byte) 0x01;
     
    	  /** Pixels of the destibation image */  
    	  protected byte[][] destPixels = null;
     
    	  protected byte[][][] destVoxels =null;
     
     final protected void checkForGrow(int x, int y, int z) {
    		    if (x < xMin || x >= xMax ||
    		        y < yMin || y >= yMax ||
    		        z < zMin || z >= zMax) {
    		      return;
    		    }
     
     
    		      int value = this.Voxelsrc.getSample(x,y,z);
     
    		      if (value >= valueMin && value < valueMax) {
     
    		    	destVoxels[x][y][z] =new Integer(this.Voxelsrc.getSample(x,y,z)).byteValue();//MARKER;//(byte) this.Voxelsrc.getSample(x,y,z);//MARKER;
    		    	//  System.out.println("value ok= "+value);
    		       candidatePoints.addLast(new Voxel(x,y,z));
    		      } else {
    		        destVoxels[x][y][z] = NOT_MEMBER;
    		      }
    		    }
    		    else
    		    {
    		    	//System.out.println("(this.Voxelsrc.getSample(x,y,z)= "+(this.Voxelsrc.getSample(x,y,z)));
    		    }
     
    		  }
    C'est dans cette classe que certains pixels vont etre selectionne suivant un critere. Ces pixels sont selectionnes parmi les pixels crees dans la classe VoxelkData :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
     
    package src.testproject;
     
    import java.io.*;
    import java.awt.image.*;
    import javax.swing.*;
    import java.awt.*;
     
    public class VoxelData extends javax.swing.JFrame
    {
    	/**
             * This is the 3D byte array of sample values that have been successfully converted 
             * to the correct range of values for the sample. 
             * NOTE: They may not need to be converted because I am not viewing them
             * yet straight.
             * */
    	private short[][][] voxels;
    	private byte[][][] bytes;
     
     
        private String str_datasetPath="C:/Etudes/DCU/ee593 Telecom project/3dVisualisation/Help from Vincent/0017384_s";
    //Give the path to the dataset
        private javax.swing.JLabel jLabelSlice;
        private javax.swing.JSlider jSlider1;
        private InputStream in;
        private short widthInVoxels;
        private short heightInVoxels;
        private short depthInVoxels;
        private short maxDensityValue;
        private short minDensityValue;
        private int int_nbByteImage;
        private int int_ColorRange;
     
        private float voxelWidth ; //mm per voxel on x
        private float voxelHeight; //mm per voxel on y
        private float voxelDepth;
     
        private int check;
     
        private BufferedImage ScanImage;
        //Constructor
        public VoxelData()
        {
            initComponents();
           // displaySlice(1);
        }
     
     private void initComponents()
        {//
      //[rows][columns][slices]
            	voxels = new short[widthInVoxels][heightInVoxels][ depthInVoxels];
     
    			//We use twice the number of columns cause we need two bytes for each voxel,hence the volume will be twice the width.
    			bytes = new byte[depthInVoxels][heightInVoxels][2*widthInVoxels ];
    			for(int z=0;z<depthInVoxels;z++)
    			//	for(int z=0;z<60;z++)
    			{
    				for(int y=0; y<heightInVoxels; y++)
    				{
    					check=in.read(bytes[z][y]);
    					for(int x=0;x<widthInVoxels;x++)
    					{
    						//Converts 2 bytes to a short. Each short is in the range -1024 -> +2048
    						//This file reading process takes 11secs. Could leave the voxels as
    						//bytes to speed up the reading process to 1sec.Leave out this loop to do so.
    	                	voxels[x][y][z]=(short)((bytes[z][y][2*x]<<8)|((bytes[z][y][(2*x)+1])& 0xff));
    	                //	System.out.println("voxels= "+voxels[x][y][z]+"bytes= "+((bytes[z][y][2*x]<<8)|((bytes[z][y][(2*x)+1])& 0xff)));
    	                    /* int int_vox = (readShort()-minDensity)*255/(maxDensity-minDensity); //convert the voxel value into a range from 0 to 255
    	                    //The int_vox is ANDed with a hex value,then shifted left by two bytes, then ORed with the other values.
    	                    //This method of shifting and masking is a good way of separating the different bytes in an int or float etc.  
    	                    int int_hex = 0xff000000 | ((int_vox & 0x000000ff) << 16) | ((int_vox & 0x000000ff) << 8) | (int_vox & 0x000000ff); */
    					}
     
    	            }
    			}
     
        public int getSample( int row, int column, int slice ){
        	return(this.voxels[row][column][slice]);
    J'ai vire le superflus : j'espere avoir ete assez clair .

  7. #7
    Battosaiii
    Invité(e)
    Par défaut
    Pourquoi j'obtiens pas de reponse ? est ce que mon poste etait trop long ?

  8. #8
    Membre éprouvé
    Inscrit en
    Juillet 2006
    Messages
    74
    Détails du profil
    Informations personnelles :
    Âge : 42

    Informations forums :
    Inscription : Juillet 2006
    Messages : 74
    Par défaut
    Citation Envoyé par Battosaiii

    si jet mets x[1][1][1]=(byte)0xff; ca marche.
    en revanche avec
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    short[][][] voxels;
    x[1][1][1]=(byte)voxels[1][1][1];
    mon programme ne s'acheve pas.

    Devrais je renommer short[][][] voxels; par
    byte[][][] voxels ;????

    Des idees ? Quelles sont mes erreurs ?
    Je ne sais pas si tu as résolu ta question mais par rapport à ce post, juste une remarque.

    Deja tes tableaux short[][][] voxels et x[1][1][1] sont des objets Java donc ton cast ne va surement pas marcher (normalement avec eclipse il te signale cette erreur)
    donc pour transformer ton tableau de short en tableau de byte, il faut le faire element par element, avec un parcours de ton tableau

    voila en esperant que ça te depanne

Discussions similaires

  1. int vers byte[4]
    Par ulquiorra dans le forum Général Java
    Réponses: 6
    Dernier message: 15/03/2011, 16h36
  2. conversion unsigned int en BYTE[] VC++
    Par koukou11 dans le forum VC++ .NET
    Réponses: 4
    Dernier message: 11/03/2011, 01h24
  3. Conversion int en Byte
    Par nonolerobot77 dans le forum C++
    Réponses: 2
    Dernier message: 14/02/2008, 08h17
  4. [Debutant(e)]conversion int to byte[] et inversement
    Par mune dans le forum Général Java
    Réponses: 23
    Dernier message: 21/07/2005, 14h47
  5. [Conversion]convertir int[] en bytes[]
    Par tony_big_guy dans le forum Langage
    Réponses: 5
    Dernier message: 02/05/2005, 15h47

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo