R
R
Ruslan Jakushev2017-10-17 00:13:27
C++ / C#
Ruslan Jakushev, 2017-10-17 00:13:27

How to split short int (from 0 to 65535) into two char numbers (from zero to 255)?

In theory, the maximum number of short int is 2 ^ 16, that is, it can be divided into two char (2 ^ 8), but I still do not understand how to do this. Healthelp

Answer the question

In order to leave comments, you need to log in

2 answer(s)
0
0xD34F, 2017-10-17
@lrusifikator

bit operations. Something like this:

short int x = 0x6543;
char a = x & 0xff;
char b = (x & 0xff00) >> 8;

R
res2001, 2017-10-17
@res2001

It is possible through union:

typedef union {
 short int x;
 char c[2];
} si_u;
...
si_u var;
var.x = 0x6543;
printf("char0 = %c\tchar1 = %c\n", "var.c[0], var.c[1]);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question