This is a read-only archive of the old Scratch 1.x Forums.
Try searching the current Scratch discussion forums.

#1 2012-01-30 05:29:15

Hardmath123
Scratcher
Registered: 2010-02-19
Posts: 1000+

Constructing a 32-bit big-endian number from bytes?

Yeah, in Objective-C, preferably, how do I turn 4 bytes into a 32-bit big-endian number?


Hardmaths-MacBook-Pro:~ Hardmath$ sudo make $(whoami) a sandwich

Offline

 

#2 2012-01-30 05:57:59

blob8108
Scratcher
Registered: 2007-06-25
Posts: 1000+

Re: Constructing a 32-bit big-endian number from bytes?

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.  smile

Otherwise, I think you want to use the bitshift operator; something like this:

Code:

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  smile  )

Last edited by blob8108 (2012-01-30 05:58:23)


Things I've made: kurt | scratchblocks2 | this cake

Offline

 

#3 2012-01-30 06:23:33

Hardmath123
Scratcher
Registered: 2010-02-19
Posts: 1000+

Re: Constructing a 32-bit big-endian number from bytes?

So given ints a, b, c, d; how do I construct a 32-bit big-endian? Just give me a formula...


Hardmaths-MacBook-Pro:~ Hardmath$ sudo make $(whoami) a sandwich

Offline

 

#4 2012-01-30 07:19:19

bobbybee
Scratcher
Registered: 2009-10-18
Posts: 1000+

Re: Constructing a 32-bit big-endian number from bytes?

I think you meant this:

Code:

int big = a << 24  |  b << 16  |  c << 8  | d;

I support the Free Software Foundation. Protect our digital rights!

Offline

 

#5 2012-01-30 08:18:17

Hardmath123
Scratcher
Registered: 2010-02-19
Posts: 1000+

Re: Constructing a 32-bit big-endian number from bytes?

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)


Hardmaths-MacBook-Pro:~ Hardmath$ sudo make $(whoami) a sandwich

Offline

 

#6 2012-01-30 10:19:57

blob8108
Scratcher
Registered: 2007-06-25
Posts: 1000+

Re: Constructing a 32-bit big-endian number from bytes?

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?


Things I've made: kurt | scratchblocks2 | this cake

Offline

 

#7 2012-02-01 06:10:35

Hardmath123
Scratcher
Registered: 2010-02-19
Posts: 1000+

Re: Constructing a 32-bit big-endian number from bytes?

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...  hmm


Hardmaths-MacBook-Pro:~ Hardmath$ sudo make $(whoami) a sandwich

Offline

 

#8 2012-02-01 06:19:12

bobbybee
Scratcher
Registered: 2009-10-18
Posts: 1000+

Re: Constructing a 32-bit big-endian number from bytes?

Make it of type unsigned int, rather than int. (it basically says big numbers are not negative)


I support the Free Software Foundation. Protect our digital rights!

Offline

 

#9 2012-02-01 07:05:23

Hardmath123
Scratcher
Registered: 2010-02-19
Posts: 1000+

Re: Constructing a 32-bit big-endian number from bytes?

bobbybee wrote:

Make it of type unsigned int, rather than int. (it basically says big numbers are not negative)

Ah... thank you sooo much!


Hardmaths-MacBook-Pro:~ Hardmath$ sudo make $(whoami) a sandwich

Offline

 

#10 2012-02-01 11:46:49

blob8108
Scratcher
Registered: 2007-06-25
Posts: 1000+

Re: Constructing a 32-bit big-endian number from bytes?

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.  smile  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.  smile


Things I've made: kurt | scratchblocks2 | this cake

Offline

 

#11 2012-02-01 13:48:51

bobbybee
Scratcher
Registered: 2009-10-18
Posts: 1000+

Re: Constructing a 32-bit big-endian number from bytes?

Oh, and blob8108, I think I'm going to work on my task: loading in a Scratch file  smile


I support the Free Software Foundation. Protect our digital rights!

Offline

 

Board footer