Simple Byte Array problem

This is a discussion on Simple Byte Array problem within the Networking/Device Communication forums, part of the General Programming Boards category; Hi, it's been 10 years since I have had to deal with bytes. I'm a little rusty please help... I ...

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    9

    Simple Byte Array problem

    Hi, it's been 10 years since I have had to deal with bytes. I'm a little rusty please help...

    I have to fetch contents of a url (easy part), but the result is a byte array which I have to convert for use. The byte array contains int's & strings

    1) Even though the byte array was created on an online machine of unknown type running unknown software, are the strings still null terminated like c?

    2) as far as I'm aware int's are of variable byte length, so if I asked the company what size their int byte lengths are, would that sound a stupid question?

    Thanks!

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Quote Originally Posted by OldGit View Post
    Hi, it's been 10 years since I have had to deal with bytes. I'm a little rusty please help...

    I have to fetch contents of a url (easy part), but the result is a byte array which I have to convert for use. The byte array contains int's & strings

    1) Even though the byte array was created on an online machine of unknown type running unknown software, are the strings still null terminated like c?

    2) as far as I'm aware int's are of variable byte length, so if I asked the company what size their int byte lengths are, would that sound a stupid question?

    Thanks!
    when you "fetch" something - do you know the size of it?
    then you can nul-terminate it manually

    about ints - are they represented as binary or as strings as well?

    in the first case you need to know how many bytes are used and the endiannel as well

    in the second case - you do not need it, just convert string to int using one of available methods
    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
    Join Date
    Feb 2009
    Posts
    9
    Quote Originally Posted by vart View Post
    when you "fetch" something - do you know the size of it?
    then you can nul-terminate it manually

    about ints - are they represented as binary or as strings as well?

    in the first case you need to know how many bytes are used and the endiannel as well

    in the second case - you do not need it, just convert string to int using one of available methods
    The order is..
    int-string-string-int-int
    encoded as utf-8

    I know in java you can read the strings with a convenience method such as..
    myString = readUTF(myBinary);

    ...Ah I think i have had a breakthrough, I have been imagining the byte array with each byte in its own array position such as...(psuedo)..lets say an int has two bytes...

    myArray[0] = IntByteOne
    myArray[1] = IntByteTwo
    myArray[2] = stringByteOne
    myArray[3] = stringByteTwo
    myArray[4] = stringByteThree
    myArray[5] = stringByteFour etc

    So in my head i thought i had to know the length of bytes an int was on the sending machine. and test for null termination string by iterating through EACH element of the array.


    But, I believe that may be wrong, am I right in saying the structure is more like this...

    myArray[0] = IntByteOne, IntByteTwo
    myArray[1] = stringByteOne,stringByteTwo, stringByteThree, stringByteFour

    ...in which case i do not need to know the length of int or string, as each datatype exists on its own index?

  4. #4
    Registered User
    Join Date
    Feb 2009
    Posts
    9
    Quote Originally Posted by vart View Post
    in the first case you need to know how many bytes are used and the endiannel as well
    The machine its coming off is running .net is that of a standard endian type?

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,144
    Wouldn't you be using htonl, htons, ntohl, ntohs for such endianness conversions?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Feb 2009
    Posts
    9
    Quote Originally Posted by laserlight View Post
    Wouldn't you be using htonl, htons, ntohl, ntohs for such endianness conversions?
    So with those functions you just mentioned, are you saying that the guys sending the byte array with .net would have used those functions to convert datatypes to (from microsofts site) "TCP/IP network byte order (which is big-endian)"


    So i would be expecting big endian?

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,144
    hmm... I recommend reading Beej's Guide to Network Programming.

    *Thread moved to Networking/Device Communication forum*
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Feb 2009
    Posts
    9
    Quote Originally Posted by OldGit View Post
    The order is..
    int-string-string-int-int
    encoded as utf-8

    I know in java you can read the strings with a convenience method such as..
    myString = readUTF(myBinary);

    ...Ah I think i have had a breakthrough, I have been imagining the byte array with each byte in its own array position such as...(psuedo)..lets say an int has two bytes...

    myArray[0] = IntByteOne
    myArray[1] = IntByteTwo
    myArray[2] = stringByteOne
    myArray[3] = stringByteTwo
    myArray[4] = stringByteThree
    myArray[5] = stringByteFour etc

    So in my head i thought i had to know the length of bytes an int was on the sending machine. and test for null termination string by iterating through EACH element of the array.


    But, I believe that may be wrong, am I right in saying the structure is more like this...

    myArray[0] = IntByteOne, IntByteTwo
    myArray[1] = stringByteOne,stringByteTwo, stringByteThree, stringByteFour

    ...in which case i do not need to know the length of int or string, as each datatype exists on its own index?
    Was I right with this new found perception of the structure of a 'byte array' ?

  9. #9
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    if myArray is bytearray
    myArray[0] is a byte, how it can store more than one byte - I do not know

    PS. Net uses 32-bit ints
    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. Array problem
    By TomBoyRacer in forum C++ Programming
    Replies: 3
    Last Post: 04-08-2007, 12:35 PM
  2. simple array of char array problem
    By cloudy in forum C++ Programming
    Replies: 5
    Last Post: 09-10-2006, 01:04 PM
  3. Replies: 5
    Last Post: 12-03-2003, 05:47 PM
  4. Replies: 6
    Last Post: 10-21-2003, 10:57 PM
  5. problem: reading user input into an array
    By alpha561 in forum C Programming
    Replies: 13
    Last Post: 05-24-2002, 08:23 PM

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