| |

VerySource

 Forgot password?
 Register
Search
View: 833|Reply: 5

How to compress an integer into a string of specified bytes and restore this string to this integer?

[Copy link]

1

Threads

3

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

Post time: 2020-3-10 09:00:01
| Show all posts |Read mode
I currently have two functions that implement this function, but can only handle positive integers. Negative integers can go wrong. Can you help me increase the function of handling negative integers? Thank you! !! !!
// Convert integer to protocol integer-multiple bytes in network byte order
Function ConvertInteger (Source, Len: Integer): String;
Var
    i: Integer;
Begin
    Result: = '';
    For i: = 1 To Len Do
    Begin
        Result: = Chr (Source And $ FF) + Result;
        Source: = Source Shr 8;
    End;
End;
// Restore protocol integer (multiple bytes in network byte order) to integer
Function RevertInteger (Source: String): Integer;
Var
    i: Integer;
Begin
    Source: = Copy (Source, 1,4);
    Result: = 0; i: = 1;
    While i <= Length (Source) Do
    Begin
        Result: = Result Shl 8 + Ord (Source [i]);
        Inc (i);
    End;
End;
Reply

Use magic Report

0

Threads

2

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-6-6 12:30:01
| Show all posts
There should be no problem.
Reply

Use magic Report

1

Threads

3

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

 Author| Post time: 2020-6-7 21:00:01
| Show all posts
But when I tested -2, it was converted back to 254
Reply

Use magic Report

1

Threads

3

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

 Author| Post time: 2020-6-8 12:30:01
| Show all posts
Put the -2 generation in, and it becomes 254 when returning. The test code is as follows:
Procedure TForm1.Button8Click (Sender: TObject);
Var
    EE,EE2: Integer;
    str: String;
Begin
    EE := -2;
    EE2 := 0;
    str := ConvertInteger (EE,1);
    EE2 := RevertInteger (str);
    showmessage ('EE2='+IntToStr (EE2) );
End;
Reply

Use magic Report

0

Threads

14

Posts

9.00

Credits

Newbie

Rank: 1

Credits
9.00

 China

Post time: 2020-6-11 18:00:01
| Show all posts
-2 and 254 are the same in binary, so put a flag in front,
One byte, the storage range is -128 --128 and 0 - 255
One word, the range of stored numbers is -32767 --32767 and 0 - 65535
An analogy, you control it yourself
Reply

Use magic Report

0

Threads

9

Posts

7.00

Credits

Newbie

Rank: 1

Credits
7.00

 China

Post time: 2020-6-20 11:45:01
| Show all posts
function ConvertInteger(Source, Len: Integer): String;
var
  i: Integer;
begin
  Result :='';
  if Len> SizeOf(Source) then
    Exit;
  for i := 1 to Len do
  begin
    Result := Chr(Source and $FF) + Result;
    Source := Source shr 8;
  end;
end;
//Restore the protocol integer (multiple bytes in network byte order) to an integer
function RevertInteger(Source: String): Integer;
var
  i, Len: Integer;
  n: Int64;
begin
  Source := Copy(Source, 1, SizeOf(Result));
  Result := 0;
  n := 0;
  i := 1;
  Len := Length(Source);
  while i <= Length(Source) do
  begin
    n := n shl 8 + Ord(Source[i]);
    Inc(i);
  end;
  if n> Power($100, Len) / 2-1 then
    n := n-Floor(Power($100, Len));
  Result := n;
end;

//Why does LZ want this? ? ? This conversion is easily out of bounds
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list