mirror of
https://github.com/UglyToad/PdfPig.git
synced 2025-09-18 18:27:55 +08:00
test hex to string implementation and remove unused method
This commit is contained in:
37
src/UglyToad.PdfPig.Tests/Util/HexTests.cs
Normal file
37
src/UglyToad.PdfPig.Tests/Util/HexTests.cs
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
namespace UglyToad.PdfPig.Tests.Util
|
||||||
|
{
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using PdfPig.Util;
|
||||||
|
using Xunit;
|
||||||
|
|
||||||
|
public class HexTests
|
||||||
|
{
|
||||||
|
public static IEnumerable<object[]> TestData => new[]
|
||||||
|
{
|
||||||
|
new object[] {new byte[0], string.Empty},
|
||||||
|
new object[] {new byte[] {37}, "25"},
|
||||||
|
new object[] {new byte[] {0}, "00"},
|
||||||
|
new object[] {new byte[] {255}, "FF"},
|
||||||
|
new object[]
|
||||||
|
{
|
||||||
|
new byte[]
|
||||||
|
{
|
||||||
|
37, 80, 68,
|
||||||
|
70, 45, 49,
|
||||||
|
46, 54, 13,
|
||||||
|
37
|
||||||
|
},
|
||||||
|
"255044462D312E360D25"
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
[Theory]
|
||||||
|
[MemberData(nameof(TestData))]
|
||||||
|
public void ConvertsToString(byte[] input, string expected)
|
||||||
|
{
|
||||||
|
var result = Hex.GetString(input);
|
||||||
|
|
||||||
|
Assert.Equal(expected, result);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@@ -1,6 +1,6 @@
|
|||||||
namespace UglyToad.PdfPig.Util
|
namespace UglyToad.PdfPig.Util
|
||||||
{
|
{
|
||||||
using System.IO;
|
using System;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -8,7 +8,7 @@
|
|||||||
*
|
*
|
||||||
* @author John Hewson
|
* @author John Hewson
|
||||||
*/
|
*/
|
||||||
internal class Hex
|
internal static class Hex
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* for hex conversion.
|
* for hex conversion.
|
||||||
@@ -16,17 +16,18 @@
|
|||||||
* https://stackoverflow.com/questions/2817752/java-code-to-convert-byte-to-hexadecimal
|
* https://stackoverflow.com/questions/2817752/java-code-to-convert-byte-to-hexadecimal
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
private static readonly byte[] HexBytes = { (byte) '0', (byte)'1', (byte)'2', (byte)'3', (byte)'4', (byte)'5', (byte)'6', (byte)'7',
|
|
||||||
(byte) '8', (byte) '9', (byte) 'A', (byte) 'B', (byte) 'C', (byte) 'D', (byte) 'E', (byte) 'F' };
|
|
||||||
private static readonly char[] HexChars = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
|
private static readonly char[] HexChars = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
|
||||||
|
|
||||||
private Hex() { }
|
/// <summary>
|
||||||
|
/// Returns a hex string for the given byte array.
|
||||||
/**
|
/// </summary>
|
||||||
* Returns a hex string of the given byte array.
|
|
||||||
*/
|
|
||||||
public static string GetString(byte[] bytes)
|
public static string GetString(byte[] bytes)
|
||||||
{
|
{
|
||||||
|
if (bytes == null)
|
||||||
|
{
|
||||||
|
throw new ArgumentNullException(nameof(bytes));
|
||||||
|
}
|
||||||
|
|
||||||
var stringBuilder = new StringBuilder(bytes.Length * 2);
|
var stringBuilder = new StringBuilder(bytes.Length * 2);
|
||||||
|
|
||||||
foreach (var b in bytes)
|
foreach (var b in bytes)
|
||||||
@@ -37,39 +38,14 @@
|
|||||||
return stringBuilder.ToString();
|
return stringBuilder.ToString();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Writes the given byte as hex value to the given output stream.
|
|
||||||
* @param b the byte to be written
|
|
||||||
* @param output the output stream to be written to
|
|
||||||
* @throws IOException exception if anything went wrong
|
|
||||||
*/
|
|
||||||
public static void WriteHexByte(byte b, BinaryWriter output)
|
|
||||||
{
|
|
||||||
output.Write(HexBytes[GetHighNibble(b)]);
|
|
||||||
output.Write(HexBytes[GetLowNibble(b)]);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* GetLongOrDefault the high nibble of the given byte.
|
|
||||||
*
|
|
||||||
* @param b the given byte
|
|
||||||
* @return the high nibble
|
|
||||||
*/
|
|
||||||
private static int GetHighNibble(byte b)
|
private static int GetHighNibble(byte b)
|
||||||
{
|
{
|
||||||
return (b & 0xF0) >> 4;
|
return (b & 0xF0) >> 4;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* GetLongOrDefault the low nibble of the given byte.
|
|
||||||
*
|
|
||||||
* @param b the given byte
|
|
||||||
* @return the low nibble
|
|
||||||
*/
|
|
||||||
private static int GetLowNibble(byte b)
|
private static int GetLowNibble(byte b)
|
||||||
{
|
{
|
||||||
return b & 0x0F;
|
return b & 0x0F;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user