❔ Bytes to UInt64

I am having a issue trying to turn the hex output to ulong, (UINT64). I keep getting 6687895829614943646 from 5CD02DD2B303DD9E I know 5CD02DD2B303DD9E should be 11447309898705915996 since my hex editor is right. I am using Little Endian. Any help would be appreciated, thank you. Here is my code:
private void GenerateHash()
{
// Make sure the input is not empty.
if (HashInput.Text.Length > 0)
{
// Convert \ to /
string hashInput = HashInput.Text.Replace(@"\", "/");
HashInput.Text = hashInput;

string hashString;
if (HashTypeList.SelectedIndex == 0)
{
hashString = ByteConverter.ByteArrayToString(HashFNV1.Hash(hashInput));
}
else
{
hashString = ByteConverter.ByteArrayToString(HashFNV1A.Hash(hashInput));

}

if (OutputAsHex.Checked)
{
HashResult.Text = hashString;
}
else
{
try
{

// Convert to UInt64
byte[] byteArray = HashFNV1A.Hash(hashInput);

if (BitConverter.IsLittleEndian)
Array.Reverse(byteArray);

ulong ulongValue = Convert.ToUInt64(hashString, 16);
HashResult.Text = ulongValue.ToString();
}
catch (OverflowException)
{
Console.WriteLine("Unable to convert '{0}' to an unsigned long integer.", hashString);
}
catch (FormatException)
{
Console.WriteLine("'{0}' is not in the correct format.", hashString);
}
}
}
}
private void GenerateHash()
{
// Make sure the input is not empty.
if (HashInput.Text.Length > 0)
{
// Convert \ to /
string hashInput = HashInput.Text.Replace(@"\", "/");
HashInput.Text = hashInput;

string hashString;
if (HashTypeList.SelectedIndex == 0)
{
hashString = ByteConverter.ByteArrayToString(HashFNV1.Hash(hashInput));
}
else
{
hashString = ByteConverter.ByteArrayToString(HashFNV1A.Hash(hashInput));

}

if (OutputAsHex.Checked)
{
HashResult.Text = hashString;
}
else
{
try
{

// Convert to UInt64
byte[] byteArray = HashFNV1A.Hash(hashInput);

if (BitConverter.IsLittleEndian)
Array.Reverse(byteArray);

ulong ulongValue = Convert.ToUInt64(hashString, 16);
HashResult.Text = ulongValue.ToString();
}
catch (OverflowException)
{
Console.WriteLine("Unable to convert '{0}' to an unsigned long integer.", hashString);
}
catch (FormatException)
{
Console.WriteLine("'{0}' is not in the correct format.", hashString);
}
}
}
}
32 Replies
mtreit
mtreit9mo ago
[13:17:23] ✓ 0x5CD02DD2B303DD9E
6687895829614943646
[13:17:23] ✓ 0x5CD02DD2B303DD9E
6687895829614943646
That's in PowerShell 6687895829614943646 is the correct value
MODiX
MODiX9mo ago
mtreit
REPL Result: Success
Console.WriteLine(0x5CD02DD2B303DD9E);
Console.WriteLine(0x5CD02DD2B303DD9E);
Console Output
6687895829614943646
6687895829614943646
Compile: 391.088ms | Execution: 25.128ms | React with ❌ to remove this embed.
Core Dream Studios
how? o_O
mtreit
mtreit9mo ago
[13:43:20] ✓ [BitConverter]::GetBytes(0x5CD02DD2B303DD9E) | %{$_.ToString("X")}
9E
DD
3
B3
D2
2D
D0
5C
[13:43:20] ✓ [BitConverter]::GetBytes(0x5CD02DD2B303DD9E) | %{$_.ToString("X")}
9E
DD
3
B3
D2
2D
D0
5C
MODiX
MODiX9mo ago
mtreit
REPL Result: Success
using System;
using System.Buffers.Binary;

var val = 0x5CD02DD2B303DD9E;

var littleEndianBuffer = new byte[8];
var bigEndianBuffer = new byte[8];
BinaryPrimitives.WriteInt64LittleEndian(littleEndianBuffer, val);
BinaryPrimitives.WriteInt64BigEndian(bigEndianBuffer, val);

Console.WriteLine("0x5CD02DD2B303DD9E as little endian:");
foreach (var b in littleEndianBuffer)
{
Console.Write($"{b:X2} ");
}

Console.WriteLine();
Console.WriteLine("----------------------------------");
Console.WriteLine("0x5CD02DD2B303DD9E as big endian:");
foreach (var b in bigEndianBuffer)
{
Console.Write($"{b:X2} ");
}
using System;
using System.Buffers.Binary;

var val = 0x5CD02DD2B303DD9E;

var littleEndianBuffer = new byte[8];
var bigEndianBuffer = new byte[8];
BinaryPrimitives.WriteInt64LittleEndian(littleEndianBuffer, val);
BinaryPrimitives.WriteInt64BigEndian(bigEndianBuffer, val);

Console.WriteLine("0x5CD02DD2B303DD9E as little endian:");
foreach (var b in littleEndianBuffer)
{
Console.Write($"{b:X2} ");
}

Console.WriteLine();
Console.WriteLine("----------------------------------");
Console.WriteLine("0x5CD02DD2B303DD9E as big endian:");
foreach (var b in bigEndianBuffer)
{
Console.Write($"{b:X2} ");
}
Console Output
0x5CD02DD2B303DD9E as little endian:
9E DD 03 B3 D2 2D D0 5C
----------------------------------
0x5CD02DD2B303DD9E as big endian:
5C D0 2D D2 B3 03 DD 9E
0x5CD02DD2B303DD9E as little endian:
9E DD 03 B3 D2 2D D0 5C
----------------------------------
0x5CD02DD2B303DD9E as big endian:
5C D0 2D D2 B3 03 DD 9E
Compile: 826.622ms | Execution: 65.431ms | React with ❌ to remove this embed.
mtreit
mtreit9mo ago
You are mixing up the endian-ness it seems.
Core Dream Studios
Hmm So what should I do?
Core Dream Studios
this is how its supposed to look like etc in hex anyway i think my editor has the UInt64 as LE even if the hex is BE but i need the LE version after the BE input is done
mtreit
mtreit9mo ago
In the code sample I pasted you can see the BinaryPrimitives type. You can use that to do either LittleEndian or BigEndian conversions, so just use that.
Core Dream Studios
I will give it a shot, thank you so much 🙂
mtreit
mtreit9mo ago
BinaryPrimitives.ReadInt64BigEndian
BinaryPrimitives.ReadInt64LittleEndian
BinaryPrimitives.ReadInt64BigEndian
BinaryPrimitives.ReadInt64LittleEndian
Core Dream Studios
In the code, above, should I start over? A lot of stuff now. I will keep my FNV1/FNV1A hash class of course
mtreit
mtreit9mo ago
What is the intent of the code? Generate a 64-bit hash?
Core Dream Studios
That already works yes 🙂 But I want to convert it to ulong (UInt64)
Core Dream Studios
my dumb little app lol doesnt need wpf for a simple 2 functions
mtreit
mtreit9mo ago
By the way, output type of "hex" and output type of "UInt64" doesn't make sense. One is a data type and the other is a representation. Like UInt64 can be represented as decimal, hexadecimal, binary, octal, whatever. So I think you mean "Decimal" not UInt64 This feels like it could be three lines of code or so.
Core Dream Studios
This is LE format though
// Convert to UInt64 and Little Endian
try
{
var leBuffer = new byte[8];

// Get the bytes from the input.
leBuffer = HashFNV1A.Hash(hashInput);


BinaryPrimitives.WriteInt64LittleEndian(leBuffer, hashString);


HashResult.Text = $"{b:X2}";
}
// Convert to UInt64 and Little Endian
try
{
var leBuffer = new byte[8];

// Get the bytes from the input.
leBuffer = HashFNV1A.Hash(hashInput);


BinaryPrimitives.WriteInt64LittleEndian(leBuffer, hashString);


HashResult.Text = $"{b:X2}";
}
what i have so far need to figure out the foreach for the textbox, doing now
if (OutputAsHex.Checked)
{
HashResult.Text = hashString;
}
else
{
// Convert to UInt64 and Little Endian
try
{
var val = hashString;

var leBuffer = new byte[8];
var beBuffer = new byte[8];

// Get the bytes from the input.
leBuffer = HashFNV1A.Hash(hashInput);


BinaryPrimitives.WriteInt64LittleEndian(leBuffer, val);
BinaryPrimitives.WriteInt64BigEndian(beBuffer, val);

foreach (var b in leBuffer)
{
Console.Write($"{b:X2} ");
}

// HashResult.Text = $"{b:X2}";
}
catch (OverflowException)
{
Console.WriteLine("Unable to convert '{0}' to an unsigned long integer.", hashString);
}
catch (FormatException)
{
Console.WriteLine("'{0}' is not in the correct format.", hashString);
}
}
}
if (OutputAsHex.Checked)
{
HashResult.Text = hashString;
}
else
{
// Convert to UInt64 and Little Endian
try
{
var val = hashString;

var leBuffer = new byte[8];
var beBuffer = new byte[8];

// Get the bytes from the input.
leBuffer = HashFNV1A.Hash(hashInput);


BinaryPrimitives.WriteInt64LittleEndian(leBuffer, val);
BinaryPrimitives.WriteInt64BigEndian(beBuffer, val);

foreach (var b in leBuffer)
{
Console.Write($"{b:X2} ");
}

// HashResult.Text = $"{b:X2}";
}
catch (OverflowException)
{
Console.WriteLine("Unable to convert '{0}' to an unsigned long integer.", hashString);
}
catch (FormatException)
{
Console.WriteLine("'{0}' is not in the correct format.", hashString);
}
}
}
updated issue with val, trying to figure out why it doesnt like the conversion, 1min doesn't like string for some reason long val = Convert.ToInt64(hashString, 16); fixed that one so far
mtreit
mtreit9mo ago
😐 Use ReadUInt64BigEndian or ReadUInt64LittleEndian To turn the bytes into a number
Core Dream Studios
5C D0 2D D2 B3 03 DD 9E ok, thanks made it worse, bleh, bbiab not having any luck atm, but will try later
mtreit
mtreit9mo ago
Wait, you just want the string representation of the hash as decimal or hex? That's literally just this:
var dec = hash.ToString();
var hex = hash.ToString("X");
Console.WriteLine(dec);
Console.WriteLine(hex);
var dec = hash.ToString();
var hex = hash.ToString("X");
Console.WriteLine(dec);
Console.WriteLine(hex);
using System;
using System.Collections.Generic;
using System.Text;

