Created
September 2, 2024 13:44
-
-
Save QuiltMeow/6c77e0926e6ade9295b4d96484d2fca8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| namespace IntegerEndianConvert | |
| { | |
| public static class Program | |
| { | |
| public static byte[] endianConvert(byte[] data) | |
| { | |
| int length = data.Length; | |
| byte[] ret = new byte[length]; | |
| Array.Copy(data, 0, ret, 0, length); | |
| Array.Reverse(ret); | |
| return ret; | |
| } | |
| public static byte[] hexStringToByteArray(string hex, char separator = ' ') | |
| { | |
| string[] splitString = hex.Split(separator); | |
| int length = splitString.Length; | |
| byte[] ret = new byte[length]; | |
| for (int i = 0; i < length; ++i) | |
| { | |
| ret[i] = Convert.ToByte(splitString[i], 16); | |
| } | |
| return ret; | |
| } | |
| public static void Main() | |
| { | |
| Console.Write("請輸入位元組序列 (小端序) : "); | |
| string input = Console.ReadLine(); | |
| try | |
| { | |
| byte[] byteArray = hexStringToByteArray(input); | |
| Console.WriteLine($"Int 32 數值 : {BitConverter.ToInt32(byteArray, 0)}"); | |
| Console.WriteLine($"UInt 32 數值 : {BitConverter.ToUInt32(byteArray, 0)}"); | |
| byte[] reverseByteArray = endianConvert(byteArray); | |
| Console.WriteLine($"反轉位元陣列 (大端序) : {BitConverter.ToString(reverseByteArray).Replace("-", " ")}"); | |
| Console.WriteLine($"Int 32 數值 : {BitConverter.ToInt32(reverseByteArray, 0)}"); | |
| Console.WriteLine($"UInt 32 數值 : {BitConverter.ToUInt32(reverseByteArray, 0)}"); | |
| } | |
| catch (Exception ex) | |
| { | |
| Console.Error.WriteLine($"發生例外狀況 : {ex.Message}"); | |
| } | |
| Console.Write("請按任意鍵繼續 ..."); | |
| Console.ReadKey(true); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment