.NET Framework Bookmark and Share   
 index > .NET Base Class Library > Reading bytes as Hex values
 

Reading bytes as Hex values

I am trying to read 100 bytes from a file then convert certain sections into doubles. I can read the data ok but it is stored as decimal values. is there some way to maintain the values as hex values?

Also is is possible to take a section from a larger array and store in a smaller one like:

byte[] smallArray = {largeArray[10]:largeArray[18]};

any help would be apreciated!

thanks, Peter
peterbeeby

Decimal and hexadecimal are just ways to present a number, it doesn't affect the way it's stored in memory.

Use Array.Copy or CopyTo to get a segment of a larger array into a smaller one.

Mattias Sjögren
I'm not sure where you might have gone wrong. This sample program has all the conversions you mention:

Module Module1
Sub Main()
Dim dbl As Double = 1492183.5
Dim dblInBytes() As Byte = BitConverter.GetBytes(dbl)
'--- Display value in bytes
Console.WriteLine(BitConverter.ToString(dblInBytes))
Dim dbl2 As Double = BitConverter.ToDouble(dblInBytes, 0)
'--- Bytes converted to Double
Console.WriteLine(dbl2)
Dim dblAsInt64 As Int64 = BitConverter.ToInt64(dblInBytes, 0)
'--- Bytes converted to Long
Console.WriteLine("{0:X}", dblAsInt64)
Dim dbl3 As Double = BitConverter.Int64BitsToDouble(dblAsInt64)
'--- Long converted to Double
Console.WriteLine(dbl3)
'--- The other way around:
Array.Reverse(dblInBytes)
Dim dbl4 As Double = BitConverter.ToDouble(dblInBytes, 0)
Console.WriteLine(dbl4)
Console.ReadLine()
End Sub
End Module

Note that the last number matches the number you get, suggesting you're doing something wrong with the byte order.
nobugz

Decimal and hexadecimal are just ways to present a number, it doesn't affect the way it's stored in memory.

Use Array.Copy or CopyTo to get a segment of a larger array into a smaller one.

Mattias Sjögren

thanks for the reply, i guess im doing something wrong then

the data is stored as little endian, for ex 00 00 00 80 D7 C4 36 41

i reverse the data then using bit converter to change it to a long then convert to a double.

theresult i believe should be 1.4921835 E006but i am getting: doubleValue = 2.7340395930909E-312

argument = 65 54 196 215 128 0 0 0

temp = 553375774273

long temp = BitConverter.ToInt64(argument,0);

double doubleValue = BitConverter.Int64BitsToDouble(temp);

return doubleValue;

if i make temp = (long)0x4136C4D780000000;

temp = 4699159691052187648

doubleValue=1492183.5

how should i resove this?

peterbeeby
I'm not sure where you might have gone wrong. This sample program has all the conversions you mention:

Module Module1
Sub Main()
Dim dbl As Double = 1492183.5
Dim dblInBytes() As Byte = BitConverter.GetBytes(dbl)
'--- Display value in bytes
Console.WriteLine(BitConverter.ToString(dblInBytes))
Dim dbl2 As Double = BitConverter.ToDouble(dblInBytes, 0)
'--- Bytes converted to Double
Console.WriteLine(dbl2)
Dim dblAsInt64 As Int64 = BitConverter.ToInt64(dblInBytes, 0)
'--- Bytes converted to Long
Console.WriteLine("{0:X}", dblAsInt64)
Dim dbl3 As Double = BitConverter.Int64BitsToDouble(dblAsInt64)
'--- Long converted to Double
Console.WriteLine(dbl3)
'--- The other way around:
Array.Reverse(dblInBytes)
Dim dbl4 As Double = BitConverter.ToDouble(dblInBytes, 0)
Console.WriteLine(dbl4)
Console.ReadLine()
End Sub
End Module

Note that the last number matches the number you get, suggesting you're doing something wrong with the byte order.
nobugz
Shoot, I was wearing by VB.NET hat. Hope it still makes sense.
nobugz

it seems that i shouldn't reverse the array, not sure why the numbers got so messed up but its working now.

Thanks for the help,

Peter

peterbeeby

You can use google to search for other answers

Custom Search

More Threads

• When is the new version of dot net schedule to come out?
• Newby of Newby questions
• Can I use OCR MODI Library (MDIVWCTL.DLL) in .NET in a production server application?
• .net 2.0 uninstall
• Set generic collection? (Java HashSet)
• data base connection
• EventLog Class and "ForwardedEvents" Log
• Run a class as a alternate user in domain environment?
• Is the release of the .Net framework 2.0 tied to the release of Visual Studio 2005
• Bug with NullableConverter ?