class Test
{
static void Main()
{
var str = "This is some string to hash!";
var hash = FNV1_64(str);
var dec = hash.ToString();
var hex = hash.ToString("X");
Console.WriteLine(dec);
Console.WriteLine(hex);
}

static ulong FNV1_64(string input)
{
var bytes = Encoding.UTF8.GetBytes(input);
return FNV1_64(bytes);
}

static ulong FNV1_64(byte[] buff)
{
unchecked
{
const long prime = (long)0x100000001b3;
long hash = (long)0xcbf29ce484222325;

for (int i = 0; i < buff.Length; i++)
{
hash *= prime;
hash ^= buff[i];
}

return (ulong)hash;
}
}
}
using System;
using System.Collections.Generic;
using System.Text;

class Test
{
static void Main()
{
var str = "This is some string to hash!";
var hash = FNV1_64(str);
var dec = hash.ToString();
var hex = hash.ToString("X");
Console.WriteLine(dec);
Console.WriteLine(hex);
}

static ulong FNV1_64(string input)
{
var bytes = Encoding.UTF8.GetBytes(input);
return FNV1_64(bytes);
}

static ulong FNV1_64(byte[] buff)
{
unchecked
{
const long prime = (long)0x100000001b3;
long hash = (long)0xcbf29ce484222325;

for (int i = 0; i < buff.Length; i++)
{
hash *= prime;
hash ^= buff[i];
}

return (ulong)hash;
}
}
}
[14:43:34] ✓ dotnet run
14077297499099057078
C35C9B7D9F4B4BB6
[14:43:34] ✓ dotnet run
14077297499099057078
C35C9B7D9F4B4BB6
Core Dream Studios
Sorry ,good morning. My internet was out the entire day. XD
// Convert to UInt64 and Little Endian
try
{
long val = Convert.ToInt64(hashString, 16);

// Convert the hexadecimal string to a byte array
byte[] bytes = new byte[hashString.Length / 2];
for (int i = 0; i < bytes.Length; i++)
{
bytes[i] = Convert.ToByte(hashString.Substring(i * 2, 2), 16);
}

// Reverse the byte array to convert from Big Endian to Little Endian
Array.Reverse(bytes);

// Convert the byte array to UInt64
ulong value = BitConverter.ToUInt64(bytes, 0);

// Use the value as needed
// ...

// Example: Print the bytes in Little Endian format
foreach (var b in bytes)
{
// Console.Write($"{b:X2} ");
Debug.Print($"{b:X2}");
}
}
catch (OverflowException)
{
Console.WriteLine("Unable to convert '{0}' to an unsigned long integer.", hashString);
}
catch (FormatException)
{
Console.WriteLine("'{0}' is not in the correct format.", hashString);
}
}
// Convert to UInt64 and Little Endian
try
{
long val = Convert.ToInt64(hashString, 16);

// Convert the hexadecimal string to a byte array
byte[] bytes = new byte[hashString.Length / 2];
for (int i = 0; i < bytes.Length; i++)
{
bytes[i] = Convert.ToByte(hashString.Substring(i * 2, 2), 16);
}

// Reverse the byte array to convert from Big Endian to Little Endian
Array.Reverse(bytes);

// Convert the byte array to UInt64
ulong value = BitConverter.ToUInt64(bytes, 0);

// Use the value as needed
// ...

// Example: Print the bytes in Little Endian format
foreach (var b in bytes)
{
// Console.Write($"{b:X2} ");
Debug.Print($"{b:X2}");
}
}
catch (OverflowException)
{
Console.WriteLine("Unable to convert '{0}' to an unsigned long integer.", hashString);
}
catch (FormatException)
{
Console.WriteLine("'{0}' is not in the correct format.", hashString);
}
}
I still need the Uint64, its not showing up
Core Dream Studios
Hope this explains it more 😦 nm i got it
Core Dream Studios
Thank you again @mtreit 🙂
Core Dream Studios
All set, thanks again for your help.
Accord
Accord9mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server
More Posts