Thread: Simple Byte Array problem

  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
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    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
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  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
    28,413
    Wouldn't you be using htonl, htons, ntohl, ntohs for such endianness conversions?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    28,413
    hmm... I recommend reading Beej's Guide to Network Programming.

    *Thread moved to Networking/Device Communication forum*
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    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
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

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, 11:35 AM
  2. simple array of char array problem
    By cloudy in forum C++ Programming
    Replies: 5
    Last Post: 09-10-2006, 12:04 PM
  3. Replies: 5
    Last Post: 12-03-2003, 05:47 PM
  4. Replies: 6
    Last Post: 10-21-2003, 09:57 PM
  5. problem: reading user input into an array
    By alpha561 in forum C Programming
    Replies: 13
    Last Post: 05-24-2002, 07:23 PM