Integer Math - Converting to Binary

Yes, I realize this is a beginning digital logic concept, but I need somewhere to write this so I don't forget again.

Unsigned Integer to Binary and Back

You are given a number: 25382
Take the modulo of it to test if it is even or odd: 25382 % 2 = 0 <= ???? ???? ???? ???0
Divide by two to get a new integer: 25382 / 2 = 12691
Take the modulo of the new number to test if it is even or odd: 12691 % 2 = 1 <= ???? ???? ???? ??10
Divide by two to get a new integer: 12691 / 2 = 6345
Take the modulo of the new number to test if it is even or odd: 6345 % 2 = 1 <= ???? ???? ???? ?110
Divide by two to get a new integer: 6345 / 2 = 3172
Take the modulo of the new number to test if it is even or odd: 3172 % 2 = 0 <= ???? ???? ???? 0110
Divide by two to get a new integer: 3172 / 2 = 1586
Take the modulo of the new number to test if it is even or odd: 1586 % 2 = 0 <= ???? ???? ???0 0110
Divide by two to get a new integer: 1586 / 2 = 793
Take the modulo of the new number to test if it is even or odd: 793 % 2 = 1 <= ???? ???? ??10 0110
Divide by two to get a new integer: 793 / 2 = 396
Take the modulo of the new number to test if it is even or odd: 396 % 2 = 0 <= ???? ???? ?010 0110
Divide by two to get a new integer: 396 / 2 = 198
Take the modulo of the new number to test if it is even or odd: 198 % 2 = 0 <= ???? ???? 0010 0110
Divide by two to get a new integer: 198 / 2 = 99
Take the modulo of the new number to test if it is even or odd: 99 % 2 = 1 <= ???? ???1 0010 0110
Divide by two to get a new integer: 99 / 2 = 49
Take the modulo of the new number to test if it is even or odd: 49 % 2 = 1 <= ???? ??11 0010 0110
Divide by two to get a new integer: 49 / 2 = 24
Take the modulo of the new number to test if it is even or odd: 24 % 2 = 0 <= ???? ?011 0010 0110
Divide by two to get a new integer: 24 / 2 = 12
Take the modulo of the new number to test if it is even or odd: 12 % 2 = 0 <= ???? 0011 0010 0110
Divide by two to get a new integer: 12 / 2 = 6
Take the modulo of the new number to test if it is even or odd: 6 % 2 = 0 <= ???0 0011 0010 0110
Divide by two to get a new integer: 6 / 2 = 3
Take the modulo of the new number to test if it is even or odd: 3 % 2 = 1 <= ??10 0011 0010 0110
Divide by two to get a new integer: 3 / 2 = 1
Take the modulo of the new number to test if it is even or odd: 1 % 2 = 1 <= ?110 0011 0010 0110
Divide by two to get a new integer: 1 / 2 = 0
Take the modulo of the new number to test if it is even or odd: 0 % 2 = 0 <= 0110 0011 0010 0110

25382 = 0110 0011 0010 0110

With a bit of programming, this can be highly consolidated:

