How to know endianness of a machine


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users How to know endianness of a machine
# 1  
Old 09-13-2012
How to know endianness of a machine

hi

I have a ubuntu machine and want to know the endianness of the system....
How would i get to know.....

The information of my machine is:

Code:
uname -a

42-Ubuntu SMP Mon Apr 11 03:31:50 UTC 201
1 i686 i686 i386 GNU/Linux

and

Code:
lsb_release -a

No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 11.04
Release: 11.04
Codename: natty


I have done it using C Program......but dont want to know using C Program.......
Want some linux command to know endianness of a machine

Plzz Help...
Thanks in advance....
# 2  
Old 09-13-2012
You are on an x86 machine, which is little-endian by definition.
# 3  
Old 09-13-2012
If you have perl:
Code:
perl -MConfig -e 'print "$Config{byteorder}\n";'

Unfortunately I don't have a big-endian machine to test it with anymore, one of the last holdouts got replaced by a more modern mac...
This User Gave Thanks to Corona688 For This Post:
# 4  
Old 09-13-2012
Google a lil' bit and found this thread.
Modified the suggestion a little bit:
Code:
echo I | tr -d [:space:] | od -to2 | awk 'FNR==1{ print substr($2,6,1)}'

'0' for Big.
'1' for Little.
These 3 Users Gave Thanks to vgersh99 For This Post:
# 5  
Old 09-13-2012
Interesting suggestion, it appears to work here.

I suggest putting single-quotes around '[:space:]' in case you have a weird-named file or folder in your directory(in my case, c) -- your shell might glob it like mine did! Smilie
# 6  
Old 09-13-2012
I agree. Interesting. However, the technique could be further improved and simplified:
Code:
printf '\1' | od -dAn

Little Endian: 1
Big Endian: 256

A simple, [ 1 -eq ... ] integer comparison can be used to test.

printf can portably avoid printing newlines, making tr unnecessary. It can also directly specify a byte value, removing the dependency between a character and its encoding.

To simplify the output, the input offset is suppressed with -An.


The linked thread mentions that the approach fails on some systems, but the behavior on which it depends has been in the POSIX od manual for at least 15 years:

Quote:
The byte order used when interpreting numeric values is implementation-defined, but shall correspond to the order in which a constant of the corresponding type is stored in memory on the system.
In my opinion, it's reasonable to expect it to work with modern systems (even when the definition of "modern" is loosely applied)

Regards,
Alister

Last edited by alister; 09-13-2012 at 03:37 PM..
This User Gave Thanks to alister For This Post:
# 7  
Old 09-13-2012
We still some some Sparc systems, so I gave it a try...

First on Linux:
Code:
$ uname -is
Linux x86_64
$ perl -MConfig -e 'print "$Config{byteorder}\n";'
12345678
$ echo I | tr -d [:space:] | od -to2 | awk 'FNR==1{ print substr($2,6,1)}'
1
$ printf '\1' | od -dAn
     1
$

Now on Solaris:
Code:
$
$ uname -is
SunOS SUNW,Sun-Blade-1000
$ perl -MConfig -e 'print "$Config{byteorder}\n";'
87654321
$
$ echo I | tr -d [:space:] | od -to2 | awk 'FNR==1{ print substr($2,6,1)}'
$
$
$ echo I | tr -d [:space:] | od -to2 | nawk 'FNR==1{ print substr($2,6,1)}'
0
$ printf '\1' | od -dAn
         00256

$

I was surprised at first that the second one gave me trouble. Then I remembered that famous Solaris awk vs nawk thing and got it to work. My favorite is the perl line.
These 2 Users Gave Thanks to Perderabo For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. AIX

IBM TDS/SDS (LDAP) - can I mix endianness among servers in an instance ?

I'd like to add some x/linux-based servers to my current AIX-based TDS/SDS server community. Reading the Fine Install Guide (rtfig ?) I believe this may be covered by the section "Upgrade an instance of a previous version to a different computer" i.e. I'm going to install latest/greatest SDS on a... (4 Replies)
Discussion started by: maraixadm
4 Replies

2. Shell Programming and Scripting

Help with fetching the data from remote machine from my jumpbox(local machine)

Team, Presently i am running a script from my local box(i.e jumpbox) to all the remote machines.Basically fetching basic queries like pwd,mkdir,touch etc and i am able to successfully fetch it from my local machine.But when i want to check certain database related queries like the dbstat... (20 Replies)
Discussion started by: whizkidash
20 Replies

3. Red Hat

iptables applied in local machine, can't ssh remote machine after chain changed to DROP

I want to SSH to 192.168.1.15 Server from my machine, my ip was 192.168.1.99 Source Destination was UP, with IP 192.168.1.15. This is LAN Network there are 30 Machine's Connected to the network and working fine, I'm Playing around the local machine's because I need to apply the same rules in... (2 Replies)
Discussion started by: babinlonston
2 Replies

4. Linux

Unable to connect to Server machine from a client machine using ftp service

Hi, Could you please help me with the below issue.. I'm running RHEL6 OS on both server (192.168.0.10) and client machines (192.168.0.1). I'm trying to connect to server from the client machine using ftp service. I have installed vsftpd daemon on both the machines. I'm getting... (4 Replies)
Discussion started by: raosr020
4 Replies

5. UNIX for Advanced & Expert Users

FTP While transfering files to local machine to remote machine

Hi Am using unix Ksh Am getting the problem while transferring zero size files through the script . When i transfer zero size files from local machine to remote machine manually i can able to do it . My question its beause of zero size files am not able to transfer through script ? or its... (2 Replies)
Discussion started by: Venkatesh1
2 Replies

6. Shell Programming and Scripting

How to transfer files from unix machine to local machine using shell script?

Hi All.. Am new to Unix!! Am creating a shell script in which a scenario is like i have transfer the output file from unix machine (Server) to local directory (Windows xp). And also i have to transfer the input file from the local directory to Unix machine (Server) Any help from you... (1 Reply)
Discussion started by: vidhyaS
1 Replies

7. Shell Programming and Scripting

shell script to copy files frm a linux machine to a windows machine using SCP

I need a shell script to copy files frm a linux machine to a windows machine using SCP. The files keeps changing day-to-day. I have to copy the latest file to the windows machine frm the linux machine. for example :In Linux, On July 20, the file name will be 20.txt and it should be copied to... (3 Replies)
Discussion started by: nithin6034
3 Replies

8. Solaris

Solaris 5.8 - BinaryFile - Endianness

Dear Users, How do I account for endian-ness while reading a Binary File generated from Solaris 5.8 (Sparc) on a Windows XP (x86) system. I just know that its a Binary File. I have no knowledge about the native datatype of the Binary File. (20 Replies)
Discussion started by: angshuman_ag
20 Replies

9. Red Hat

To find the LATEST file from a dir on REMOTE machine and SCP to local machine?

Hi All, URGENT - Please help me form a scipt for this: I need the LATEST file from a dir on REMOTE machine to be SCP'd to a dir on local machine. (and I need to execute this from local server) I know that the below cmd is used to find the LATEST file from a dir. But this command is not... (3 Replies)
Discussion started by: me_ub
3 Replies

10. Solaris

Best possible communication mechanism between a Solaris machine and a windows machine

hi, I have some windows client machines which require a signal to be sent by a Solaris machine( SunOS 5.6) when ever a particular event occurs on that Solaris machine. What are possible communication mechanisms by which i can do this. the constraints are > the windows machines have to... (7 Replies)
Discussion started by: Krsh
7 Replies
Login or Register to Ask a Question