• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 734
  • Last Modified:

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
0
Sunny_Kumar
Asked:
Sunny_Kumar
1 Solution
 
Gene_CypCommented:
UInt32 can be converted using BitConverter.GetBytes


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
*/
0
 
Gene_CypCommented:
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
.
.
.
0
 
Daniel Van Der WerkenIndependent ConsultantCommented:
0
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
MathiyazhaganCommented:
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.
0
 
Sunny_KumarAuthor Commented:
Thanks for all the help guys! Much appreciated!
0
 
sarabandeCommented:
c# has the same bit operations as c and c++.

so you could do

if ((ui & (1<<i) != 0) 

Open in new window


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
0
 
sarabandeCommented:
there is a typo. it should be


if ((ui & (1<<i)) != 0)

Open in new window

 

Sara
0

Featured Post

Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now