Converting an array of byte to an array int

This is a discussion on Converting an array of byte to an array int within the C Programming forums, part of the General Programming Boards category; Dear all, I am using sockets in c in order to send an array of integers from the server and ...

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    4

    Converting an array of byte to an array int

    Dear all,

    I am using sockets in c in order to send an array of integers from the server and then receive it at the client side. Now at the client side as far as i know i will receive it as an array of bytes.

    Therefore I need a way to convert this array of bytes to an array of int. Can anyone suggest any solution please?

    Thank you very much,
    Chris

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,424
    When you send array of ints - you also end it as array of bytes.

    So question is - are client and server located on the same OS or different OSes.

    It means - do you need to handle big/small endian issues or not?

    If you sending from/to same OS like from XP to XP - no worries - just read input into array of ints direclty

    If not... It should be taken into account also while sending array
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751
    if you don't know what your doing you might want to read up on network programming in general.

  4. #4
    Registered User
    Join Date
    Mar 2008
    Posts
    4
    Dear all,

    thanks for your replies.

    My query however is the following...

    I have read all my data in an array of bytes. I have no problem in doing that. The problem is that now I need to convert that array of bytes to an array of integers therefore in some way I want to tae 4 bytes at a time in order to represent each int.

    Can someone suggest a solutions?

    Thanks a lot,
    Chris Farrugia

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Depends on whether you care about the byte-ordering or not. If you have equivalent hardware at both ends (e.g. two PC's ) you certainly OK to ignore endianness. If you have different hardware (e.g. Sun Sparc connected to a PC), then you will need to make sure that the bytes appear in the right order when you receive the data.

    If byteorder is not important, then something like this will most likely work:
    Code:
       int *intptr;
       char *bytedata;
    
       bytedata = received_data_array;
    
       if (((unsigned long) bytedata) & (sizeof(int)-1))
           error("Must align data correctly first");
       intptr = (int *)bytedata;
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Registered User
    Join Date
    Mar 2008
    Posts
    4
    Thank you very much for your reply,

    If the platforms are different do you suggest make use of the htonl function? If yes can you suggest how to use it?

    Thanks,
    Chris

  7. #7
    Registered User
    Join Date
    Mar 2008
    Posts
    4
    Excuse me for my previous reply, I mean ntohl not htonl.

    Thanks

  8. #8
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,424
    Actually ntohl and htonl do exactly the same...

    But generally - if you can have different platforms on sender and received you have to call

    ntohl before you send an int to convert it to byte array
    and htonl after you receive to convert byte array back to int

    These calls have to be made for each int to be sent/receved
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. NEED HELP READING FILE and PRINTING
    By geoffr0 in forum C Programming
    Replies: 4
    Last Post: 04-16-2009, 05:26 PM
  2. can some one please tell me the cause of the error ?
    By broli86 in forum C Programming
    Replies: 8
    Last Post: 06-26-2008, 08:36 PM
  3. newbie needs help with code
    By compudude86 in forum C Programming
    Replies: 6
    Last Post: 07-23-2006, 08:54 PM
  4. Working with random like dice
    By SebastionV3 in forum C++ Programming
    Replies: 10
    Last Post: 05-26-2006, 09:16 PM
  5. Replies: 4
    Last Post: 11-23-2003, 06:15 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21