■ 이진 문자열 구하기 예제

------------------------------------------------------------------------------------------------------------------------

using System;

 

int    sourceValue1 = 100;

float  sourceValue2 = 100f;

double sourceValue3 = 100d;

 

string binaryString1 = GetBinaryString(sourceValue1);

string binaryString2 = GetBinaryString(sourceValue2);

string binaryString3 = GetBinaryString(sourceValue3);

 

Console.WriteLine(binaryString1 + " : " + sourceValue1);

Console.WriteLine(binaryString2 + " : " + sourceValue2);

Console.WriteLine(binaryString3 + " : " + sourceValue3);

------------------------------------------------------------------------------------------------------------------------

 

■ 이진 문자열 구하기

------------------------------------------------------------------------------------------------------------------------

using System;

using System.Text;

 

#region 이진 문자열 구하기 - GetBinaryString(sourceValue)

 

/// <summary>

/// 이진 문자열 구하기

/// </summary>

/// <param name="sourceValue">소스 값</param>

/// <returns>이진 문자열</returns>

public string GetBinaryString(int sourceValue)

{

    int temporaryValue = sourceValue;

 

    byte[] temporaryArray = new byte[32];

 

    for(int i = 0; i < 32; i++)

    {

        if((temporaryValue & 0x01) == 0x01)

        {

            temporaryArray[i] = 1;

        }

        else

        {

            temporaryArray[i] = 0;

        }

 

        temporaryValue = (temporaryValue >> 1);

    }

 

    StringBuilder stringBuilder = new StringBuilder();

 

    for(int i = 1; i <= 32; i++)

    {

        stringBuilder.Append(temporaryArray[32 - i]);

 

        if(i % 4 == 0)

        {

            stringBuilder.Append(" ");

        }

    }

 

    return stringBuilder.ToString();

}

 

#endregion

#region 이진 문자열 구하기 - GetBinaryString(sourceValue)

 

/// <summary>

/// 이진 문자열 구하기

/// </summary>

/// <param name="sourceValue">소스 값</param>

/// <returns>이진 문자열</returns>

public string GetBinaryString(float sourceValue)

{

    float temporaryValue = sourceValue;

 

    byte[] temporaryArray = new byte[32];

 

    unsafe

    {

        int* pointer = (int*)(&temporaryValue);

 

        for(int i = 0; i < 32; i++)

        {

            if((*pointer & 0x01) == 0x01)

            {

                temporaryArray[i] = 1;

            }

            else

            {

                temporaryArray[i] = 0;

            }

 

            *pointer = (*pointer >> 1);

        }

 

        StringBuilder stringBuilder = new StringBuilder();

 

        for(int i = 1; i <= 32; i++)

        {

            stringBuilder.Append(temporaryArray[32 - i]);

 

            if(i % 4 == 0)

            {

                stringBuilder.Append(" ");

            }

        }

 

        return stringBuilder.ToString();

    }

}

 

#endregion

#region 이진 문자열 구하기 - GetBinaryString(sourceValue)

 

/// <summary>

/// 이진 문자열 구하기

/// </summary>

/// <param name="sourceValue">소스 값</param>

/// <returns>이진 문자열</returns>

public string GetBinaryString(double sourceValue)

{

    double temporaryValue = sourceValue;

 

    byte[] temporaryArray = new byte[64];

 

    unsafe

    {

        int* pointer = (int*)(&temporaryValue);

 

        for(int i = 0; i < 64; i++)

        {

            if((*pointer & 0x01) == 0x01)

            {

                temporaryArray[i] = 1;

            }

            else

            {

                temporaryArray[i] = 0;

            }

 

            *pointer = (*pointer >> 1);

        }

 

        StringBuilder stringBuilder = new StringBuilder();

 

        for(int i = 1; i <= 64; i++)

        {

            stringBuilder.Append(temporaryArray[64 - i]);

 

            if(i % 4 == 0)

            {

                stringBuilder.Append(" ");

            }

        }

 

        return stringBuilder.ToString();

    }

}

 

#endregion

------------------------------------------------------------------------------------------------------------------------

Posted by 사용자 icodebroker
TAG

댓글을 달아 주세요