int decVal = 25382;
char binVal[16];
printf("Integer: = Binary: ");
for (int i = 0; i < 16; i++) {
    binVal[i] = (decVal % 2);
    decVal /= 2;
    printf("%d", binVal[i]);

Note however that the binary value's lowest-order bit is expressed on the right-hand side whereas an array's lowest-order element is expressed on the left-hand side. This will result in the direct output of the array being displayed as the reverse of the binary string it represents.

binInt = 0110001100100110;
          = B15 B14 B13 ... B2 B1 B0;

binVal[16] = {0, 1, 1, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 1, 0};
                  = {I0, I1, I2, ... , I13, I14, I15};

Thus, we will need to parse through the array in reverse order when using its values to either print to the standard output or convert back into the regular integer:

// binVal is calculated from for loop above.
// binVal[16] = {0, 1, 1, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 1, 0};
int decVal;
printf("Binary String: ");
for (int i = 0; i < 16; i++) {
    if (binVal[16-(i+1)] == 1) {
        decVal += pow(2,i);
printf(" = Integer: %d",decVal);

Finally! An internal DNS Server that works (for me)!

So it's been much too long since I wanted to do this, but thankfully, I have finally discovered the solution for which I can serve up my own DNS server for internal LAN things, but still have an external host that administers my public website!

My scenario and quandry was this:

Right now, our company is having our website (and inherently our domain) maintained and hosted with an outside provider. This is fairly simple to set up, and all the back-end stuff is kept nice and tidy on some remote server that isn't likely to have any serious power outages, so our public content is pretty much guaranteed to always be live.

However, our domain and its DNS records were being handled by this service as well (,,, etc). This wasn't too much of a problem, because before we didn't care.

Then I decided, hey it would be really nice to have a server where we could host internal company things such as an internal website, network storage area, svn/git, etc. This was easy. Just get the boss to buy us a fancy new server computer from Dell, install a good, solid Linux server OS on it and get going. And it worked no problem.

And it turns out our public (internet) IP was static as well, which meant that I could get up on that public webhost DNS record and add a nice little subdomain for our public IP to let us serve up with a few port forwarding configurations through our NAT router and cable gateway. Awesome!

However, there was one big issue. For because we are set up with a two-step configuration to connect to the internet (router signs into cable modem as a client, and cable modem signs into Comcast), we were not able to pull up from INSIDE the company. Of course, what sort of issue is that, when you can just dial in the reserved IP from inside? Well...that's confusing. Not that confusing to someone who knows what they're doing, but definitely confusing to someone who is just trying to make it work.

But how should I set it up so that if someone from outside the LAN is trying to access they get routed to our Comcast public IP, but if they're inside the LAN, they get routed to

I had toyed with the idea of an in-house DNS for quite a while. Yet, everywhere I read, there was a ton of legaleze mixed into the documentation: "You shouldn't have records to a name that you don't own, you shouldn't create records for a domain that already has a master record somewhere else, etc." This was annoying. How would I ever be able to even play with the idea of a DNS server if everyone kept telling me I shouldn't do it the only way I saw it could be done?

But then I had this thought: "The entire internet is not going to be able to access my DNS server as long as I don't forward the port it uses through the router. Nobody in the Internet Policing Agency is going to even KNOW that I have a DNS server that is routing domains that I don't own. And after reading all the "this is what DNS actually does" documentation with its levels of looking up names all the way from the website-level DNS servers to the root DNS servers, no actual computer out there will ever use my public IP address to look up DNS records, because I simply won't let the world know it's even possible! I'm not qualified as a valid DNS host, nor do I ever intend to be.

I found a pretty good tutorial at that was super descriptive in telling me just exactly what DNS did. And then I found the entire article was based around a cool browser-based interface called Webmin. Turns out I can pretty well administrate the entire server over that service. I'm definitely using it for other stuff!

But anyway, after a bit of reading, I decided to give using it a shot.
  1. I couldn't very well test out a DNS server without configuring a client to route through it for lookups, so I changed my LAN adapter settings in Windows to look at for a primary DNS server. Since I don't care about standards with the system, I decided to leave the secondary field blank, which it took.
  2. I installed BIND9 via aptitude: sudo apt-get install bind9 bind9-host bind9-doc
  3. Next I grabbed Webmin from Sourceforge to prepare for its installation: wget though if this article is outdated (which in a few months it probably will be) the actual page to download will be more likely at
  4. Tried installing it once and it failed due to a few missing libraries, so I grabbed those as well after reading the actual installation instructions from the Webmin wiki: sudo apt-get install perl libnet-ssleay-perl openssl libauthen-pam-perl libpam-runtime libio-pty-perl apt-show-versions python
  5. Now, since I had attempted to install Webmin from the .deb file before, it automatically asked in no simple terms if I wanted to retry after aptitude finished installing those libraries. I apparently said yes to this (sudo apt-get -f install) and webmin was installed from some cache somewhere. However, if I had done it right, I would have next tried: sudo dpkg --install webmin_1.660_all.deb
  6. Once Webmin was installed and autoconfigured, I was then prompted to navigate to http://server:10000 in my browser:
  7. Once logged in via Webmin, I navigated to Servers > BIND DNS Server

    However, this only let me access the internet as the internet, the internal network by their hostnames and the local computer as itself.
  8. Now all the tutorials I found said that you should only create a master zone if you own it, and if this was the primary location for its record (to avoid clashing with the actual one or something). However, again, this was all for experimentation, and I had to put rules and standards aside. So I clicked Create master zone in the zone section of the BIND DNS Server configuration window.
    After all, creating a slave zone only lets you ask an already-existing master for its records. You can't create DNS records in a slave zone.
  9. From here, I specified the Zone type as Forward, the Domain as, left Records file set as automatic, set, as was recommended on the above-mentioned tutorial, to put the server hostname (server) in the Master server field, and for the heck of it put my email in the email field. The rest I left as default.
  10. With no problems whatsoever, I was brought to the Edit Master Zone screen. 
  11. Clicking the Address icon, I was brought to an A-NAME record entry screen, where I entered the Name internal and the address I left Update reverse? as Yes which is its default.
  12. To test it out, I clicked the Apply Zone link at the top right of the screen. However, since I left Time-To-Live as default, I had to wait a few minutes before it actually worked. In time, I was able to open another browser window and successfully load the internal webpage at!!

    Sweet! So it works the way I want it. The internal subdomain successfully loads my internal webpages. But what about the external site that was hosted elsewhere, along with its domain name record which is also hosted elsewhere? Well, I've told my own internal domain name server that I'm controlling the root now as the master record, when in all actuality the official root and www subdomains are being controlled offsite by some other company. I guess this is what they meant by making sure there was only one master zone. Well, thankfully BIND can take care of this issue as well!
  13. Backing out to the Master Zone Config screen by clicking the Return to record types link at the bottom the page, I was able to click the Name Server icon. This record type allows me to enter a subdomain and pass all the DNS lookup work off on some other server...which is exactly what I needed to do.
  14. In the Zone Name field, I first entered @ which is the default root prefix for any domain name. In the Name Server field, I then entered our website hosting DNS server Of course, since public, qualified DNS servers have two to three (and in the case of root domain servers, 13) actual machines doing all the work, I created a new Name Server record for the root subdomain with our host's second dns server name I also repeated this process for the www and ftp subdomain.
    In all reality, this seems a lot like the experience one might have with connecting through several devices to get onto the Internet, or port forwarding a port through multiple levels of network firewalls. The cable modem thinks the entire network consists of one client: the router, with an IP address of But the ROUTER sees the rest of the computers on the LAN and serves them up with IP's. In the same way, the router sees as the actual machine with the webserver that we tell it to forward through to the outside world. But to the router, the outside world is only its single wire from it to the modem on the network that the modem is serving. And the modem thinks that the router at is the machine with the webserver that it needs to forward through. A lot of information passing just to get information to the outside world, and by then, who knows where it goes with all the routing and hopping?
  15. In any case, I'd gotten all of my public website subdomain requests passed to our hosting provider's name servers at and, applied the zone settings (and just to be safe, applied the configuration as well) in Webmin.
    After a few minutes, I was able to confirm that yes in fact got me to our external website, as did And still routed me via the LAN to the server's webpage.
  16. I was basically done.. Except for one thing. How in the world was I going to get all the computers in the shop to look at my DNS server before they went outside to look at the real, public ones? I had set it up on my own computer to look at as its primary DNS server via the IPv4 settings in the Ethernet driver settings, but I had no way of telling the other computers around the shop to do the same. Or did I?

    Opening up the main LAN router settings (an Apple Airport Extreme), I was pleased to find in its Internet > TCP/IP settings a little field there titled DNS Server(s):.
    Entering the LAN IP of my server (again, and saving it, I was able to login over the Wi-Fi with my laptop, visit and and re-verify that everything STILL worked. Now with every request the router received to pull information from the internet or elsewhere, it would first look at my internal server to pick apart domain names before going outside to the public servers.
So that's that! Now I just need to do something similar at home. That, and creating internal subdomains for different sites hosted off of the same machine. THAT should prove interesting... but given that it's almost the end of the work day, that will have to wait until either next week, or when it finally proves practical.

So in short, if its for internal use, you can use any domain name you want on in your DNS records. Nobody or computer will know any different unless you explicitly tell it to search your own DNS server first. This method could even be used as a website filtering service. No more proxies through external services. Just create a blacklist and route all traffic via DNS records back in to a page that says, "Sorry. That website is blocked."

And because many people complain that the BIND configuration is super hard to figure out, I would highly recommend running Webmin on your server. It brings a GUI interface to so many of your tools...and eliminates a lot of the headache of typing everything in a console, or figuring out where everything is kept.


New project at work! Setting up OpenVPN. It's great fun...except when it doesn't work.

I love the Windows interface for setting up the Server. It's straight-forward, and following the tutorial at works like a charm. Setting up the client is just as easy, and the connection can be made in a matter of minutes.

However, I started running into problems when I began trying to replicate the server over to Ubuntu 12.04. The tutorial once again was straightforward and everything SEEMED to work out. I could start up the server and everything. It was all good.

However, for some strange reason, I was unable to actually connect to the client. I would generate the certificate authority key, server key, and client key plus the Diffie Hellman number. Server again started up great. But after transferring the client keys to my Windows computer, and setting up the client configuration, I kept getting an odd error "Private key password verification failed."

Looking this error up, all I could find were forum entries on talking about making sure you didn't set a challenge password when generating keys and ensuring that the user/password authentication on your OpenVPN server was turned off. Both of these were true in my case, so no help.

Then I started thinking, could it be the age-old, infamous Unix vs. MS line ending problem? Let's find out! Opened each of the files, ca.crt, client.crt and client.key in a text editor in turn, made a slight change, undid it and saved the file to ensure they all had MS line endings. was in this process that I found my big problem. Opening client.key (the private RSA key that the client would use when authenticating over SSL), I found that instead of a standard RSA hash, it read:

<html><head><br /><title>403
Forbidden</title></head><body><br /><h1>
<br />You don't have permission to access /Lynx/dp_m14x.key<br />on this server.<br />
<br /><hr />
<br /><address>
Apache/2.2.22 (Ubuntu) Server at Port 80</address>

Oops.  Turns out that Ubuntu, when generating the key/cert via easy-rsa, decided to give the private key file root-only access, which, for this type of file is a *very good thing*. Thus, when attempting to access it via the standard Apache www-data user from the browser, it gave me a 403 which I did not see as I did a right-click > Save As on my directory listing of the file.

Attempted to instead copy it to my Samba folder and transfer it over to my Windows client that way. Still no go. Now I got Windows access errors "Could not read file..."

Finally decided to get smart about it.
cp /etc/openvpn/easy-rsa/keys/client.key /home/daniel/Documents
chmod 777 /home/daniel/Documents/client.key

rm /home/daniel/Documents/client.key

Now I just need to figure out how in the world to transfer it over without being so unsecured about it. Gzip?

This Annoying Christmas Theme

Amazing. I've neglected this site so much in the past year that I forgot to take my Christmas theme down and now it's time to put it back up!


Ruby on Rails - Success kinda!

Okay. I'm totally going to try this again, just to see if I can't figure it out.

Ruby on Rails - A ruby based framework for web stuff. Basically, I haven't a clue, but I'm going to at least try to get it running on Windows and make some rudimentary website things, just to see HOW easy it is once I get the basic syntax down.

For the steps outlined below, I'm basically following the tutorial from verbatim, save for a few edits I'm needing to make to get the libraries downloaded and installed properly (i.e., they never told me in that tutorial that we needed the Ruby DevKit installed, and the DevKit download page doesn't really say HOW to install it).

  1. Download and install Ruby. Current version is 2.0.0p247 from I installed mine to C:\Ruby200.
  2. Ensure Ruby is accessible from anywhere in the command shell by typing "ruby -v". You should get the current version of Ruby. If not, you may need to edit your environment variable "PATH" to include C:\Ruby200\bin (edited, of course, to where you installed Ruby).
  3. Download and unzip ruby DevKit, again from For version 2.0.0 and up, use DevKit 4.7.2. MinGW64-32 or MinGW64-64 specifies whether you're on 32 or 64-bit. I unzipped the development kit to C:\Ruby_DevKit.
  4. Install Ruby DevKit:
    1. Open a command shell and navigate to the location where you unzipped the development kit (C:\Ruby_DevKit).
    2. Type "ruby dk.rb" init
    3. Type "ruby dk.rb" install
    4. You should get messages that the development kit overwrote a few ruby gems that may have been already installed (Updating convenience notice gem override for 'C:/Ruby200'), and an "installing /devkit.rb"
    5. Type "gem install rails". You should get a ton of secondary libraries (gems) downloaded along with the main rails gem and documentation for each which could take a good long while. Just be patient. You should get something like "20 gems installed" at the end of the sequence.
    6. Type "rails --version". I got the message "Rails 4.0.0"
    7. We now have the ruby libraries needed to build a Rails application. Create a new location for Rails (C:\Rails, perhaps), navigate into it from the command shell and type "rails new ". I did like the tutorial suggested and just tried "rails new blog" which created an entire app folder structure in the "blog" subfolder. This was after I received the success messages "Your bundle is complete!" after it had installed a bunch of ...whatevers to the blog folder.
    8. Now, I did notice, rails automatically installs its OWN webserver (WEBrick, not Apache or IIS), so this app is able to run straight from its own folder out of Rails. I believe there's a way to install it over top of Apache, and use the Apache server configurations to control its access, but that's for later. Right now I just want to get this thing working in the first place, and am willing to put up with WEBrick.
    9. So anyway, I'm now told to startup Rails by typing "rails server".
    10. Whoops! I got a bunch of sqlite errors. Looks like I need to set up database config and connections first. I also tried starting OUT of the blog application, in which case I was told I needed to be inside a Rails application folder before I could start the server.
    11. Well that was odd. Tried installing the sqlite3 gem, which did not fix anything. Turns out I needed some other version of Ruby that had all the headers included, which I apparently did not do by using the windows installer. I REALLY hate trying to compile gcc projects on Windows. Hopefully I can try to make it work with MySQL since I already have that installed with source files and it's a part of my path.
    12. Thus, I deleted the blog folder and re-created the app with "rails new blog -d mysql" which forces the new app to use MySQL instead of the default sqlite.
    13.  Got a bundler error that said I needed the mysql2 gem. Ran "gem install mysql2 '0.3.13'. This errored, so I tried "gem install mysql" which worked, but was not the right version.
    14. Finally after a bit of research, I got the mysql2 gem to work with the following
      1. "subst X: "C:\Program Files\MySQL\MySQL Server 5.6"
      2. "gem install mysql2 mysql2 --platform=ruby -- --with-mysql-dir=X: --with-mysql-lib=X:\lib --with-mysql-include=X:\include" <== Note the double -- in there. Basically, this splits out the main command with a bunch of sub-switches....I think. Simply put, you HAVE to have the -- --with-mysql- in there. Otherwise it will fail with an "invalid switch" error.
    15. Trying "rails new blog -d mysql" once more.
    16. Success! I received the "Your bundle is complete" message.
    17. However, when starting up the server, I once again got errors (needed MySQL2 gem 0.3.13). Tried to install using "gem install mysql2 --version=0.3.13 -- stuff stuff stuff". That worked. However, when trying to start up the server, I was STILL getting a bunch of stupid errors about missing something inside the mysql2 0.3.13 gem.
    18. I managed to fix this error. Basically, I was able to copy libmysql.dll from the MySQL Server/lib folder into Ruby/bin. Rails is now started up clean with no errors whatsoever -  I actually see the line "=> Ctrl-C to shutdown server."
    19. Tried navigating to "localhost:3000" and got the error "Specified 'mysql' for database adapter, but the gem is not loaded. Add `gem 'mysql'` to your Gemfile." - My database.yml I think is using mysql instead of mysql2 for its handle. I will change it there.
    20. Sure enough. Changing the "adapter" field in config/database.yml from "mysql" to "mysql2" got rid of the error. However, now it's attempting to login to my MySQL root user without a password. Gotta enter that information (with a password that you will not see repeated here ;)).
    21. Entered the password into database.yml. However, the error did not disappear. I manged to figure it out. I have to restart Rails. Ctrl-C in the command shell and run "rails server" again. This time it worked. However, now the website saying that it cannot find the "blog_development" database. Apparently it's not smart enough to create it itself (yet), so I will have to look up new tutorials on how to do that later on.
    22. Check that. I just went into PHPMyAdmin and created a blank "blog_developement" database, not adding anything to it. A refresh on the page now shows the "welcome to rails" homepage! I'm in!
In any case! I've apparently gotten it to work, so I'll be changing the title of the blog post from "A failed attempt" to "Success kinda!!"