1. phix - int_to_bytes

is this behavior wanted?

sequence s = int_to_bytes(1) 
?s 
s = int_to_bytes(9) 
?s 

gives:

{1,0,0,0} 
"\t\0\0\0" 

if you do that for 11 and 111 you get

{11,0,0,0} 
"o\0\0\0" 

if you do that for 1111 and 11111 you get

{87,4,0,0} 
"g+\0\0" 

richard

new topic     » topic index » view message » categorize

2. Re: phix - int_to_bytes

Pretty much, yes, it is intentional.

I half knew the answer to this, but I wanted to confirm, so for educational purposes here's what I did: ran p -d test and examined list.asm:

;     2 ?s  
    mov ecx,4                             ;#00426054: 271 04000000               uv 02 00  1  17       
    mov edx,125                           ;#00426059: 272 7D000000               vu 04 00  1  17       
    call #00430633 (:%opFrame) (print)    ;#0042605E: 350 D0A50000               v  00 00  1  18       
    mov [ebp] (fn), dword 1               ;#00426063: 307105 00 01000000         uv 00 20  1  19       
    mov ecx,[#004028FC] (s)               ;#0042606A: 213015 FC284000            vu 02 00  1  19       
    mov [ebp-4] (x),ecx                   ;#00426070: 211115 FC                  uv 00 22  1  20       
    add dword[ebx+ecx*4-8],1              ;#00426073: 203104213 F8 01            u  00 0A  3  22    *02* 
    mov [ebp+28] (retaddr),#00426084      ;#00426078: 307105 1C 84604200         vu 00 20  1  24       
    jmp #0042616A (code:print)            ;#0042607F: 351 E6000000               v  00 00  1  25       
        ^^^^^^^^^ 
    mov eax,1                             ;#00426084: 270 01000000               uv 01 00  1  26       
    mov edx,10                            ;#00426089: 272 0A000000               vu 04 00  1  26       
    call #0042C13F (:%opPuts)             ;#0042608E: 350 AC600000               v  00 00  1  27       

A quick search for that #0042616A (further down in that list.asm) led me to:

;   140 global procedure print(integer fn, object x, integer maxlen=-1) 
    mov ecx,[ebp-8] (maxlen)              ;#0042616A: 213115 F8                  uv 02 20  1   1       

and pressing <Ctrl Q> (in edita/edix) showed it was (as expected) in builtins\VM\psprintN.e, so I went to look at that.

There I found (by this stage I had pretty much fully remembered everything) the following:

global procedure print(integer fn, object x, integer maxlen=-1) 
-- Print a string representation of any data object. 
-- Alternative: see ppp.e (pp/ppOpt/ppEx). 
    puts(fn,sprint(x,maxlen)) 
end procedure 
... 
    elsif string(x) then 
        s = allascii(x) 
        if string(s) then return s end if 
    end if 

So now I can answer fully. ? is shorthand for print() [followed by puts(1,'\n')] and print() uses the function allascii() to decide whether strings really are strings. In the examples you gave, 1, 11, and 4 are clearly not characters so you get a sequence representation, whereas (by chance) 9 ('\t'), 111 ('o'), and 11111 (=#2B67=43,103='+','g'[big endian]) are all character-like, so it shows the value as a string.

If you like you can force the issue, by ensuring s is not a string before passing it to ?/print():

function to_seq(sequence s) 
    s &= -1         -- force string -> dword-sequence 
    s = s[1..$-1]   -- discard that -1 we just added 
    return s 
end function 

and that way get

s = int_to_bytes(1)         ?to_seq(s)      -- {1,0,0,0} 
s = int_to_bytes(9)         ?to_seq(s)      -- {9,0,0,0} 
s = int_to_bytes(11)        ?to_seq(s)      -- {11,0,0,0} 
s = int_to_bytes(111)       ?to_seq(s)      -- {111,0,0,0} 
s = int_to_bytes(1111)      ?to_seq(s)      -- {87,4,0,0} 
s = int_to_bytes(11111)     ?to_seq(s)      -- {103,43,0,0} 

Pete

new topic     » goto parent     » topic index » view message » categorize

3. Re: phix - int_to_bytes

thanks pete,

now i can try to read integers with that an turn bytes maybe to satisfy external functions.

richard

new topic     » goto parent     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu