DADiSP Worksheet Functions > Function Categories > Data Type Conversion > TYPE2BYTE

 

TYPE2BYTE

Purpose:

Converts a series from a specific data type to a raw stream of bytes.

Syntax:

TYPE2BYTE(series, datatype, byteswap)

series

-

A series, the values to convert.

datatype

-

Optional. An integer, the binary data type for the output values described by either its macro name or integer code. Defaults to DOUBLE. Valid arguments are:

 

Name

Code

Data Type

Range

INT8

SBYTE

1

8-bit Signed Byte

-128 to +127

UINT8

UBYTE

BYTE

2

8-bit Unsigned Byte

0 to 255

INT16

SINT

3

16-bit (2-byte) Signed Integer

-32768 to +32767

UINT16

UINT

4

16-bit (2-byte) Unsigned Integer

0 to 65535

INT32

LONG

5

32-bit (4-byte) Signed Integer

-2,147,483,648 to +2,147,483,647

FLOAT

6

32-bit (4-byte) Floating Point

-1037 to +1038

DOUBLE

7

64-bit (8-byte) Floating Point

-10-307 to +10308

UINT32

ULONG

8

32-bit (4-byte) Unsigned Integer

0 to 4,294,967,295

INT64

9

64-bit (8-byte) Signed Integer

-9,223,372,036,854,775,808 to +9,223,372,036,854,775,807

UINT64

10

64-bit (8-byte) Unsigned Integer

0 to 18,446,744,073,709,551,615

 

1000 + N

N-byte Signed Integer

-28N-1 to 28N-1-1

 

2000 + N

N-byte Unsigned Integer

0 to 28N-1

 

byteswap

-

Optional. An integer, the byte order flag:

0

:

preserve byte order (default)

1

:

swap byte order

Returns:

A series.

Example:

W1: grand(10, 1)

W2: type2byte(w1)

W3: byte2type(W2)

 

W2 contains a 80 point series where every 8 values is the byte representation of the corresponding double value in W1. W3 converts the byte values of W2 to double precision and recovers the original data.

Example:

W1: grand(10, 1)

W2: type2byte(w1, float)

W3: byte2type(W2, float)

 

Same as above except the data is converted to 4 byte floats. The series in W2 is 40 bytes long where every 4 values is the byte representation of the corresponding float value in W1. W3 converts the byte values of W2 to float precision and recovers the original data.

Example:

W1: type2byte(int(100 * gsin(100, 1/100, 2)), 1003);

writeb("sbin.dat", ubyte, W1);

W2: readb("sbin.dat", ubyte);

W3: byte2type(W2, 1003);

 

A sinewave of 3 byte signed integers is created by TYPE2BYTE and written to the file sbin.dat. The data is read as a stream of unsigned bytes and BYTE2TYPE converts the data to the original 3 byte signed integer format. The special datatype value of 1000 + N specifies an N byte signed integer value.

Remarks:

TYPE2BYTE uses CASTBYTE to convert the input byte stream.

 

TYPE2BYTE also supports N byte integer format to read signed or unsigned multiple bytes as integer values. A datatype value of 1000 + N specifies N byte signed integer values and 2000 + N specifies N byte unsigned integer values. For example:

 

1003

 

implies a 3 byte signed integer values.

 

2005

 

implies a 5 byte unsigned integer values.

 

 

See BYTE2TYPE to convert a byte stream to a series of a specific type.

See Also:

CASTBYTE

BYTE2TYPE

READB

WRITEB