POV-Ray : Newsgroups : povray.advanced-users : write uint32_t Server Time
20 Apr 2024 02:44:42 EDT (-0400)
  write uint32_t (Message 6 to 15 of 15)  
<<< Previous 5 Messages Goto Initial 10 Messages
From: ingo
Subject: Re: write uint32_t
Date: 29 Mar 2021 13:25:35
Message: <XnsACFCC59E163CDseed7@news.povray.org>
in news:60620b36$1@news.povray.org Thorsten wrote:

> You can just compute the two's complement inverse before output, and
> you get what you want
> 

Sorry Thorsten, I realy don't understand. Can we do it by example.
I have a number, 4321 and it hast to written to a file as uint_32.


Ingo


Post a reply to this message

From: Tor Olav Kristensen
Subject: Re: write uint32_t
Date: 29 Mar 2021 13:35:00
Message: <web.60620e7f8cf934b8ae6f04a89db30a9@news.povray.org>
ingo <ing### [at] tagpovrayorg> wrote:
> The fine documentation states: "Note: Currently, unsigned 32-bit words are
> not supported."
>
> Guess what. I need those.
>
> Is there some trickery with 4 uint8' or 2 uint16le's to get the proper
> bytes in the proper place?

Hi Ingo

You can try this:

#version 3.7;

#declare AnInteger = 4091728320; // 0xf3e2d1c0

#declare R = AnInteger;
#declare Byte_0 = mod(R, 256);
#declare R = div(R, 256);
#declare Byte_1 = mod(R, 256);
#declare R = div(R, 256);
#declare Byte_2 = mod(R, 256);
#declare R = div(R, 256);
#declare Byte_3 = mod(R, 256);

#fopen SomeFile "LitteEndian.bin" write
#write (SomeFile, uint8 Byte_0)
#write (SomeFile, uint8 Byte_1)
#write (SomeFile, uint8 Byte_2)
#write (SomeFile, uint8 Byte_3)
#fclose SomeFile

#fopen SomeFile "BigEndian.bin" write
#write (SomeFile, uint8 Byte_3)
#write (SomeFile, uint8 Byte_2)
#write (SomeFile, uint8 Byte_1)
#write (SomeFile, uint8 Byte_0)
#fclose SomeFile

#error "Finished"

You can upload the resulting files to this web page: https://hexed.it
- and then inspect the results as both big endian and little endian integeres.

--
Tor Olav
http://subcube.com
https://github.com/t-o-k


Post a reply to this message

From: Bald Eagle
Subject: Re: write uint32_t
Date: 29 Mar 2021 13:45:00
Message: <web.606211058cf934b81f9dae3025979125@news.povray.org>
If you get any of that to work, I would like a very small working code snippet
to paste into my collection of Keyword Examples.

Computing complement inverses would be a nice trick to add in as well.

I'm sticking to the stuff I know how to do already, and saving the "need to
learn that" stuff until later/last.


Post a reply to this message

From: ingo
Subject: Re: write uint32_t
Date: 29 Mar 2021 13:45:38
Message: <XnsACFCC9042258Fseed7@news.povray.org>
in news:web.60620e7f8cf934b8ae6f04a89db30a9@news.povray.org Tor Olav
Kristensen wrote: 

> 
> #declare AnInteger = 4091728320; // 0xf3e2d1c0
> 
> #declare R = AnInteger;
> #declare Byte_0 = mod(R, 256);
> #declare R = div(R, 256);
> 
> [.....]
> 

Thank you Tor Olav!

Ingo


Post a reply to this message

From: Bald Eagle
Subject: Re: write uint32_t
Date: 29 Mar 2021 14:00:00
Message: <web.606214ce8cf934b81f9dae3025979125@news.povray.org>
"Tor Olav Kristensen" <tor### [at] TOBEREMOVEDgmailcom> wrote:

> You can try this:

Leave it to you to post a full code example before I even finished typing my
request  :D  (He's too fast for me, folks.)


That ought to give me some more interesting material for div and mod as well.
:)

Thanks for keeping the info flowing - there's always something new to learn.


Post a reply to this message

From: Tor Olav Kristensen
Subject: Re: write uint32_t
Date: 29 Mar 2021 16:45:00
Message: <web.60623c1a8cf934b8ae6f04a89db30a9@news.povray.org>
"Bald Eagle" <cre### [at] netscapenet> wrote:
> "Tor Olav Kristensen" <tor### [at] TOBEREMOVEDgmailcom> wrote:
>
> > You can try this:
>
> Leave it to you to post a full code example before I even finished typing my
> request  :D  (He's too fast for me, folks.)

Then perhaps I started before you ;)


> That ought to give me some more interesting material for div and mod as well.
> :)
>
> Thanks for keeping the info flowing - there's always something new to learn.

If you want to try to do something along the way Thorsten suggested, here's some
code to start with:

#version 3.7;

