Recent

Author Topic: how to convert BitConverter.GetBytes for lazarus  (Read 18464 times)

ahmetnurideniz

  • Full Member
  • ***
  • Posts: 110
  • As you sow, you shall reap.
    • Big Student Web Site
how to convert BitConverter.GetBytes for lazarus
« on: August 25, 2010, 09:50:23 pm »
Hi,
i have c# code and i am convert it lazarus.
c# program has this lines. how can convert it lazarus?
Code: [Select]
          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);

bobc

  • New Member
  • *
  • Posts: 41
Re: how to convert BitConverter.GetBytes for lazarus
« Reply #1 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: [Select]
ms.Write (offset and $ff);
ms.Write (offset shr 8);

ahmetnurideniz

  • Full Member
  • ***
  • Posts: 110
  • As you sow, you shall reap.
    • Big Student Web Site
Re: how to convert BitConverter.GetBytes for lazarus
« Reply #2 on: August 25, 2010, 10:52:02 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: [Select]
ms.Write (offset and $ff);
ms.Write (offset shr 8);
ms.Write ? i write it lazarus. there is error message.
main.pas(58,3) Error: identifier not found: ms


bobc

  • New Member
  • *
  • Posts: 41
Re: how to convert BitConverter.GetBytes for lazarus
« Reply #3 on: August 25, 2010, 11:40:13 pm »
Sorry, I was assuming you have declared a memory stream

Code: [Select]
var
  ms: Tmemorystream;
....
begin
  ms := TMemoryStream.Create;
...
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.
« Last Edit: August 26, 2010, 12:14:15 am by bobc »

ahmetnurideniz

  • Full Member
  • ***
  • Posts: 110
  • As you sow, you shall reap.
    • Big Student Web Site
Re: how to convert BitConverter.GetBytes for lazarus
« Reply #4 on: August 26, 2010, 12:34:27 am »
Hi,
when i read my data i see  carecter. how i can return it normal carecter?
how can i convert  binary to ASCII (string) ?
« Last Edit: August 26, 2010, 02:04:07 am by ahmetnurideniz »

bobc

  • New Member
  • *
  • Posts: 41
Re: how to convert BitConverter.GetBytes for lazarus
« Reply #5 on: August 26, 2010, 01:08:53 pm »
Quote
when i read my data i see  carecter. how i can return it normal carecter?
how can i convert  binary to ASCII (string) ?

There is not really enough detail in that question for me to answer. The "box" character is the standard Unicode symbol meaning that the requested character code has no representation in the current font. I don't know what you are trying to display, where or how.

Usually you can just cast a byte to a char to display ASCII, but if you are using Unicode it all gets more tricky.

ahmetnurideniz

  • Full Member
  • ***
  • Posts: 110
  • As you sow, you shall reap.
    • Big Student Web Site
Re: how to convert BitConverter.GetBytes for lazarus
« Reply #6 on: August 26, 2010, 02:05:57 pm »
Quote
when i read my data i see  carecter. how i can return it normal carecter?
how can i convert  binary to ASCII (string) ?

There is not really enough detail in that question for me to answer. The "box" character is the standard Unicode symbol meaning that the requested character code has no representation in the current font. I don't know what you are trying to display, where or how.

Usually you can just cast a byte to a char to display ASCII, but if you are using Unicode it all gets more tricky.
The deveice send me this data "\001\r\n\" i see it from wireshark. it send me binnary data. i need convert it string data. how can i do this?

Laksen

  • Hero Member
  • *****
  • Posts: 724
    • J-Software
Re: how to convert BitConverter.GetBytes for lazarus
« Reply #7 on: August 26, 2010, 04:05:49 pm »
What does \001

If it means 1 then you can't get it as ascii, since it's not a renderable character

ahmetnurideniz

  • Full Member
  • ***
  • Posts: 110
  • As you sow, you shall reap.
    • Big Student Web Site
Re: how to convert BitConverter.GetBytes for lazarus
« Reply #8 on: August 26, 2010, 04:38:58 pm »
What does \001

