Showing entries with tag "C".

Found 4 entries

C/C++: Force floating point math

In C/C++ if you want to get a floating point result from integer division you need to cast at least one of the values as a float to force floating point division (not integer):

int a = 1;
int b = 7;

float c = (float)a / b;

If you don't cast one of the variables as a float you will get an integer result. This is true even if your resulting variable is cast as a float.

Leave A Reply

C++: Foreach over an array

If you have an array of elements that you want to iterate over you can do this:

int dpins[] = { 2, 4, 7, 12 };

for (int i = 0; i < (sizeof(dpins) / sizeof(dpins[0])); i++) {
    int pin  = dpins[i];
    int pval = digitalRead(pin);

    printf("%i = %i", pin, pval);
}

There is also a newer syntax adopted in C++11 (and supported by Arduino) that is more readable:

int dpins[] = { 2, 4, 7, 12 };

for (int pin : dpins) {
    int pval = digitalRead(pin);

    printf("%i = %i", pin, pval);
}
Leave A Reply

C/C++: Appending to a string with sprintf()

I'm a big fan of sprintf() and use it in a lot of projects. Often I will want to append to a string instead of creating a new one. This solution will create an "end of string" function named eos() that returns a pointer to the end of a given string. If you feed that to sprintf() it will effectively append to the existing string.

char *eos(char str[]) {
    return (str) + strlen(str);
}

int main(int argc, char *argv[]) {
    char tmp_str[50] = "";

    sprintf(eos(tmp_str), "Weird");
    sprintf(eos(tmp_str), " Al");
    sprintf(eos(tmp_str), " Yankovic");
}
Leave A Reply

Perlfunc: human_size()

Quick function to convert bytes to a human readable string:

my $str = human_size(1536);       # "1.5K"
my $str = human_size(1234567);    # "1.2M"
my $str = human_size(1234567890); # "1.1G"
sub human_size {
    my $size = shift();
    if (!defined($size)) { return undef; }

    if    ($size >= (1024**5) * 0.98) { $size = sprintf("%.1fP", $size / 1024**5); }
    elsif ($size >= (1024**4) * 0.98) { $size = sprintf("%.1fT", $size / 1024**4); }
    elsif ($size >= (1024**3) * 0.98) { $size = sprintf("%.1fG", $size / 1024**3); }
    elsif ($size >= (1024**2) * 0.98) { $size = sprintf("%.1fM", $size / 1024**2); }
    elsif ($size >= 1024)             { $size = sprintf("%.1fK", $size / 1024);    }
    elsif ($size >= 0)                { $size = sprintf("%dB"  , $size);           }

    return $size;
}

Here is the same function implemented in PHP:

function human_size($size) {
    # If the size is 0 or less, return 0 B this stops math errors from occurring
    if ($size <= 0) {
        return '0B';
    } else {
        $unit=array('B','K','M','G','T','P');
        return @round($size/pow(1024,($i=floor(log($size,1024)))),2) . $unit[$i];
    }
}

The same function in C

char buf[8] = "";

human_size(348, buf);
printf("Got: %s\n", buf);

char* humanSize(unsigned long long size, char* str) {
    if (size > 1152921504606846976L) {
        snprintf(str, 7, "%.1fE", (float)size / 1152921504606846976L);
    } else if (size > 1125899906842624L) {
        snprintf(str, 7, "%.1fP", (float)size / 1125899906842624L);
    } else if (size > 1099511627776L) {
        snprintf(str, 7, "%.1fT", (float)size / 1099511627776L);
    } else if (size > 1073741824L) {
        snprintf(str, 7, "%.1fG", (float)size / 1073741824L);
    } else if (size > 1048576L) {
        snprintf(str, 7, "%.1fM", (float)size / 1048576L);
    } else if (size > 1024) {
        snprintf(str, 7, "%.1fK", (float)size / 1024);
    } else if (size <= 1024) {
        snprintf(str, 7, "%uB", (unsigned)size);
    }

    return str;
}
Leave A Reply - 1 Reply