#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2017
    Posts
    16
    Rep Power
    0

    How to store 3 different variables into single binary string?


    Hi,

    How to store 3 different variables into single binary string without losing the variable values? I need logic to proceed my work.

    I have 3 variables like pressure, temperature and humidity, which are in decimal values.

    For hardware implementation, I need to convert 3 different decimal values to binary values, and need to store into single binary string without losing the variable values .

    My questions:

    u32 pressure=15423;
    int temperature =26;
    u32 humidity=25; I need to convert 3 variables into binary values, and i need to store all binary values into single array.

    Logic please
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2017
    Posts
    16
    Rep Power
    0
    How to Concatenate array1, array2 and array3 values of binary numbers of different lengths into single array values. Please suggest me.

    Without using strcat, sprintf(), snprintf(), vsprintf() and vsnprintf()
    Code:
    int main() 
    {
        int temperature=25;
        int pressure =152383;
        int humidity =26;
    	int  array1[8]={0};
    	int  array2[8]={0};
    	int  array3[18]={0};
    	long long result;
    //	int *ai;
    
    int i,j;
    for (i = 0; i<8; i++)
    {
        array1[7 - i] = (temperature >> i) & 1;
        
    }
    for (j = 0; j<8; j++)
    {
        
        array2[7 - j] = (humidity >> j) & 1;
        
    }
    for (i = 0; i<18; i++)
    {
        array3[17 - i] = (pressure >> i) & 1;
        
    }
    
     printf("\n");
      //ai= (int *)(array);
    for (i = 0; i<8; i++)
    {
        printf("%d", array1[i]);
     
      
    }
    printf("\n");
    for (j = 0; j<8; j++)
    {
       
        printf("%d", array2[j]);
    }
    printf("\n");
    for (j = 0; j<18; j++)
    {
       
        printf("%d", array3[j]);
    }
    }
  4. #3
  5. Contributing User
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Aug 2011
    Posts
    5,888
    Rep Power
    509
    You don't need binary, although it'll be a little more efficient.
    Step 1) Transmit the data length, the array, and repeat from step 1.

    Another wonderful trick. If you don't know the length ahead of time, and the file is random access only on the reading end,
    at the end of the record write the distance back at which the record starts. That way you can read from the end of the file backward.
    [code]Code tags[/code] are essential for python code and Makefiles!
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2017
    Posts
    16
    Rep Power
    0
    Hi,
    Thanks for your response. I have tried with below codes. Its working. But same issues, i need to optimize the below codes.
    Code:
    int main()
    {
    convertDecimalToBinary(u32 temperature, u32 v_x1_u32, u32 v_pressure_u32);
    }
    
    int convertDecimalToBinary(u32 temperature, u32 v_x1_u32, u32 v_pressure_u32)
    {
    
    	u32  array1[5]={0};
    	u32  array2[5]={0};
    	u32  array3[18]={0};
    	u32 merge[102]={0};
    	int *ai;
    	int b,i,j,k,l,m,n,p,q,size;
    
       for (i = 0; i<5; i++)
       {
        array1[4 - i] = (temperature >> i) & 1;
    
       }
       for (j = 0; j<5; j++)
      {
    
        array2[4 - j] = (v_x1_u32 >> j) & 1;
    
      }
    
    for (m = 0; m<18; m++)
       {
        array3[17 - m] = (v_pressure_u32 >> m) & 1;
    
       }
    
    
       for(k=2,size=0;k<7,size<5;k++,size++)
    	{
    		merge[k]=array1[size];
    		//printf("%d",merge[k]);
    	}
    
    	merge[8]=1;
    	for(i=0, l=9; l<14 && i<5; i++, l++)
    	{
    		merge[l]=array2[i];
    
    	}
    	//for(m=0;m<14;m++)
         //printf("%d",merge[m]);
    merge[15]=1;
    
    for(p=0, q=17; q<35 && p<18; p++, q++)
    	{
    		merge[q]=array3[p];
    
    	}
    ai= (int *)(merge);
    
    printf("\n");
    	for(b=0;b<102;b++)
    	{
         printf("%d",merge[b]);
    	}
    	printf("\n");
    	Encoder(ai);
    }
    
    int Encoder(u32 *array)
    {
    SOME Codes
    .....
    }
    How to optimize the convertDecimalToBinary Function without changing its output.. STILL lot of FOR LOOPS.
  8. #5
  9. Contributing User
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Aug 2011
    Posts
    5,888
    Rep Power
    509
    You've taken temperature, a 32 bit binary value, and stored it using a single bit in each of five 32 bit words. How is that useful?
    Your convertDecimalToBinary function is supposed to return an int but has no return value. Are you relying on gcc extensions to the c language?
    The
    u32 array1[5]={0};
    u32 array2[5]={0};
    u32 array3[18]={0};
    u32 merge[102]={0};
    variables are all local. Setting their values has no useful effect outside of the function.

    main is supposed to return an integer. The main function needs a return statement or an exit(status) function call. Or are you relying on a gcc c language extension?

    Here's a program which compiles and runs. The output is
    0011010011100101000001111000011111100000000000000000000000000000000000000000000000000000000000000000 00
    Code:
    #include<stdio.h>
    
    #define u32 unsigned long
    
    int Encoder(u32*array) {
      return 0;
    }
    
    int convertDecimalToBinary(u32 temperature, u32 v_x1_u32, u32 v_pressure_u32) {
      u32 array1[5]={0};
      u32 array2[5]={0};
      u32 array3[18]={0};
      u32 merge[102]={0};
      u32*ai;
      int b,i,j,k,l,m,n,p,q,size;
    
      for (i = 0; i<5; i++)
        array1[4 - i] = (temperature >> i) & 1;
      for (j = 0; j<5; j++)
        array2[4 - j] = (v_x1_u32 >> j) & 1;
    
      for (m = 0; m<18; m++)
        array3[17 - m] = (v_pressure_u32 >> m) & 1;
      for(k=2,size=0;k<7,size<5;k++,size++) {
        merge[k]=array1[size];
        //printf("%d",merge[k]);
      }
      merge[8]=1;
      for(i=0, l=9; l<14 && i<5; i++, l++)
        merge[l]=array2[i];
      //for(m=0;m<14;m++)
      //printf("%d",merge[m]);
      merge[15]=1;
    
      for(p=0, q=17; q<35 && p<18; p++, q++)
        merge[q]=array3[p];
    
      ai = merge;
    
      putchar('\n');
      for(b=0;b<102;b++)
        printf("%lu",merge[b]);
      putchar('\n');
      Encoder(ai);
      return 0;
    }
    
    int main() {
      convertDecimalToBinary((u32)26, (u32)25, (u32)15423);
      return 0;
    }
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo