Sunny_Kumar
asked on
Convert long to byte array to string representation
Hi all,
I'm working with a 3rd party com method with returns a "dword" which is 32 bits. The first 16 bits contains info, and the rest is empty.
Using c#, I'm trying to get the returned value (which I think is a long?), convert that value to a byte array, and then get the string representation of the whole lot.
What I'm hoping to end up with is something like this:
"0000 1011 0000 0000" - without the spaces.
Each bit represents an error message and this is why I need to shift through the string values at specific indexes etc.
Can someone please help with this, I'm getting nowhere fast.
Thank you in advance,
Sunny
I'm working with a 3rd party com method with returns a "dword" which is 32 bits. The first 16 bits contains info, and the rest is empty.
Using c#, I'm trying to get the returned value (which I think is a long?), convert that value to a byte array, and then get the string representation of the whole lot.
What I'm hoping to end up with is something like this:
"0000 1011 0000 0000" - without the spaces.
Each bit represents an error message and this is why I need to shift through the string values at specific indexes etc.
Can someone please help with this, I'm getting nowhere fast.
Thank you in advance,
Sunny
You can get your 0s and 1s by converting the HEX value into a binary value.
Hex - Binary
0 = 0000
1 = 0001
2 = 0010
3 = 0011
4 = 0100
.
.
.
Hex - Binary
0 = 0000
1 = 0001
2 = 0010
3 = 0011
4 = 0100
.
.
.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Could you post the dword value that you are getting from 3rd party com method? so that any one of us help you with possible solution.
ASKER
Thanks for all the help guys! Much appreciated!
c# has the same bit operations as c and c++.
so you could do
what would test whether the unsigned integer ui has bit value 1 at bit position i.
that works cause 1<<i moves bit 0 to bit i and the ui & operation makes a bitwise 'and' such that the result either is 0 (all bits are 0) or has exactly 1 bit at position i.
Sara
so you could do
if ((ui & (1<<i) != 0)
what would test whether the unsigned integer ui has bit value 1 at bit position i.
that works cause 1<<i moves bit 0 to bit i and the ui & operation makes a bitwise 'and' such that the result either is 0 (all bits are 0) or has exactly 1 bit at position i.
Sara
there is a typo. it should be
Sara
if ((ui & (1<<i)) != 0)
Sara
See: http://msdn.microsoft.com/en-us/library/1184xdy4.aspx
// Example of the BitConverter.GetBytes( uint ) method.
using System;
class GetBytesUInt32Demo
{
const string formatter = "{0,16}{1,20}";
// Convert a uint argument to a byte array and display it.
public static void GetBytesUInt32( uint argument )
{
byte[ ] byteArray = BitConverter.GetBytes( argument );
Console.WriteLine( formatter, argument,
BitConverter.ToString( byteArray ) );
}
public static void Main( )
{
Console.WriteLine(
"This example of the BitConverter.GetBytes( uint ) " +
"\nmethod generates the following output.\n" );
Console.WriteLine( formatter, "uint", "byte array" );
Console.WriteLine( formatter, "----", "----------" );
// Convert uint values and display the results.
GetBytesUInt32( 15 );
GetBytesUInt32( 1023 );
GetBytesUInt32( 0x100000 );
GetBytesUInt32( 1000000000 );
GetBytesUInt32( uint.MinValue );
GetBytesUInt32( int.MaxValue );
GetBytesUInt32( uint.MaxValue );
}
}
/*
This example of the BitConverter.GetBytes( uint )
method generates the following output.
uint byte array
---- ----------
15 0F-00-00-00
1023 FF-03-00-00
1048576 00-00-10-00
1000000000 00-CA-9A-3B
0 00-00-00-00
2147483647 FF-FF-FF-7F
4294967295 FF-FF-FF-FF
*/