If it means 1 then you can't get it as ascii, since it's not a renderable character
i see this text when i look wireshark(Packet Sniffer)  "\001\r\n\". wireshark can convert it binary to string and i can see it.

i know data is binnary i need to convert it string.
how i can do this?

Laksen

  • Hero Member
  • *****
  • Posts: 724
    • J-Software
Re: how to convert BitConverter.GetBytes for lazarus
« Reply #9 on: August 26, 2010, 05:04:18 pm »
IntToHex?

bobc

  • New Member
  • *
  • Posts: 41
Re: how to convert BitConverter.GetBytes for lazarus
« Reply #10 on: August 26, 2010, 06:11:30 pm »
i see this text when i look wireshark(Packet Sniffer)  "\001\r\n\". wireshark can convert it binary to string and i can see it.

i know data is binnary i need to convert it string.
how i can do this?
Ok, I think I see what you mean. I usually have a function something like this:

Code: [Select]
function BuftoString (buf: pchar; len: integer): string;
var
  j : integer;
  b: byte;
begin
  result := '';
  for j := 0 to len-1 do
  begin
    b := buf[j];
    if (b>=32) and (b<=126) then
       result := result + chr(b)
    else
       result := result + '$'+IntToHex(b,2);
  end;
end;

ahmetnurideniz

  • Full Member
  • ***
  • Posts: 110
  • As you sow, you shall reap.
    • Big Student Web Site
Re: how to convert BitConverter.GetBytes for lazarus
« Reply #11 on: August 26, 2010, 09:48:59 pm »
Code: [Select]
    b := buf[j];


[/quote]
there is error message for this code error message şs
main.pas(269,13) Error: Incompatible types: got "Char" expected "Byte"

bobc

  • New Member
  • *
  • Posts: 41
Re: how to convert BitConverter.GetBytes for lazarus
« Reply #12 on: August 26, 2010, 10:32:51 pm »
try

b := ord(buf[j]);

Heh, now I got you debugging my code  :D

ahmetnurideniz

  • Full Member
  • ***
  • Posts: 110
  • As you sow, you shall reap.
    • Big Student Web Site
Re: how to convert BitConverter.GetBytes for lazarus
« Reply #13 on: August 27, 2010, 05:00:50 am »
Thanks for answers.

ahmetnurideniz

  • Full Member
  • ***
  • Posts: 110
  • As you sow, you shall reap.
    • Big Student Web Site
Re: how to convert BitConverter.GetBytes for lazarus
« Reply #14 on: August 27, 2010, 11:52:05 pm »
Thanks for answers.  i have new problem. BuftoString function. when i call this function i see "$01$0d$0a" how can i see normal carecter? it shold be "\001\r\n"

i find this function from net. but it is not convert normaly. i can see "....,..." after convert.
Code: [Select]
function TFrmMain.HexToStr(AStr: string): string;
var
I,len : Integer;
CharValue: Word;
Tmp:string;
s:char;
begin
  Tmp:='';
  len:=length(Astr);
  for i:=1 to len  do
  begin

    s:=  Astr[i];
    if s <> ' ' then Tmp:=Tmp+ string(s);
  end;
  Result := '';
  For I := 1 to Trunc(Length(Tmp)/2) do
  begin
    Result := Result + ' ';
    CharValue := TransChar(Tmp[2*I-1])*16 + TransChar(Tmp[2*I]);
    if (charvalue < 32) or (charvalue > 126)  then Result[I] := '.'   //???????
    else Result[I] := Char(CharValue);
  end;
 end;

function TFrmMain.TransChar(AChar: Char): Integer;
begin
  if AChar in ['0'..'9'] then
  Result := Ord(AChar) - Ord('0')
  else
  Result := 10 + Ord(AChar) - Ord('A');
end;   


there is error message
thanks again.
« Last Edit: August 28, 2010, 01:33:20 am by ahmetnurideniz »

 

TinyPortal © 2005-2018