Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

how to convert 4bytes into 4 seperate bytes

Status
Not open for further replies.

cool.man

Full Member level 6
Joined
May 29, 2008
Messages
323
Helped
42
Reputation
84
Reaction score
29
Trophy points
1,308
Location
Earth
Activity points
3,307
Hi ,
I have a 4byte data.i want to break this 4byte data into 4 (1byte) data and store it in four variables.how can i do that in C.
Thanks
 

Hi,

Can you tell me which microcontroller you use ?

Btw, you can use the mask 000F,00F0 then a shift by 8, 0F00 with a shift by 16 and F000 with shift by 24 byte.
 

Hi,
I am using pic16f877a and ccs c compiler (4.020).
 

As AdvaRes already told, you can use byte shifting

Code:
typedef char OneByteData;

OneByteData data1, data2, data3, data4; 
data1 = FourByteData & 0x000F;
data2 = (FourByteData >> 8) & 0x000F;
data3 = (FourByteData >> 16) & 0x000F;
data4 = (FourByteData >> 24) & 0x000F;
 

... or, if you are using 8-bit byte rather than 4-bit ;-) :
Code:
typedef unsigned char OneByteData;

OneByteData data1, data2, data3, data4; 
data1 = FourByteData & 0xFF;
data2 = (FourByteData >> 8) & 0xFF;
data3 = (FourByteData >> 16) & 0xFF;
data4 = (FourByteData >> 24) & 0xFF;
JW
 

Right :)
 

This works:

Code:
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>

int main(void)
{
   int a = 0x45abcdef, i;
   char b[] = {3, 4, 5, 6};
   char *p;
 
   p = (char *)&a;  // typecast the 32 bit integer address to a char pointer
   for(i = 0; i <= 3; i++)
   {
      b[i] = p[i];       
   }

   printf("%x  %x  %x  %x \n\n", b[0],b[1],b[2],b[3]); 
   getch();   
   return 0;
}

The output gives:

ffffffef ffffffcd ffffffab 45

I used DevCPP.

Added after 5 minutes:

If you wanted to simply access the data you could just make a union of one 32 bit variable unioned with 4 8 bit variables. Of course changing any variable changes the other part of the union which may not be what you want.
 

Thanks all for helping me out.
 

hi ,

u can also use the UNION structure of the standard C language. u will get it very easily. just study about UNION from a standard C text book


this is the classic situation for which UNION is ther in standard C language..............


arnab/vu2bpw
 

Hi

Here is a union example:

1) Declare a union
union {
unsigned long MyNumber; //Long is 4 byte length
unsigned char Byte[4];
}ByteConvert; //Can be done in the H file


2) create an instance in RAM
ByteConvert Test; //Only in the .C file



3) Use the union to convert long to 4 byte

Test.MyNumber = 1234;
//access unsigned char via the union
Test.Byte[0] // will hold the number 4;
Test.Byte[1] // will hold the number 3;
Test.Byte[2] // will hold the number 2;
Test.Byte[3] // will hold the number 1;
 

hi bobcat,


u gave the example---------------


Test.MyNumber = 1234;
//access unsigned char via the union
Test.Byte[0] // will hold the number 4;
Test.Byte[1] // will hold the number 3;
Test.Byte[2] // will hold the number 2;
Test.Byte[3] // will hold the number 1;




i dont think this is right, just check it please........................................


union holds byte values, but for the int 1234 it wont break it to 1,2,3,4 rather we will get it the first byte, second, third and forth byte of int value 1234 in the byte wise.............................


arnab/vu2bpw
 

Hi arnab

Yes . you are right

It was a misteak it should be as you say

Test.Byte[0] // will hold the first bye;
Test.Byte[1] // will hold the 2nd bye;;
Test.Byte[2] // will hold the 3rd bye;;
Test.Byte[3] // will hold the 4th bye;;

Thank you

Bobi
 

4bytes = INT 0xFFFFFFFF as max
funct


Code:
header .. main.h
#include <math.h>

INT indata; // integer for holding a 32bit value look at the compiler to see how to define 32bit vals
BYTE outdata[4]; // array of values assigned by convert function
BYTE convertedbyte; // converted spot byte value...
INT t;//range 0-3 or whatever....
BOOL error; // error bit
////////////////////////////////
// : CASE LOGIC DEFINITIONS : //
////////////////////////////////
#case
#ifndef TRUE
#define TRUE 1
#endif
#ifndef FALSE
#define FALSE 0
#endif
#ifndef YES
#define YES 1
#endif
#ifndef NO
#define NO 0
#endif
#ifndef HIGH
#define HIGH 1
#endif
#ifndef LOW
#define LOW 0
#endif
#ifndef ON
#define ON 1
#endif
#ifndef OFF
#define OFF 0
#endif
#ifndef UP
#define UP 1
#endif
#ifndef DOWN
#define DOWN 0
#endif
#ifndef UCHAR
#define UCHAR char
#endif
#ifndef UINT
#define UINT long
#endif
#ifndef BIT
#define BIT short
#endif
#ifndef SCHAR
#define SCHAR signed int
#endif
#ifndef SINT
#define SINT signed long
#endif
#ifndef FLOAT
#define FLOAT float
#endif
/////////////////////////////////
/////////////////////////////////



convertclass.cpp 

include main.h
t,indata=0;
outdata[0]=outdata[1]=outdata[2]=outdata[3]=0x00;

convert (BYTE *outdata[],INT indata)
{
*outdata[0]=(BYTE) indata;
*outdata[1]=(BYTE) (indata<<8);
*outdata[2]=(BYTE) (indata<<16);
*outdata[3]=(BYTE) (indata<<24);
return(TRUE);
}



to call use in the main class etc

{set indata here and t value{bin bas}}


.....main

Code:
error= FALSE;
t=0;
indata=0;  //null all variables
converterbyte=0x00;
...set t val...
..set indata;

if ((error)!=(convert (&outdata[t],indata)))
{
convertedbyte= outdata[t];
// do with the outdata byte etc what you want here
// outdata[t] returned
}
else
{ supply error or recompute}




handy for byte blit funct & = data at address {ie & is a pointer...}
this routine can also return a null or 1 for success or not etc..

this funct will give you any byte indata in four you want by t val
or you can call it a few times to give all four values
this is handy if you want three different bytes from three different inval {the int you supply}

if you dont want to use loop arrays to convert this is cool
or you could use
Code:
convertedbyte=(convert(&outdata[t],indata));
with mostly all c engines
yes no true false etc arent supplied
you must # define them first

ie

#define YEMAN =1;
#define NAMAN=0;
in the header allong with all class wide and compile wide variables

you must also add the class to your project...
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top