Last active
August 19, 2020 08:30
-
-
Save devlights/62193c824ac598d199e6039ab3b14e55 to your computer and use it in GitHub Desktop.
エンディアンのチェック (ビッグエンディアンとリトルエンディアン)
This file contains 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
package main | |
import ( | |
"encoding/binary" | |
"fmt" | |
"io/ioutil" | |
"os" | |
) | |
func main() { | |
os.Exit(run()) | |
} | |
//goland:noinspection GoUnhandledErrorResult | |
func run() int { | |
var ( | |
i uint32 | |
) | |
fmt.Print("数値入力 (32bit範囲): 0x") | |
fmt.Fscanf(os.Stdin, "%x", &i) | |
fmt.Println("----------------------------------") | |
fmt.Printf("[プログラム上でそのまま][10進] %d\n", i) | |
fmt.Printf("[プログラム上でそのまま][16進] %#08x\n", i) | |
fmt.Printf("[プログラム上でそのまま][2進 ] %#032b\n", i) | |
fmt.Println("----------------------------------") | |
var ( | |
big = make([]byte, 4) | |
little = make([]byte, 4) | |
) | |
binary.BigEndian.PutUint32(big, i) | |
binary.LittleEndian.PutUint32(little, i) | |
fmt.Printf("[Big Endian][10進] %v\n", big) | |
fmt.Printf("[Little Endian][10進] %v\n", little) | |
fmt.Println("----------------------------------") | |
func() { | |
defer fmt.Println("") | |
fmt.Print("[Big Endian][16進] ") | |
for _, b := range big { | |
fmt.Printf("%02x ", b) | |
} | |
}() | |
func() { | |
defer fmt.Println("") | |
fmt.Print("[Little Endian][16進] ") | |
for _, b := range little { | |
fmt.Printf("%02x ", b) | |
} | |
}() | |
fmt.Println("----------------------------------") | |
func() { | |
defer fmt.Println("") | |
fmt.Print("[Big Endian][2進] ") | |
for _, b := range big { | |
fmt.Printf("%08b ", b) | |
} | |
}() | |
func() { | |
defer fmt.Println("") | |
fmt.Print("[Little Endian][2進] ") | |
for _, b := range little { | |
fmt.Printf("%08b ", b) | |
} | |
}() | |
fmt.Println("----------------------------------") | |
ioutil.WriteFile("big-endian.bin", big, 744) | |
ioutil.WriteFile("little-endian.bin", little, 744) | |
fmt.Printf("値 %d をビッグエンディアンで書き出したファイル ==> %s\n", i, "big-endian.bin") | |
fmt.Printf("値 %d をリトルエンディアンで書き出したファイル ==> %s\n", i, "little-endian.bin") | |
fmt.Println("----------------------------------") | |
fmt.Printf("ビッグエンディアンのバイトの並びで ビッグエンディアン で読み込み ==> %d\n", binary.BigEndian.Uint32(big)) | |
fmt.Printf("ビッグエンディアンのバイトの並びで リトルエンディアン で読み込み ==> %d\n", binary.LittleEndian.Uint32(big)) | |
fmt.Println("----------------------------------") | |
fmt.Printf("リトルエンディアンのバイトの並びで ビッグエンディアン で読み込み ==> %d\n", binary.BigEndian.Uint32(little)) | |
fmt.Printf("リトルエンディアンのバイトの並びで リトルエンディアン で読み込み ==> %d\n", binary.LittleEndian.Uint32(little)) | |
return 0 | |
} |
This file contains 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
#include <stdio.h> | |
#include <stdlib.h> | |
#include <byteswap.h> | |
void nouse_bswap(void); | |
void use_bswap(void); | |
int main(void) { | |
// ------------------------------------------------ | |
// エンディアン変換のサンプル | |
// ------------------------------------------------ | |
// ライブラリ関数を利用しないバージョン | |
nouse_bswap(); | |
printf("%s\n", "-----------------------------------------------"); | |
// ライブラリ関数を利用するバージョン | |
use_bswap(); | |
return EXIT_SUCCESS; | |
} | |
void nouse_bswap(void) { | |
u_int32_t i = 0x08040201; | |
u_int32_t r; | |
char *little = (char*) &i; | |
char *big = (char*) &r; | |
// ビッグエンディアンに入れ替え | |
big[0] = little[3]; | |
big[1] = little[2]; | |
big[2] = little[1]; | |
big[3] = little[0]; | |
printf("[Original ] %#08x\n", i); | |
printf("[Little Endian] %02x %02x %02x %02x\n", little[0], little[1], little[2], little[3]); | |
printf("[Big Endian] %02x %02x %02x %02x\n", big[0], big[1], big[2], big[3]); | |
} | |
void use_bswap(void) { | |
u_int32_t i = 0x08040201; | |
u_int32_t r; | |
r = bswap_32(i); | |
char *little = (char*) &i; | |
char *big = (char*) &r; | |
printf("[Original ] %#08x\n", i); | |
printf("[Little Endian] %02x %02x %02x %02x\n", little[0], little[1], little[2], little[3]); | |
printf("[Big Endian] %02x %02x %02x %02x\n", big[0], big[1], big[2], big[3]); | |
} |
This file contains 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; | |
using System.Linq; | |
using System.Net; | |
namespace EndianChecker | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
Console.Write("数値入力 (32bit範囲): 0x"); | |
var s = Console.ReadLine(); | |
int i = Convert.ToInt32(s, 16); | |
Console.WriteLine("----------------------------------"); | |
Console.WriteLine("[プログラム上でそのまま][10進] {0}", i); | |
Console.WriteLine("[プログラム上でそのまま][16進] {0}", Convert.ToString(i, 16)); | |
Console.WriteLine("[プログラム上でそのまま][2進 ] {0}", Convert.ToString(i, 2)); | |
Console.WriteLine("----------------------------------"); | |
var bytes = BitConverter.GetBytes(i); | |
var big = BitConverter.GetBytes(IPAddress.HostToNetworkOrder(i)); | |
var little = BitConverter.GetBytes(IPAddress.NetworkToHostOrder(BitConverter.ToInt32(big, 0))); | |
Console.WriteLine("[バイト列]"); | |
PrintBytes("Program ", bytes); | |
PrintBytes("Big Endian", big); | |
PrintBytes("Little Endian", little); | |
} | |
static void PrintBytes(string prefix, byte[] bytes) | |
{ | |
var value = string.Join(' ', bytes.Select(x => string.Format("{0:D2}", int.Parse(Convert.ToString(x, 16))))); | |
Console.WriteLine($"{prefix} {value}"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment