Forum > General

how to convert BitConverter.GetBytes for lazarus

(1/4) > >>

ahmetnurideniz:
Hi,
i have c# code and i am convert it lazarus.
c# program has this lines. how can convert it lazarus?

--- Code: ---          foreach (string adr in addresses)
            {
                UInt16 offset = UInt16.Parse(adr.Trim());
                ms.Write(BitConverter.GetBytes(offset), 0, 2);
            }
            byte[] data = ms.ToArray();
            m_state = State.MQuery;
            m_client.GetStream().Write(data, 0, data.Length);

--- End code ---

bobc:
I guess ms is a memory stream, so the code is converting a list of uint16 to an array of bytes. The fun thing is that BitConverter.GetBytes does not seem to specify the byte order, but on an intel machine it will be little endian.

So assuming that little endian is the required byte order, do something like

--- Code: ---ms.Write (offset and $ff);
ms.Write (offset shr 8);

--- End code ---

ahmetnurideniz:

--- Quote from: bobc on August 25, 2010, 10:19:03 pm ---I guess ms is a memory stream, so the code is converting a list of uint16 to an array of bytes. The fun thing is that BitConverter.GetBytes does not seem to specify the byte order, but on an intel machine it will be little endian.

So assuming that little endian is the required byte order, do something like

--- Code: ---ms.Write (offset and $ff);
ms.Write (offset shr 8);

--- End code ---

--- End quote ---
ms.Write ? i write it lazarus. there is error message.
main.pas(58,3) Error: identifier not found: ms

bobc:
Sorry, I was assuming you have declared a memory stream


--- Code: ---var
  ms: Tmemorystream;
....
begin
  ms := TMemoryStream.Create;
...

--- End code ---
I don't know what the rest of your C# code does, it might be easier to create the array of bytes directly.

EDIT: I should have written "ms.WriteByte (..)" of course.

ahmetnurideniz:
Hi,
when i read my data i see  carecter. how i can return it normal carecter?
how can i convert  binary to ASCII (string) ?

Navigation

[0] Message Index

[#] Next page

Go to full version