4

I wonder if I could ask for some advice regarding some work I'm currently doing.

I am working from a STANAG document which quotes the following:

ID numbers shall be formed as 4-byte numbers. The first (most significant) byte shall be the standard NATO country code for the object in question. Valid country codes shall range from 0 to 99 decimal... Country code 255 (hexadecimal FF) shall be reserved.

It then goes on to detail the three other bytes. In the specification, the ID is given the type Integer 4, where Integer n is a signed integer and n is 1,2, or 4 bytes.

My question, and I acknowledge this could be considered an ignorant question and I apologise, is that an integer is, as we know, 32 bits/4 bytes. How can "the first byte" be, for example, 99, when 99 is an integer?

I would greatly appreciate any clarification here.

Leif Gruenwoldt
  • 13,561
  • 5
  • 60
  • 64
Myn
  • 691
  • 4
  • 9
  • 19

3 Answers3

4

An integer is normally 4 bytes. But if you store a small number like 99, the other three bytes store 8x 0-value bits. The spec is asking for you to use one integer storage (4-bytes) to store 4 different smaller numbers within its bytes.

The easiest way is probably to use a toInt function on an array of 4 bytes, e.g. (there is no byte[] length checking nor is this function tested - it is illustrative only)

public static final int toInt(byte[] b) 
{
    int l = 0;
    l |= b[0] & 0xFF;
    l <<= 8;
    l |= b[1] & 0xFF;
    l <<= 8;
    l |= b[2] & 0xFF;
    l <<= 8;
    l |= b[3] & 0xFF;
    return l;
}

byte[] bytes = new byte[] {99, 4, 9, 0};
int i = toInt(bytes, 0);

32-bits of an int

11110101   00000100   00001001   00000000
^byte      ^byte      ^byte      ^byte

Each block of 8 bits in the int is enough to "encode"/"store" a smaller number. So an int can be used to mash together 4 smaller numbers.

RichardTheKiwi
  • 105,798
  • 26
  • 196
  • 262
  • Ah..I beleive I've misinterpreted the "Integer 4" part of the specification - as you and T.J. say, it must mean it as being an integer number rather than an integer type. I was looking at it with my brain in programming mode, it suddenly makes a lot more sense! Thank you very much for the code, that sounds like exactly what I'm after, though I won't require the toInt function as I beleive it's to be used as a byte array anyway. Thank you very much to you both, you've just cleared up what's been bugging me all morning! – Myn Jan 26 '11 at 12:08
  • As a followup question, would the same work with a double or characters? So instead of a double "51.304215" or a String "T-321", a byte array? – Myn Jan 26 '11 at 12:13
  • You can always coerce and bit shift into any storage.. since they end up as bytes. – RichardTheKiwi Jan 26 '11 at 12:24
  • What's the "toInt" function. Where does it come from? – Joeri Hendrickx Jan 26 '11 at 14:49
3

"99" is an integer number mathematically, but not necessarily an Integer or int from the Java perspective. The value 99 can be held by a short, for instance (which is short for "short integer"), which is a 16-bit data type, or in a byte, which is an 8-bit data type.

So basically, you'll want to look at their ID thing as a series of four byte values. Beware that Java's byte type is signed.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • Same comment as with cyberkiwi, if I could I would give two best answers, but thank you very much to you both, your explanations were exceptionally helpful. – Myn Jan 26 '11 at 12:10
1

An integer (outside computing) just means any value without a decimal which includes 2, 99, -5, 34134, 427391471244211, etc. In computing terms, an integer is (traditionally) a 32-bit number that can contains any value that will fit in it. Each byte (8 bits) of that (computing) integer value is also an individual (numerical) integer between 0 and 255.

Deanna
  • 23,876
  • 7
  • 71
  • 156