Re: binary to decimal conversion

new topic     » goto parent     » topic index » view thread      » older message » newer message

> From: Grape_ Vine_

> I too have seen that. It does not work just the way i want it to
> Id does as long as i supply the binary by hand.
> I have a 128byte buffer i need changed from
> 8 bit binary to 2 bit binary
> I tried sequence slicing but it keeps
> giving me the old number

This isn't completely clear, but is this what you mean:

original buffer dec = { 255, 128 }
=>
original buffer bin = { 11111111, 10101010 }
=>
new buffer bin = { 11, 11, 11, 11, 10, 10, 10, 10 }
=>
new buffer dec = { 3, 3, 3, 3, 2, 2, 2, 2 }


if so:

-- begin tested code

include machine.e

-- sequence buf is a sequence of integers [0-255]
function convert_buffer( sequence buf )
  sequence new

  for i = 1 to length( buf ) do
    -- put into binary/sequence form
    buf[i] = int_to_bits( buf[i] )
  end for

  new = {}
  for i = 1 to length( buf ) do
    -- slice up the bytes into 2 bit lengths and add to new
    new &= { buf[i][1..2], buf[i][3..4], buf[i][5..6], buf[i][7..8] }
  end for

  buf = repeat(0, length(new) )
  for i = 1 to length( new ) do
    -- convert back to integers from binary/sequence form
    buf[i] = bits_to_int( new[i] )
  end for

  return buf
end function

-- end code

There are probably ways that this could be sped up, but I think this gets
the job done.  I'm not certain how you're getting the buffer, but this
should be easily modifiable.

Matt

new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu