How to access one byte of an integer?

For a given communication standard RTCM SC104 3.1, I need to split the data by bytes as a pair of 12-bit data segments. Therefore, for this message, I need to put the message type number in the first byte and half the second byte. Then I need to run an integer ID on half the second byte and continue the third byte. This kind of pattern continues until the end of the message, discarding other integers in 20-bit, 5-bit and other sizes, significantly reducing the number 0, which usually fills the end of the integer MSB values.

I did not see a clear definition, but I assume that it should go out in the network byte order, so before copying the bits I will have to cancel my integers in memory. I'm still new to cpp, and I wonder how can I get the individual bytes that make up an integer in memory? If I can access bytes, then I could use bitwise or break bits from 2 bytes into one for the message.

Here begins the assembly of the message before adding data:

//build message 1002  gps 00111110 1010
    char buf1002[BUFFERSIZE];
    buf1002[0] = 0x3E; //00111110
    buf1002[1] = 0xA0; //10100000
//ref station id 12 bits of 16
//ex unsigned short m = 1100;
//m would be byte2 00000100 byte1 01001100
//buf1002[1] would be 10100100
//buf1002[2] would be 01001100
//reverse bit order for network after building? 

The reference station will be from unsigned short, so an integer of 2 bytes. So, how do I start reading one byte? Am I starting with a memory location pointer? If so, then what?

Any help would be greatly appreciated.

+5
source share
4 answers

, : :

typedef union packet {
    struct {
        unsigned int msgno  : 12; // creates a 12 bit wide integral field
        unsigned int msgtype  : 12;
        unsigned int somefield2  : 3;
        unsigned int somefield3  : 5;
    } fields;

    unsigned char asbytes[4];
} packet;

...

packet p;
// fill it, by using the field side of the union
p.fields.msgno = 16;
p.fields.msgtype = 12;
p.fields.somefield2 = 6;
// and retrieving them as regular bytes
unsigned char abyte = p.asbytes[0];
abyte = p.asbytes[1];
// and so on

- : , . (, ), , .

+4

, 00001000 00000000 11000000 00110000 ( , ), int a:

int     a;
int     single_byte;

a = the_number;

​​,

int     get_byte_in_integer(int byte_number, int integer);

, :

single_byte = get_byte_in_integer(1, a); // That would give you the 11000000 byte

int    get_byte_in_integer(int byte_number, int integer)
{
      return (integer >> (byte_number << 3)) & 0xff);
}

, , .

+2

, , :

bit = number & (1 << x);

x .

: , ?

+1
source

You can resort to customizing it in bytes, for example:

const unsigned short s(1);
const char* const bytes(reinterpret_cast<const char*>(&s));

const char NaturalOrder[sizeof(s)] = {bytes[0],bytes[1]};
const char EndianFlipped[sizeof(s)] = {bytes[1],bytes[0]};

(But also see dmckee's comment regarding the natural size of inline elements and consider using fixed-width types).

0
source

All Articles