LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-05-2005, 03:53 PM   #1
slzckboy
Member
 
Registered: May 2005
Location: uk - Reading
Distribution: slack 10.2 kde 3.4.2 kernel 2.6.15
Posts: 452

Rep: Reputation: 30
integer encoding & byte ordering


Ladies & gents,boys n girls..

basically I am reading tcp/ip sockets in C written by Micahel J donahoo & Kenneth L Calvert..

dunno if anyones come across it..its a good book.

anyway.

There is a section on constructing messages,in particular encoding integer values for transmission pg 26 sect 3.1

it gives two values 17,998,720 and 47,034,615 which represent two figures that need to be
transmitted using tcp as two "type int" values.

it states ..
on a big-endian" machine the two vales would form the following byte sequence.

1 18 163 128 2 205 176 247
with four bytes representing the first value 17,998,720 and the last four bytes represnting the second value ,47,034,615.

I don't get it :0(

it goes on to say that in a little endian machine the stream would look like ..

128 163 18 1 247 176 205 2.

I understand that on differnt architectures the MSB and LSB have the same address.!?!!

What i dont get is how 1 18 163 128 2 205 176 247 or 128 163 18 1 247 176 205 2 can be obtained from 17,998,720 & 47,034,615 ???????



.Pls help wasted my whole day at work thinking about this instad of doing wot they pay me to do ;0)

ta.
 
Old 07-05-2005, 04:10 PM   #2
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.3, Oracle Linux, Mint, Tribblix
Posts: 9,742

Rep: Reputation: 455Reputation: 455Reputation: 455Reputation: 455Reputation: 455
2 205 176 247 are double byte values represented in decimal
they can equally be displayed as
02 CD B0 F7

They indeed represent 47034615:

(2*256*256*256)+(205*256*256)+(176*256)+247 = 47034615

reverse the ordering for little endians.
 
Old 07-05-2005, 04:17 PM   #3
slzckboy
Member
 
Registered: May 2005
Location: uk - Reading
Distribution: slack 10.2 kde 3.4.2 kernel 2.6.15
Posts: 452

Original Poster
Rep: Reputation: 30
Thank you.

It is greatly appreciated.
 
Old 07-06-2005, 03:37 AM   #4
slzckboy
Member
 
Registered: May 2005
Location: uk - Reading
Distribution: slack 10.2 kde 3.4.2 kernel 2.6.15
Posts: 452

Original Poster
Rep: Reputation: 30
question.

is there an algorithm that can be applied on say 47034615 that will spit out 2 205 176 247 ?
 
Old 07-06-2005, 04:10 AM   #5
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.3, Oracle Linux, Mint, Tribblix
Posts: 9,742

Rep: Reputation: 455Reputation: 455Reputation: 455Reputation: 455Reputation: 455
Definitely, divide it by 256, print the remainder. Divide the integer result of this division by 256 again, print the remainder ...
Repeat the whole operation as long as the result is not null.
 
Old 07-06-2005, 04:31 AM   #6
slzckboy
Member
 
Registered: May 2005
Location: uk - Reading
Distribution: slack 10.2 kde 3.4.2 kernel 2.6.15
Posts: 452

Original Poster
Rep: Reputation: 30
1st step divide the whole number ((47034615)/256)...
?
 
Old 07-06-2005, 05:22 AM   #7
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.3, Oracle Linux, Mint, Tribblix
Posts: 9,742

Rep: Reputation: 455Reputation: 455Reputation: 455Reputation: 455Reputation: 455
Hmm, you have trouble understanding what a integer division and its remainder are ?

http://en.wikipedia.org/wiki/Remainder
 
Old 07-06-2005, 03:23 PM   #8
slzckboy
Member
 
Registered: May 2005
Location: uk - Reading
Distribution: slack 10.2 kde 3.4.2 kernel 2.6.15
Posts: 452

Original Poster
Rep: Reputation: 30
I don't believe so...?!!!

I have used the modulus operator in C code before i.e (to find out if a number is odd or even ).

its just that when i apply the formula u gave me,the numbers don't come out right.
 
Old 07-06-2005, 03:44 PM   #9
slzckboy
Member
 
Registered: May 2005
Location: uk - Reading
Distribution: slack 10.2 kde 3.4.2 kernel 2.6.15
Posts: 452

Original Poster
Rep: Reputation: 30
ok found this following the links you sent ..

Consider as an example the computation of gcd/HCF(120, 23) with Euclid's algorithm:

120 / 23 = 5 r 5
23 / 5 = 4 r 3
5 / 3 = 1 r 2
3 / 2 = 1 r 1
2 / 1 = 2 r 0

cheers.
 
Old 07-06-2005, 05:28 PM   #10
aluser
Member
 
Registered: Mar 2004
Location: Massachusetts
Distribution: Debian
Posts: 557

Rep: Reputation: 42
if you get to write your "algorithm" in C or another programming language, you can cheat:

Code:
/* assume int is 4 bytes: this is not portable to older archs */

int a = 47034615;
int i;
for (i = 3; i >= 0; --i)
    printf("%d ", (a >> (i * 8)) & 0xFF);
printf("\n");
or trickier, and relying on the fact that my machine is little endian:

Code:
int a = 47034615;
int i;
for (i = 3; i >= 0; --i)
    printf("%d ", (int)((unsigned char*)&a)[i]);
printf("\n");
In both cases, you're looping through the 4 bytes of the integer a and printing the decimal value of each one. The first snippet works no matter what your endianness, because the >> operator knows to do the right thing. The second snippet will give a different (wrong) answer on a big endian machine because I hacked around C's arithmetic by treating a as a character array.
 
Old 07-06-2005, 07:03 PM   #11
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.3, Oracle Linux, Mint, Tribblix
Posts: 9,742

Rep: Reputation: 455Reputation: 455Reputation: 455Reputation: 455Reputation: 455
Here is a sample code that is using the algorithm I suggested.
Code:
main()
{
  int i;
  for(i=47034615; i>0; i/=256)
    printf("%d ",i%256);
  printf("\n");
}
Aluser code is equally right, but isn't based on the same algorithm, so risk to confuse the OP.

Just to demonstrate there are many algorithms to solve a problem, here's one gratuitously complicated:
Code:
  {
    int i, n=47034615;
    char buf[8];
    sprintf(buf, "%08x", n);
    for(i=0; i<4; i++)
    {
      int j;
      static char byte[3];
      strncpy(byte, &buf[i*2], 2);
      sscanf(byte, "%02x", &j);
      printf("%d ",j);
    }
    printf("\n");
  }
 
Old 07-06-2005, 07:42 PM   #12
aluser
Member
 
Registered: Mar 2004
Location: Massachusetts
Distribution: Debian
Posts: 557

Rep: Reputation: 42
actually, the output of the /= and % code needs to be reversed: it prints the bytes in reverse order as posted.

It might be interesting to note that (a >> (i * 8)) & 0xFF from my snippet is equivalent to (a / 256^i) % 256, which starts to look similar to jlliagre's. (that's ^ as exponentiation, not xor.) I've approached the problem by thinking about what bits I want out of a, while jlliagre has probably thought in terms of changing one base to another (in some sense, base 10 to base 256).

It would be moderately informative to check what code gcc produces under optimizations with the two methods: perhaps division by a constant power of two gets turned into a right shift anyway. -- similar could be done for mod and &. I've no idea what the relative costs of those are.
 
Old 07-06-2005, 07:59 PM   #13
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.3, Oracle Linux, Mint, Tribblix
Posts: 9,742

Rep: Reputation: 455Reputation: 455Reputation: 455Reputation: 455Reputation: 455
Quote:
actually, the output of the /= and % code needs to be reversed: it prints the bytes in reverse order as posted.
The bytes are indeed in reverse order, I was just showing an implementation of my proposed algorithm.
Reversing the output was left as an exercise to the reader ...
 
Old 07-06-2005, 08:32 PM   #14
exvor
Senior Member
 
Registered: Jul 2004
Location: Phoenix, Arizona
Distribution: Gentoo, LFS, Debian,Ubuntu
Posts: 1,537

Rep: Reputation: 87
Wow i created a little function for this question but im getting totaly diffrent ansers lol. Its probably my math tho im sure others are better then i am at it

heres what i did
Code:
 
int var;
int ans;

scanf("%d ",var);

 ans = ( var % 256);  <-- this gives the remainder 
 var = (var / 256);
of course this is not the whole code just the important parts i put this in a while loop that continues untill var is less then 0.



Last edited by exvor; 07-06-2005 at 08:36 PM.
 
Old 07-07-2005, 01:29 AM   #15
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.3, Oracle Linux, Mint, Tribblix
Posts: 9,742

Rep: Reputation: 455Reputation: 455Reputation: 455Reputation: 455Reputation: 455
Except I'm processing a constant, that's exactly what my first sample code is doing.

The problem is you messed the interactive part:
Code:
scanf("%d ",var);
should be:
Code:
scanf("%d ",&var);
Don't overlook your compiler error messages !

Last edited by jlliagre; 07-07-2005 at 02:04 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Linux Distro Live CD for Video & Audio Encoding UltimateLinux Linux - Distributions 1 08-26-2005 12:22 PM
Ordering Linux CDs LinuxNewUser Linux - Newbie 13 04-25-2005 12:54 AM
how to convert long integer value to byte array appas Programming 11 11-23-2004 01:56 PM
backup byte-for-byte axion0917 Linux - Software 2 12-11-2003 05:01 PM
DVD rip & encoding to avi tsitras Linux - Software 0 05-07-2003 08:17 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:44 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration