commit dc9d80aff9333507a8555c5223379077d75ef7ad
Author: M. Yamanaka <myamanaka@live.com>
Date: Thu, 12 Nov 2020 22:17:13 -0500
endian test initial commit
Diffstat:
A | LICENSE | | | 22 | ++++++++++++++++++++++ |
A | Makefile | | | 9 | +++++++++ |
A | README | | | 30 | ++++++++++++++++++++++++++++++ |
A | endiantest.c | | | 56 | ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
4 files changed, 117 insertions(+), 0 deletions(-)
diff --git a/LICENSE b/LICENSE
@@ -0,0 +1,22 @@
+MIT License
+
+Copyright (c) 2020 M. Yamanaka <myamanaka@live.com>
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+SOFTWARE.
+
diff --git a/Makefile b/Makefile
@@ -0,0 +1,9 @@
+FILES=endiantest.c
+CC=gcc
+OUT=EndianTest
+
+$(OUT): $(FILES)
+ $(CC) $^ -o $@
+
+clean:
+ rm $(OUT)
diff --git a/README b/README
@@ -0,0 +1,30 @@
+Test for Endian-ness
+"endiantest"
+M. Yamanaka
+email: myamanaka@live.com
+website: csmyamanaka.com
+license: MIT (See included "LICENSE" file for details)
+
+Description:
+============
+Originally, this project was just a quick test used in another project (namely "File IO ++" or "fileiopp")
+because it was necessary for me to know how data is stored on my compiler because I was reading and writing byte data from files.
+Although it's a very small and simple test, I figured it could eventually become useful for other projects as well so I made it into its own repo.
+
+Installation:
+=============
+Just use make
+
+ make
+
+Usage:
+======
+After it's been compiled, just run it:
+
+ ./EndianTest
+
+
+Uninstall:
+==========
+
+ make clean
diff --git a/endiantest.c b/endiantest.c
@@ -0,0 +1,56 @@
+/*
+ Test for endian-ness
+ "endiantest.c"
+ M. Yamanaka
+ email: myamanaka@live.com
+ website: csmyamanaka.com
+ license: MIT (See included "LICENSE" file for details)
+*/
+
+#include <stdio.h>
+
+int main(){
+ /*
+ Originally, this test was used as a test for my other project "File IO ++" (fileiopp)
+ because it was quite necessary that I know the endian-ness of my compiler.
+ I figured it could be useful in itself to the point that it merits its own repo.
+ */
+
+ /*
+ I'm pretty sure integers are 4 bytes long at least on the compiler that I'm using.
+ Integer 65 could be stored as 00 00 00 65 or 65 00 00 00 (btw I chose 65 because it's the letter 'A' as a char).
+ I'm not bothering with the whole signed vs unsigned because it should be irrelevant given how small the number is
+ and also given that it's a positive number so there is no issue with any of the two's complement stuff.
+ */
+ int n = 65;
+
+ int* pn = &n;
+ /*
+ Having a char pointer point to the "first byte" of the integer could either mean
+ that it's pointing to "00" or to "65".
+
+ For big-endian, the most significant byte is stored in the first byte (i.e. how normal humans read)
+ The reverse is true for little endian.
+
+ Using a char pointer (which has a stride length of 1 byte) it should be fairly trivial to read the byte at the address of integer n
+ and the three following bytes (reading any more than that would probably cause a seg fault).
+
+ The following table should serve as a tl;dr
+
+ byte | 00 | 01 | 02 | 03 |
+ ------+----+----+----+----+
+ | 00 | 00 | 00 | 65 | big endian
+ int n +----+----+----+----+
+ | 65 | 00 | 00 | 00 | little endian
+ ------+----+----+----+----+
+ char* | +0 | +1 | +2 | +3 |
+
+ */
+ char* pcn = (char*)pn;
+
+
+ if(*pcn == '\0' && *(pcn + 3) == 'A') printf("big endian\n");
+ else if(*pcn == 'A' && *(pcn + 3) == '\0') printf("little endian\n");
+ else printf("something probably went wrong\n");
+ return 0;
+}