6

This a legacy system so please don't tell me what I'm trying to do is wrong. It has to be this way.

I have a byte array where date is put in a sequential manner. This is fine until I want to store a value above 255. To do this I need to use 2 bytes as a 16 bit value.

So I need to convert an int into two chars and then the two chars back into an int.

One program is in C and the other in Java and they communicate through a byte array.

This seems to me like a problem that should have been fixed a long time ago so I'm wondering if there is a function for doing this in the Java and C libraries. If not, is there an easy way of making the conversion?

mmking
  • 1,564
  • 4
  • 26
  • 36
Skeith
  • 2,512
  • 5
  • 35
  • 57

3 Answers3

2

As far as I can see you are just doing base 256 maths.

I have not programmed c for a little while so disregard any old terminology;

int original_number = 300; // A number over 255
char higher_char, lower_char; // the 2 numbers/chars you are trying to get
higher_char = original_number / 256;
lower_char = original_number % 256;

and then just print higher_char and lower_char as normal.

[Edit

And to convert back.

char higher_char, lower_char; // The two chars you have in order
int number_to_restore; // The original number you are trying to get back to
number_to_restore =  higher_char * 256 + lower_char;

]

So using binary operators this becomes:

int mask = 0xFF;
int original_number = 300; // A number over 255
char higher_char, lower_char; // the 2 numbers/chars you are trying to get
higher_char = (original_number >> 8) & mask;
lower_char = original_number & mask;

and the restore

char higher_char, lower_char; // The two chars you have in order
int number_to_restore; // The original number you are trying to get back to
number_to_restore =  (higher_char << 8) | lower_char;
Rewind
  • 2,554
  • 3
  • 30
  • 56
  • 2
    While `/ 256` and `% 256` are correct, the code would be more obvious when written with bit operations, i.e. `>> 8` and `& 0xFF` respectively. – user1252434 Dec 19 '14 at 17:22
  • Yep, just putting that edit in as you were writing your comment. Just trying to make it explicit. – Rewind Dec 19 '14 at 17:27
  • I downvoted because: a two byte integer can have the approx values -32k ... +32k which means 7 string characters. However, if the purpose is to take a 2 byte integer and place it, as is, into a a byte array, then the above would work. However, still need to take endian-ness into account. – user3629249 Dec 19 '14 at 20:21
1

You can also use BigInteger.

    byte[] bytes = BigInteger.valueOf(x).toByteArray();
    int y = new BigInteger(bytes).intValue();
OldCurmudgeon
  • 64,482
  • 16
  • 119
  • 213
0

This is how i would do it in C

uint16_t value;
uint8_t  chars[2];

value = 280;

/* to bytes array */

chars[0] = value & 0xFF;
chars[1] = (value >> CHAR_BIT) & 0xFF;

/* back */

value = chars[0] | (chars[1] << CHAR_BIT);
Iharob Al Asimi
  • 52,653
  • 6
  • 59
  • 97
  • Yes, 4 is wrong, it needs to be 8 (see my answer above). – Rewind Dec 19 '14 at 17:35
  • 2
    This assumes `CHAR_BIT == 8`, which is not guaranteed. It also uses signed shift, which may result in undefined behaviour. – EOF Dec 19 '14 at 18:09
  • @EOF it now doesn't, and of course you are totaly right, i didn't fix it for a downvote reversal, but for the site quality. Although it is hard to trust a downvoted answer :) – Iharob Al Asimi Dec 19 '14 at 20:59