在c#中将小端序转换为大端序

本文关键字:转换 | 更新日期: 2023-09-27 18:21:05

我想将字符串"8BABEEF9D2472E65"转换为big-endian。

我已经输入了这个函数,我收到UINT大小错误。。我该怎么办?

功能:

    string bigToLittle(string data)
    {
        int number = Convert.ToInt32(data, 16); 
        byte[] bytes = BitConverter.GetBytes(number);
        string retval = "";
        foreach (byte b in bytes)
            retval += b.ToString("X2");
        return retval; 
    }

在c#中将小端序转换为大端序

我认为您可以使用System.Runtime.Remoting.Metadata.W3cXsd2001.SoapHexBinary类。

string input = "8BABEEF9D2472E65";
var output = new SoapHexBinary(SoapHexBinary.Parse(input).Value.Reverse().ToArray())
            .ToString();

输出:652E47D2F9EEAB8B

或者

var output = IPAddress.HostToNetworkOrder(long.Parse(input, NumberStyles.HexNumber))
            .ToString("X");

由于我们无法猜测你想要什么样的输出,我让自己写下了缺失的可能性(在代码注释中)。。。你可以选择你真正需要的:

internal class Program
{
    private static int ReverseBytes(long val)
    {
        byte[] intAsBytes = BitConverter.GetBytes(val);
        Array.Reverse(intAsBytes);
        return BitConverter.ToInt32(intAsBytes, 0);
    }
    private static string IntToBinaryString(long v)
    {
        string s = Convert.ToString(v, 2);
        string t = s.PadLeft(32, '0');
        string res = "";
        for (int i = 0; i < t.Length; ++i)
        {
            if (i > 0 && i%8 == 0)
                res += " ";
            res += t[i];
        }
        return res;
    }
    private static void Main(string[] args)
    {
        string sValue = "8BABEEF9D2472E65";
        long sValueAsInt = long.Parse(sValue, System.Globalization.NumberStyles.HexNumber);
        //output {-8382343524677898651}
        string sValueAsStringAgain = IntToBinaryString(sValueAsInt);
        //output {10001011 10101011 11101110 11111001 11010010 01000111 00101110 01100101}
        byte[] data = Encoding.BigEndianUnicode.GetBytes(sValue);
        string decodedX = Encoding.BigEndianUnicode.GetString(data);
        string retval = data.Aggregate("", (current, b) => current + b.ToString("X2"));
        //output {0038004200410042004500450046003900440032003400370032004500360035}
        char[] decodedX2 = Encoding.BigEndianUnicode.GetString(data).Reverse().ToArray();
        StringBuilder retval2 = new StringBuilder(); //output {56E2742D9FEEBAB8}
        foreach (var b in decodedX2)
            retval2.Append(b);

        Console.ReadLine();
    }
}

}

关于你的方法:

public static string bigToLittle(string data)
{
    long sValueAsInt = long.Parse(data, System.Globalization.NumberStyles.HexNumber);
    byte[] bytes = BitConverter.GetBytes(sValueAsInt);
    string retval = "";
    foreach (byte b in bytes)
        retval += b.ToString("X2");
    return retval; //output {652E47D2F9EEAB8B}
}

我喜欢@Sebatsian的回答,但我遇到了用额外的零填充条目的问题,所以我稍微修改了一下,以防你正在做一些事情,比如将Azure中的Linux VMUUID转换为标准格式。我使用了IPAddress.HostToNetworkOrder,它处理将long分解为字节并反转它们。

private string BigToLittle(string data)
{
    long sValueAsInt = long.Parse(data, System.Globalization.NumberStyles.HexNumber);
    var length = data.Length;
    return IPAddress.HostToNetworkOrder(sValueAsInt).ToString("X2").Substring(0, length);
}

使用中:

    string uuid = "090556DA-D4FA-764F-A9F1-63614EDA0163";
    
    private string BigToLittle(string data)
    {
        long sValueAsInt = long.Parse(data, System.Globalization.NumberStyles.HexNumber);
        var length = data.Length;
        return IPAddress.HostToNetworkOrder(sValueAsInt).ToString("X2").Substring(0, length);
    }
    var elements = uuid.Split(new char[] { '-' });
    var outMe = new List<string> { };
    foreach (var item in elements)
    {
        outMe.Add(BigToLittle(item));
    }
    var output = String.Join("-", outMe);
    //DA560509-FAD4-4F76-F1A9-6301DA4E6163