All pastes #57326 Raw Edit

Something

public text v1 · immutable
#57326 ·published 2006-05-20 17:17 UTC
rendered paste body
bash-3.00# more tigrux_char_float.c

#include <stdio.h>
#include <inttypes.h>
                                                                               
typedef __int8_t byte;

union float_bytes {
         byte b[sizeof (float)];
         float f;
       } ;
                                                                               

union double_bytes {
         byte b[sizeof (double)];
         double f;
       } ;

                                                                               
int main() {
int i,cuentafloat;
char cadenaflotantes[512];
char numero[4];
float *elnuevo;
float vector[512];
        union float_bytes a_number;
        printf("Size of char type is: %d (must be 1)\n", sizeof (char) );
        printf("Enter a number: ");
        scanf("%f",&a_number.f);
/*for(i=0; i< sizeof(float); i++) printf("Byte %d is %02Xh\n", i, 0xff & a_numbe
r.b[i]);*/

for(i=0; i< sizeof(float); i++) printf("Byte %d is %02Xh\n", i, 0xff & a_number.
b[i]);

/*numero[0] = a_number.b[0];
numero[1] = a_number.b[1];
numero[2] = a_number.b[2];
numero[3] = a_number.b[3];*/

memcpy((void *) numero,(void *)a_number.b,4);

for(i=0; i< sizeof(float); i++) printf("Numero %d es %02Xh\n", i, 0xff & numero[
i]);



elnuevo=(float*) &numero;
/*                                               */

printf("elnuevo numero es %e\n",*elnuevo);


/*cadena de flotantes 4 bytes adentro */


for(i=0; i< 512; i=i+4) {
cadenaflotantes[i]=a_number.b[0];
cadenaflotantes[i+1]=a_number.b[1];
cadenaflotantes[i+2]=a_number.b[2];
cadenaflotantes[i+3]=a_number.b[3];

                    }


printf("double %d \n",sizeof(double));
printf("long double %d \n",sizeof(long double));

printf("elnuevo tama~no %d \n",sizeof(cadenaflotantes));
printf("elnuevo string es %s \n",cadenaflotantes);

/*cadena de flotantes 4 bytes hacia afuera,.. ahora */
/* 128 floats, solamente por los 512 bytes */
        
cuentafloat=1;  
for(i=0; i< 512; i=i+4) {
numero[0] = cadenaflotantes[i];
numero[1] = cadenaflotantes[i+1];
numero[2] = cadenaflotantes[i+2];
numero[3] = cadenaflotantes[i+3];
elnuevo=(float*) &numero;
vector[cuentafloat]=*elnuevo;
cuentafloat++;
                        }



for(i=1;i<129;i++){
printf("aqui los vectores del otro lado %d = %f\n",i,vector[i]);
                  }


/* printf("Numero \n", i,cadenaflotantes[i]);*/


}