Skip to content

Commit 92f76ba

Browse files
committed
Found out that float to int bytes conversion methods are already available in standard lirary
1 parent 9c63165 commit 92f76ba

File tree

1 file changed

+8
-48
lines changed

1 file changed

+8
-48
lines changed

src/FirebirdSql.Data.FirebirdClient/Client/Managed/XdrReaderWriter.cs

Lines changed: 8 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -317,42 +317,22 @@ public async ValueTask<Guid> ReadGuidAsync(int sqlType, CancellationToken cancel
317317
}
318318
}
319319

320-
public float Int2Single(int sqlType)
321-
{
322-
Span<byte> bytes = stackalloc byte[4];
323-
if (!BitConverter.TryWriteBytes(bytes, sqlType))
324-
{
325-
throw new InvalidOperationException("Failed to write Single bytes.");
326-
}
327-
return BitConverter.ToSingle(bytes);
328-
}
329-
330320
public float ReadSingle()
331321
{
332-
return Int2Single(ReadInt32());
322+
return BitConverter.Int32BitsToSingle(ReadInt32());
333323
}
334324
public async ValueTask<float> ReadSingleAsync(CancellationToken cancellationToken = default)
335325
{
336-
return Int2Single(await ReadInt32Async(cancellationToken).ConfigureAwait(false));
337-
}
338-
339-
public double Long2Double(long sqlType)
340-
{
341-
Span<byte> bytes = stackalloc byte[8];
342-
if (!BitConverter.TryWriteBytes(bytes, sqlType))
343-
{
344-
throw new InvalidOperationException("Failed to write Double bytes.");
345-
}
346-
return BitConverter.ToDouble(bytes);
326+
return BitConverter.Int32BitsToSingle(await ReadInt32Async(cancellationToken).ConfigureAwait(false));
347327
}
348328

349329
public double ReadDouble()
350330
{
351-
return Long2Double(ReadInt64());
331+
return BitConverter.Int64BitsToDouble(ReadInt64());
352332
}
353333
public async ValueTask<double> ReadDoubleAsync(CancellationToken cancellationToken = default)
354334
{
355-
return Long2Double(await ReadInt64Async(cancellationToken).ConfigureAwait(false));
335+
return BitConverter.Int64BitsToDouble(await ReadInt64Async(cancellationToken).ConfigureAwait(false));
356336
}
357337

358338
public DateTime ReadDateTime()
@@ -1026,42 +1006,22 @@ public ValueTask WriteAsync(long value, CancellationToken cancellationToken = de
10261006
return ReturnAfter(task, rented);
10271007
}
10281008

1029-
static int Float2Int(float value)
1030-
{
1031-
Span<byte> bytes = stackalloc byte[4];
1032-
if (!BitConverter.TryWriteBytes(bytes, value))
1033-
{
1034-
throw new InvalidOperationException("Failed to write Single bytes.");
1035-
}
1036-
return BitConverter.ToInt32(bytes);
1037-
}
1038-
10391009
public void Write(float value)
10401010
{
1041-
Write(Float2Int(value));
1011+
Write(BitConverter.SingleToInt32Bits(value));
10421012
}
10431013
public ValueTask WriteAsync(float value, CancellationToken cancellationToken = default)
10441014
{
1045-
return WriteAsync(Float2Int(value), cancellationToken);
1046-
}
1047-
1048-
static long Double2Long(double value)
1049-
{
1050-
Span<byte> buffer = stackalloc byte[8];
1051-
if (!BitConverter.TryWriteBytes(buffer, value))
1052-
{
1053-
throw new InvalidOperationException("Failed to write Double bytes.");
1054-
}
1055-
return BitConverter.ToInt64(buffer);
1015+
return WriteAsync(BitConverter.SingleToInt32Bits(value), cancellationToken);
10561016
}
10571017

10581018
public void Write(double value)
10591019
{
1060-
Write(Double2Long(value));
1020+
Write(BitConverter.DoubleToInt64Bits(value));
10611021
}
10621022
public ValueTask WriteAsync(double value, CancellationToken cancellationToken = default)
10631023
{
1064-
return WriteAsync(Double2Long(value), cancellationToken);
1024+
return WriteAsync(BitConverter.DoubleToInt64Bits(value), cancellationToken);
10651025
}
10661026

10671027
public void Write(decimal value, int type, int scale)

0 commit comments

Comments
 (0)