hex to decimal binary cli tool

Submitted scripts and programs
Forum rules
Your own work only.
User avatar
DebianJoe
Frame Buffer
Posts: 1915
Joined: Mon Jul 01, 2013 5:41 am
Location: emacs.d

hex to decimal binary cli tool

Unread post by DebianJoe » Fri Oct 04, 2013 6:27 pm

Well, you guys are always sharing your nice little quality of life things, and since I had to do this for a class, I thought I'd share it.

It's a simple tool that allows you to launch it from command line with a hexadecimal number after it (like "htoi FFF" or "htoi 0xF1F" or "htoi 0XFF1"), and it converts the hex number into an integer for you. Since we were supposed to allow upper or lowercase letter, catch overflows, or allow prefixing with '0x', this one took me a bit. Seems to work well, so why not share:

Code: Select all

#include <stdio.h>
#include <stdlib.h>
#include <limits.h>

unsigned int htoi(char s[])
{
    unsigned int val = 0;
    int x = 0;

    if(s[x] == '0' && (s[x+1]=='x' || s[x+1]=='X')) x+=2;

    while(s[x] != '\0'){
        if(val > UINT_MAX) return 0;
        else if(s[x]>='0' && s[x]<='9'){
            val = val * 16 + s[x] - '0';
        }
        else if(s[x]>='A' && s[x] <='F'){
            val = val * 16 + s[x] - 'A' + 10;
        }
        else if(s[x]>='a' && s[x]<='f'){
            val = val * 16 + s[x] - 'a' + 10;
        }
        else return 0;
        x++;
    }
    return val;
}

int main(int argc, char *argv[])
{
    char *hexalpha;
    if((argc > 1) && (argc < 3))
        hexalpha=argv[1];
    else{
        printf("Must be ran as 'htoi <hexnumber>'\n");
        exit(0);
    }
    if(htoi(hexalpha)==0)
        printf("Hex string overflow or non-valid entry\n");
    else
        printf("%u\n", htoi(hexalpha));

    return 0;
}

Compile with gcc (or your favorite C compiler) and output to a file called "htoi". Copy or move to /usr/bin/ or wherever you want it, and enjoy never having to convert a hex to a decimal again.

Guess I should explain the choice of name: Hex TO Interger == htoi
|>>BBQ Roaster, Alpha Branch<< | >> clinky << | >> X11 must die << |
Thanks BASIC

User avatar
wuxmedia
Grasshopper
Posts: 6454
Joined: Wed Oct 17, 2012 11:32 am
Location: Back in Blighty
Contact:

Re: hex to decimal binary cli tool

Unread post by wuxmedia » Fri Oct 04, 2013 6:40 pm

neat.
look forward to 'itoh'
mucho gusto.
"Seek, and Ye shall find"
"Github | Chooons | Site"

User avatar
xaos52
The Good Doctor
Posts: 190
Joined: Thu Aug 15, 2013 11:59 am
Location: Eernegem, Belgium

Re: hex to decimal binary cli tool

Unread post by xaos52 » Fri Oct 04, 2013 7:17 pm

htoi

Code: Select all

#!/bin/sh
printf '%d\n' 0x"$1"
Last edited by xaos52 on Fri Oct 04, 2013 7:32 pm, edited 1 time in total.
Connected. Take this REPL, brother, and may it serve you well.

User avatar
DebianJoe
Frame Buffer
Posts: 1915
Joined: Mon Jul 01, 2013 5:41 am
Location: emacs.d

Re: hex to decimal binary cli tool

Unread post by DebianJoe » Fri Oct 04, 2013 7:27 pm

|>>BBQ Roaster, Alpha Branch<< | >> clinky << | >> X11 must die << |
Thanks BASIC

User avatar
xaos52
The Good Doctor
Posts: 190
Joined: Thu Aug 15, 2013 11:59 am
Location: Eernegem, Belgium

Re: hex to decimal binary cli tool

Unread post by xaos52 » Fri Oct 04, 2013 7:30 pm

Code: Select all

#!/bin/sh
printf '%d\n' 0x"${1#0[xX]}"
This one allows 0x or 0X prefix.

It still works without the !

Yes, I know /bin/sh hides a lot of coding. But it is there ready for use on any linux system.
Connected. Take this REPL, brother, and may it serve you well.

User avatar
DebianJoe
Frame Buffer
Posts: 1915
Joined: Mon Jul 01, 2013 5:41 am
Location: emacs.d

Re: hex to decimal binary cli tool

Unread post by DebianJoe » Fri Oct 04, 2013 7:43 pm

xaos52 wrote: It still works without the !
I honestly didn't know that. I was trying it out while you replied.

hmmm..

Code: Select all

#!/usr/bin/env python
from sys import argv
script, first = argv
i = int(first, 16)
print i
....should work too, but WAY more overhead than any of the supplied.

Edit: I think it's probably reasonable to point out that Xaos's answer is by far the simplest way for a user who just wants a cli option for entering in a hex number and getting out a decimal one. You're already in a shell, just make the call. The only advantage to doing it in a lower level language is if it serves as a system process that is forked elsewhere as a sub-process, and you use the IO stream for some purpose. This is mostly just me joking at this point. Also, don't call python if there's a shell/bash option, ever. It's bloated and pointless. /disclaimer

2nd Edit: (because I just can't help myself.)

Code: Select all

#!/bin/sh
htoi $1
|>>BBQ Roaster, Alpha Branch<< | >> clinky << | >> X11 must die << |
Thanks BASIC

User avatar
Titan
なまいき
Posts: 407
Joined: Sat Oct 20, 2012 7:12 pm
Location: UK
Contact:

Re: hex to decimal binary cli tool

Unread post by Titan » Sat Oct 05, 2013 3:31 pm

Just tried this Joe and its great!

Going to go through the code and comment it to see if i understand it all, which will hopefully compliment my progress so far with the C book.
* Be fat, be as fat as you fucking please, just don't sit next to me on an aeroplane.
* "The sun never sets on the British Empire...." "Yeah, well, the sun never sets on my asshole!!"
* I am an "old skool" administrator who has been managing UNIX and Linux systems since the early 80s <-- big fkin lol

User avatar
GekkoP
Emacs Sancho Panza
Posts: 5878
Joined: Tue Sep 03, 2013 7:05 am

Re: hex to decimal binary cli tool

Unread post by GekkoP » Mon Oct 07, 2013 9:57 am

^ same here.
I'm still on learncodethehardway (both C and Python, guess why I'm going mad lately...), and I'll go through this C script to see what I can understand by my own.

Post Reply