Byte Swapping in C

Introduction Some times you need to swap bytes. Sometimes you don’t. But right now we do. Well, we don’t but if we were implementing some sorting functions we’d need a good swap function. Sorting typically doesn’t move pointers around, instead it moves the bytes pointed to by the pointer. Generic Swap We only want to have one swap function and not one for every single possible type. We’ll use void pointers and a width (number of bytes the type occupies) to make a generic swap function....

March 15, 2018 · John

Binary Search and Insert

Introduction When dealing with arrays it’s often necessary to keep their elements in sorted order. The easiest way is to use an insert algorithm that always puts element into the array in sorted order. That said, you don’t know where elements will end up in the array because if you already knew the order you wouldn’t need to sort in the first place. At some point you will need to pull elements back out and you will want a quick way to find a given element....

January 18, 2018 · John

Base64 Encode and Decode in C

Introduction A very popular way to encode binary data is Base64. The basis of this is an encoding table. As you might expect, there are 64 total characters that go into the tale. There are multiple implementations of base64 with slight differences. They are all the same except for the last two characters and line ending requirements. The first 62 characters are always “ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789”. PEM and MIME encoding are the most common and use “+/” as the last two characters....

November 18, 2017 · John

Hex Encode and Decode in C

Introduction A very common task when working with binary data in C is, converting it to and from Hex. It’s unfortunate that C doesn’t provide a standard function that can take care of this for us. However, it’s pretty easy to implement a set of functions to handle it. Hex encoding is always twice the size of binary. Since hex is base 16 we can take any unsigned char value 0-255 and represent it in two hex digits, 0x00-0xFF....

September 24, 2017 · John

Wrapping C++ objects in C

Introduction Using C functions from C++ is very easy but going the other way isn’t. However, it can be done with a little ingenuity. Really, this isn’t as crazy as it sounds. I’ll use a simple adder for the object. It takes an integer as a starting value and has two functions for adding and getting the current value. This could be expanded later into a complex object for mathematical operations but that’s really not necessary right now....

August 18, 2017 · John

JNI is (Not) Your Friend

Introduction So far we’ve scratched the surface of using JNI when we looked at wrapping a C library and Calling Java from C. Now we’re going to look into some more complex uses. Java Class Since we’re working with JNI, we’ll need a Java class to use what we’re going to expose from C. DemoFuncs.java class DemoFuncs { static { System.loadLibrary("demo_lib"); } public enum Days { MON, TUE, WED, THU, FRI, SAT, SUN, } public static native int[] demo_array_return(); public static native int[] demo_array_copy_inc(int[] in); public static native Days demo_enum_val(); public static native Days demo_enum_field_val(); public static native void demo_exception_1(); public static native void demo_exception_2(); public static native void demo_exception_3(); public static native boolean demo_exception_4(); public static native Name demo_name(String name); } Note: This class includes an enum because one of the later examples uses them....

July 9, 2017 · John

Calling Java From C

Introduction Obviously, JNI lets you call Java functions and use Java classes in C. Typical a Java app is the one calling into C. Now Let’s say you don’t have a Java app that kicks off the process but you want your C app to still use some Java code. Okay, I know what you’re thinking, “why”? Well, there are companies out there that provide SDKs for their products only in Java....

June 17, 2017 · John

Wrapping a C library in Java

Introduction It can’t be argued that Java is popular and successful. It is consistently the number one language on TIOBE’s popularity list, above C which comes in as number two. This ranking is based on popularity and doesn’t mean Java is more used than C but that doesn’t change the fact that there is a lot of Java code out there. Unlike other languages interop with Java is not what I’d call easy....

June 6, 2017 · John

Recursive Create Directory in C

C has a very large gap when it comes to working with files and directories. It is C, so all the building blocks are there. Thankfully, it’s pretty easy to put together something useful. In a project I’ve been working on I needed to create a directory. This is pretty trivial but I needed to create a directory tree where all the parts might not already exist. Unfortunately C’s directory creation functions can’t make directories recursively....

May 17, 2017 · John

Poddown a simple podcast downloader

I have been listing to podcasts for a long time and many years ago I wrote a podcast downloader. Back then I had a media center and I wanted my favorite podcasts to automatically download each night. At that time there wasn’t anything that really did that. Well, there was but they were full management and play back applications. All I wanted was a simple downloader and not seeing any I wrote my own....

May 6, 2017 · John