Encoding and Decoding XDR data with Python

0
Xdrlib module Python

Python can deal with any type of data easily, including the XDR data as well. Python provides the xdrlib module, which is a built-in module that helps in encoding and decoding the XDR data.

What is XDR data?

XDR is a file encoding system, just like the utf and another encoding system. XDR stands for "eXternal Data Representation" and is a standard for describing and encoding the data just like other encoding schemes. By the term XDR data, we refer to the data that is encoded with the help of the XDR encoding scheme. XDR is used by the Remote Procedure Call (RPC) and Network File System (NFS) to describe the format of the data. Check this article by IBM about XDR encoding.

Why do we still use the XDR?

XDR is still widely used and the reason is that it can be very helpful in transferring data between different computer architectures. The main reason for the XDR popularity is that it uses implicit typing while the other protocol such as X.409 uses explicit typing. To learn more about the Importance of XDR data Encoding Please check this article.

Python Support for XDR

Python Programming provides a built-in module xdrlib, that helps us work with the XDR data more easily. We can encode and decode the XDR data by using certain methods available in the xdrlib package. THe xdrlib python module provides two classes, one is for the packing variables into the XDR representational and the other class can be used for unpacking data from the SDR representation. I will tell you more about them in a second.

Encoding different XDR data types

XDR has different data types that are most common in programming language and support all popular data types that are used by the programming languages. There is a list of XDR Data Types by IBM if you want to read more about the topic.

See the following code for How we can encode different types to XDR.

import  xdrlib

# creating the packer object
packerobj = xdrlib.Packer()

# Encoding floating value to xdr
packerobj.pack_float(3.4)

#encoding value with double precision
packerobj.pack_double(3.34)

#Encoding bytes data 
packerobj.pack_bytes(b'this is bytes')

# encding list to xdr format
packerobj.pack_list([1,2,3],packerobj.pack_int)

Decoding data types from XDR

Once we have the object of the XDR.Unpacker() we can easily get the required values from the XDR. We can do the same for the above code. in the above code, we have used the XDR.Packer() class to encode the values.

import  xdrlib

# creating the Unpacker object
unpackerobj = xdrlib.Packer()

# decoding floating value to xdr
unpackerobj.unpack_float(3.4)

#decoding value with double precision
unpackerobj.unpack_double(3.34)

#decoding bytes data 
unpackerobj.unpack_bytes(b'this is bytes')

# decding list to xdr format
unpackerobj.pack_list([1,2,3],unpackerobj.pack_int)

Resources to Learn More about the XDR:

Related Posts:

Post a Comment

0Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.
Post a Comment (0)

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Learn More
Accept !