#declare UnsignedInteger = 4091728320; // 0xf3e2d1c0

#debug "\n\n"
#debug str(UnsignedInteger, 0, 0)
#debug "\n"

#declare NoOfBits = ceil(ln(UnsignedInteger)/ln(2));

#declare TheBits = array[NoOfBits];

#declare R = UnsignedInteger;
#for (I, 0, NoOfBits-1)
    #declare TheBits[I] = mod(R, 2);
    #declare R = div(R, 2);
#end // for

#declare N = 0;
#for (I, NoOfBits-1, 0, -1)
    #declare InvertedBit = 1 - TheBits[I];
    #declare N = 2*N + InvertedBit;
#end // for

#debug str(N, 0, 0) // 203238975 = 0x0C1D2E3F
#debug "\n\n"

#error "Finished"


If you get to a point where you need to mask away all other bits than the lower
32 your can do this:

#declare N = mod(N, pow(2, 32));

--
Tor Olav
http://subcube.com
https://github.com/t-o-k


Post a reply to this message

From: Thorsten
Subject: Re: write uint32_t
Date: 30 Mar 2021 03:04:52
Message: <6062cd94$1@news.povray.org>
On 29.03.2021 19:25, ingo wrote:
> in news:60620b36$1@news.povray.org Thorsten wrote:
> 
>> You can just compute the two's complement inverse before output, and
>> you get what you want
>>
> 
> Sorry Thorsten, I realy don't understand. Can we do it by example.
> I have a number, 4321 and it hast to written to a file as uint_32.

Unsigned is only relevant when dealing with numbers past 2**31, so you 
can just write it as signed.

Thorsten


Post a reply to this message

From: Thorsten
Subject: Re: write uint32_t
Date: 30 Mar 2021 03:24:32
Message: <6062d230$1@news.povray.org>
On 29.03.2021 19:40, Bald Eagle wrote:
> If you get any of that to work, I would like a very small working code snippet
> to paste into my collection of Keyword Examples.
> 
> Computing complement inverses would be a nice trick to add in as well.
> 
> I'm sticking to the stuff I know how to do already, and saving the "need to
> learn that" stuff until later/last.

It should be (untested):

#declare uint32 = function(x) { select(x-2147483648, x, x-4294967296) }

Thorsten


Post a reply to this message

From: ingo
Subject: Re: write uint32_t
Date: 30 Mar 2021 06:48:01
Message: <XnsACFD823568867seed7@news.povray.org>
in news:6062cd94$1@news.povray.org Thorsten wrote:

> Unsigned is only relevant when dealing with numbers past 2**31, so you 
> can just write it as signed.
> 

It's a field in a header file describing the data length in it. The 
maximum length can be up to 2^32. Hence the unint32 question regardless of 
the actual data length.

Thanks Thorsten,

Ingo


Post a reply to this message

From: Tor Olav Kristensen
Subject: Re: write uint32_t
Date: 30 Mar 2021 09:15:00
Message: <web.6063231a8cf934b8ae6f04a89db30a9@news.povray.org>
ingo <ing### [at] tagpovrayorg> wrote:
> in news:60620b36$1@news.povray.org Thorsten wrote:
>
> > You can just compute the two's complement inverse before output, and
> > you get what you want
> >
>
> Sorry Thorsten, I realy don't understand. Can we do it by example.
> I have a number, 4321 and it hast to written to a file as uint_32.

Here's how you can do it with sint32le and sint32be. This does it like Thorsten
indicated in his last post.


#version 3.7;

// #declare UnsignedInteger = 0; // 0x00000000
// #declare UnsignedInteger = 1; // 0x00000001
// #declare UnsignedInteger = pow(2, 31) - 1; // 0x7FFFFFFF
// #declare UnsignedInteger = pow(2, 31); // 0x80000000
// #declare UnsignedInteger = pow(2, 31) + 1; // 0x80000001
// #declare UnsignedInteger = pow(2, 32) - 1; // 0xFFFFFFFF

#declare UnsignedInteger = 4321; // 0x000010E1
// #declare UnsignedInteger = 1059986700; // 0x3F2E1D0C
// #declare UnsignedInteger = 4091728320; // 0xF3E2D1C0

#if (UnsignedInteger < pow(2, 31))
    #declare N = UnsignedInteger;
#else
    #declare N = UnsignedInteger - pow(2, 32);
#end // if

#debug concat("\n\n", str(N, 0, 0), "\n\n")

#fopen SomeFile "LitteEndian32.bin" write
#write (SomeFile, sint32le N)
#fclose SomeFile

#fopen SomeFile "BigEndian32.bin" write
#write (SomeFile, sint32be N)
#fclose SomeFile

#error "Finished"


--
Tor Olav
http://subcube.com
https://github.com/t-o-k


Post a reply to this message

<<< Previous 5 Messages Goto Initial 10 Messages

Copyright 2003-2023 Persistence of Vision Raytracer Pty. Ltd.