Yeah, in Objective-C, preferably, how do I turn 4 bytes into a 32-bit big-endian number?
Offline
As a general solution to whatever you're solving, you could try Google Protocol Buffers. It's C++, but I think you can use C++ code in Xcode (assuming that's what you're using). Just a thought.
Otherwise, I think you want to use the bitshift operator; something like this:
int a, b, c, d; int big = a << 24 + b << 16 + c << 8 + d;
(I'm afraid I'm not so good on C types -- maybe it should be unsigned char or long or something somewhere. Hope that helps, anyhow
)
Last edited by blob8108 (2012-01-30 05:58:23)
Offline
So given ints a, b, c, d; how do I construct a 32-bit big-endian? Just give me a formula...
Offline
I think you meant this:
int big = a << 24 | b << 16 | c << 8 | d;
Offline
bobbybee wrote:
I think you meant this:
Code:
int big = a << 24 | b << 16 | c << 8 | d;
What do "|" and "<<" do/mean?
Last edited by Hardmath123 (2012-01-30 08:18:26)
Offline
Hardmath123 wrote:
So given ints a, b, c, d; how do I construct a 32-bit big-endian? Just give me a formula...
I'm sorry; I'm not sure I understand what you're asking. Are you trying to write that 32-bit int to a file?
Offline
blob8108 wrote:
Hardmath123 wrote:
So given ints a, b, c, d; how do I construct a 32-bit big-endian? Just give me a formula...
I'm sorry; I'm not sure I understand what you're asking. Are you trying to write that 32-bit int to a file?
No. I'm sorry, I haven't explained it very well, have I?
I'm programming in Objective-C. I want to read the data of a BYOB or Scratch project. I have the code to obtain the required byte array. Now, in the Scratch file format, the bytes 11 to 14 dictate a 32-bit big-endian number which tells me how many bytes long the header is (I'm aiming to isolate some data from the header, mainly the thumbnail image). So, I ask again, how do I obtain that number. As a digression, I would really appreciate a bunch of notes on byte arrays. Is far as I know, they're arrays of integers constructed from data.
bobbybee, for some Scratch projects, your way returns a negative integer...
Offline
Make it of type unsigned int, rather than int. (it basically says big numbers are not negative)
Offline
bobbybee wrote:
Make it of type unsigned int, rather than int. (it basically says big numbers are not negative)
Ah... thank you sooo much!
Offline
Hardmath123 wrote:
As a digression, I would really appreciate a bunch of notes on byte arrays. Is far as I know, they're arrays of integers constructed from data.
A byte array is just that -- an array of bytes. (I think translates to an array of char in C.) The Scratch file format uses them to store strings (as characters can be translated to 8-bit integers using an encoding such as ASCII or UT8). Their format consists of a 4-byte (32-bit) integer describing the length of the string, followed by that many bytes content.
See the little wiki I made if you need more detail -- it may help.
Image data is stored in Forms, according to nXIII -- see his post (which I copied to the wiki).
Anyhow, I'm glad you solved your immediate problem.
Offline
Oh, and blob8108, I think I'm going to work on my task: loading in a Scratch file
Offline