byte vector to unsigned integer

Imported from previous forum

Hi,
How to convert a byte vector into an unsigned integer?

Per spec:
8.4 Converting from Byte Vector to String
The byte vector is represented as a sequence of an even number of hexadecimal digits [0-9a-f]. Each pair is a hexadecimal number representing a byte in the vector.

In my case, the sequence number of the message is in byte vector
of size 4. How to convert it into an unsigned integer?

Thanks for your help

Charles,

the section you are referring to is not directly applicable.

The short answer is: It depends on how the encoder has encoded from the unsigned integer to the byte vector.

A slightly longer answer is: The byte order in the byte vector may be most significant byte first which is equivalent to “network byte order” or “big-endian order”, or the least significant byte first which is sometimes referred to as “little-endian” byte order. Big-endian order is used in most Internet protocols (such as IP, TCP etc). Little-endian order is the native order of how quantities are stored in memory on x86 (Intel, AMD) machines.

Example: Let’s assume you have an unsigned integer value of 4711. This corresponds to 0x1267 (hex). Depending on your byte order, you may encode in two different ways:

04 00 00 12 67 // big-endian
04 67 12 00 00 // little-endian

Let’s further assume that you use a C-like language and that your byte vector data is located in a unsigned char vector d [4]. Your code to convert could be something like:

(d[0] << 24) | (d[1] << 16) | (d[2] << 8) | d [3] // big-endian
(d[3] << 24) | (d[2] << 16) | (d[1] << 8) | d [0] // little-endian

(there are other coding tricks if you want to optimize but this will yield the result you are after)

I suggest you check with the encoding party re used byte order.

Hope this helps,
Rolf

Hi,
How to convert a byte vector into an unsigned integer?

Per spec:
8.4 Converting from Byte Vector to String
The byte vector is represented as a sequence of an even number of hexadecimal digits [0-9a-f]. Each pair is a hexadecimal number representing a byte in the vector.

In my case, the sequence number of the message is in byte vector
of size 4. How to convert it into an unsigned integer?

Thanks for your help

Hm,

the byte vector size is SBIT-coded so the examples should read:
84 00 00 12 67 // big-endian
84 67 12 00 00 // little-endian

/Rolf

Charles,

the section you are referring to is not directly applicable.

The short answer is: It depends on how the encoder has encoded from the unsigned integer to the byte vector.

A slightly longer answer is: The byte order in the byte vector may be most significant byte first which is equivalent to “network byte order” or “big-endian order”, or the least significant byte first which is sometimes referred to as “little-endian” byte order. Big-endian order is used in most Internet protocols (such as IP, TCP etc). Little-endian order is the native order of how quantities are stored in memory on x86 (Intel, AMD) machines.

Example: Let’s assume you have an unsigned integer value of 4711. This corresponds to 0x1267 (hex). Depending on your byte order, you may encode in two different ways:

04 00 00 12 67 // big-endian
04 67 12 00 00 // little-endian

Let’s further assume that you use a C-like language and that your byte vector data is located in a unsigned char vector d [4]. Your code to convert could be something like:

(d[0] << 24) | (d[1] << 16) | (d[2] << 8) | d [3] // big-endian
(d[3] << 24) | (d[2] << 16) | (d[1] << 8) | d [0] // little-endian

(there are other coding tricks if you want to optimize but this will yield the result you are after)

I suggest you check with the encoding party re used byte order.

Hope this helps,
Rolf

Hi,
How to convert a byte vector into an unsigned integer?

Per spec:
8.4 Converting from Byte Vector to String
The byte vector is represented as a sequence of an even number of hexadecimal digits [0-9a-f]. Each pair is a hexadecimal number representing a byte in the vector.

In my case, the sequence number of the message is in byte vector
of size 4. How to convert it into an unsigned integer?

Thanks for your help

Thanks Rolf.
The exchange has used network byte order.
Your help is very much appreciated.

Charles

Hm,

the byte vector size is SBIT-coded so the examples should read:
84 00 00 12 67 // big-endian
84 67 12 00 00 // little-endian

/Rolf

Charles,

the section you are referring to is not directly applicable.

The short answer is: It depends on how the encoder has encoded from the unsigned integer to the byte vector.

A slightly longer answer is: The byte order in the byte vector may be most significant byte first which is equivalent to “network byte order” or “big-endian order”, or the least significant byte first which is sometimes referred to as “little-endian” byte order. Big-endian order is used in most Internet protocols (such as IP, TCP etc). Little-endian order is the native order of how quantities are stored in memory on x86 (Intel, AMD) machines.

Example: Let’s assume you have an unsigned integer value of 4711. This corresponds to 0x1267 (hex). Depending on your byte order, you may encode in two different ways:

04 00 00 12 67 // big-endian
04 67 12 00 00 // little-endian

Let’s further assume that you use a C-like language and that your byte vector data is located in a unsigned char vector d [4]. Your code to convert could be something like:

(d[0] << 24) | (d[1] << 16) | (d[2] << 8) | d [3] // big-endian
(d[3] << 24) | (d[2] << 16) | (d[1] << 8) | d [0] // little-endian

(there are other coding tricks if you want to optimize but this will yield the result you are after)

I suggest you check with the encoding party re used byte order.

Hope this helps,
Rolf

Hi,
How to convert a byte vector into an unsigned integer?

Per spec:
8.4 Converting from Byte Vector to String
The byte vector is represented as a sequence of an even number of hexadecimal digits [0-9a-f]. Each pair is a hexadecimal number representing a byte in the vector.

In my case, the sequence number of the message is in byte vector
of size 4. How to convert it into an unsigned integer?

Thanks for your help