I try to verify an UUID v4. I try to do this with sscanf, if the UUID can be read completly with sscanf (= total number of characters read - 36), i assume this is a correct UUID. My code up to now:
#include <stdio.h>
int main()
{
char uuid[ 37 ] = "da4dd6a0-5d4c-4dc6-a5e3-559a89aff639";
int a = 0, b = 0, c = 0, d = 0, e = 0, g = 0;
long long int f = 0;
printf( "uuid >%s<, variables read: %d \n", uuid, sscanf( uuid, "%8x-%4x-4%3x-%1x%3x-%12llx%n", &a, &b, &c, &d, &e, &f, &g ) );
printf( " a - %x, b - %x, c - %x, d - %x, e - %x, f - %llx, total number of characters read - %d \n", a, b, c, d, e, f, g );
return 0;
}
which return the following output
uuid >da4dd6a0-5d4c-4dc6-a5e3-559a89aff639<, variables read: 6
a - da4dd6a0, b - 5d4c, c - dc6, d - a, e - 5e3, f - 559a89aff639, total number of characters read - 36
So far, everything okay.
Now I want to include, that the first character after the third hyphen needs to be one of [89ab]. So I changed %1x%3x to %1x[89ab]%3x. But now, the first character is read and the rest not anymore.
The output:
uuid >da4dd6a0-5d4c-4dc6-a5e3-559a89aff639<, variables read: 4
a - da4dd6a0, b - 5d4c, c - dc6, d - a, e - 0, f - 0, total number of characters read - 0
What am I missing? What is wrong with the syntax? Is possible to read it like this? I tried several combinations of the scanset and the specifier, but nothing works.
Instead of using sscanf() for this task, you might just write a simple dedicated function:
#include <ctype.h>
#include <string.h>
int check_UUID(const char *s) {
int i;
for (i = 0; s[i]; i++) {
if (i == 8 || i == 13 || i == 18 || i == 23) {
if (s[i] != '-')
return 0;
} else {
if (!isxdigit((unsigned char)s[i])) {
return 0;
}
}
if (i != 36)
return 0;
// you can add further tests for specific characters:
if (!strchr("89abAB", s[19]))
return 0;
return 1;
}
If you insist on using sscanf(), here is concise implementation:
#include <stdio.h>
int check_UUID(const char *s) {
int n = 0;
sscanf(s, "%*8[0-9a-fA-F]-%*4[0-9a-fA-F]-%*4[0-9a-fA-F]-%*4[0-9a-fA-F]-%*12[0-9a-fA-F]%n", &n);
return n == 36 && s[n] == '\0';
}
If you want to refine the test for the first character after the third hyphen, add another character class:
#include <stdio.h>
int check_UUID(const char *s) {
int n = 0;
sscanf(s, "%*8[0-9a-fA-F]-%*4[0-9a-fA-F]-%*4[0-9a-fA-F]-%*1[89ab]%*3[0-9a-fA-F]-%*12[0-9a-fA-F]%n", &n);
return n == 36 && s[n] == '\0';
}
Notes:
The * after the % means do not store the conversion, just skip the characters and the 1 means consume at most 1 character.
For the number of characters parsed by sscanf to reach 36, all hex digit sequences must have exactly the specified width.
%n causes scanf to store the number of characters read so far into the int pointed to by the next argument.
your conversion specification is useful to get the actual UUID numbers, but the %x format accepts leading white space, an optional sign and an optional 0x or 0X prefix, all of which are invalid inside a UUID. You can first validate the UUID, then convert it to its individual parts if required.
Now I want to include, that the first character after the third hyphen needs to be one of [89ab]. So I changed %1x%3x to %1x[89ab]%3x
Should have been "%1[89ab]%3x" and then saved into a 2 character string. Then convert that small string into a hex value with strtol(..., ..., 16).
Instead, I suggest a 2 step validation for universally unique identifier (UUID)
:
Check for syntax, then read the value.
I'd avoid "%x" as it allows leading spaces, leading '+','-' and optional leading 0x and narrow inputs.
For validation, perhaps a simply test in code:
#include <ctype.h>
#include <stdio.h>
// byte lengths: 4-2-2-2-6
typedef struct {
unsigned long time_low;
unsigned time_mid;
unsigned time_hi_and_version;
unsigned clock_seq_hi_and_res_clock_seq_low;
unsigned long long node;
} uuid_T;
uuid_T* validate_uuid(uuid_T *dest, const char *uuid_source) {
static const char *uuid_pat = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx";
const char *pat = uuid_pat;
const unsigned char *u = (const unsigned char*) uuid_source;
while (*u) {
if ((*pat++ == 'x' && !isxdigit(*u)) || *u != '-') {
return NULL;
}
u++;
}
if (*pat) { // Too short
return NULL;
}
sscanf(uuid_source, "%lx-%x-%x-%x-%llx", &dest->time_low,
&dest->time_mid, &dest->time_hi_and_version,
&dest->clock_seq_hi_and_res_clock_seq_low, &dest->node);
return dest;
}
u is unsigned char *u so isxdigit(*u) is only called with non-negative values and so avoids UB,
Related
I have data in my .txt file containing different time ins and time outs of employees. For example, 10:20 but I initially designed the structure to have their data types to be of char arrays or string. Since I'll be using the time values in another function, I have to use the atoi() function to convert them into integer values. Problem is, there is a colon : in each of the time values. Would it be possible to convert the string 10:20 to an integer using atoi() so that I can it in my future functions? Does the use of atoi() allow some splitting or some sort so that I can convert my time value from string to int?
I tried
char time[10] = "10:20";
int val;
printf("string val = %s, int value = %d", time, atoi(time));
But my output is only
string val = 10:20, int value = 10 so only the string before the : is read and converted to string. I would want that after converting, I would stil have 10:20 as the result but in integer because I am going to use relational operators with it.
It's not clear what you actually want, but maybe something like:
#include <stdio.h>
#include <stdlib.h>
int
main(int argc, char **argv)
{
char *time = argc > 1 ? argv[1] : "10:20";
int d;
char *e;
d = strtol(time, &e, 10);
if( *e == ':' ){
d *= 100;
d += strtol(e + 1, &e, 10);
}
if( *e != '\0' ){
fprintf(stderr, "invalid input\n");
return 1;
}
printf("string val = %s, int value = %d\n", time, d);
return 0;
}
This will produce d = 1020 for the string "10:20". It's not at all clear to me what integer you want to produce, but that seems to be what you're looking for.
You can also use sscanf:
#include <stdio.h>
int main() {
char const* time = "10:20";
int h, m;
if (sscanf(time, "%d:%d", &h, &m) != 2)
return 1;
printf("string val = %s, int value = %d\n", time, h * 100 + m);
}
Right now I am trying to convert an int to a char in C programming. After doing research, I found that I should be able to do it like this:
int value = 10;
char result = (char) value;
What I would like is for this to return 'A' (and for 0-9 to return '0'-'9') but this returns a new line character I think.
My whole function looks like this:
char int2char (int radix, int value) {
if (value < 0 || value >= radix) {
return '?';
}
char result = (char) value;
return result;
}
to convert int to char you do not have to do anything
char x;
int y;
/* do something */
x = y;
only one int to char value as the printable (usually ASCII) digit like in your example:
const char digits[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
int inttochar(int val, int base)
{
return digits[val % base];
}
if you want to convert to the string (char *) then you need to use any of the stansdard functions like sprintf, itoa, ltoa, utoa, ultoa .... or write one yourself:
char *reverse(char *str);
const char digits[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
char *convert(int number, char *buff, int base)
{
char *result = (buff == NULL || base > strlen(digits) || base < 2) ? NULL : buff;
char sign = 0;
if (number < 0)
{
sign = '-';
}
if (result != NULL)
{
do
{
*buff++ = digits[abs(number % (base ))];
number /= base;
} while (number);
if(sign) *buff++ = sign;
if (!*result) *buff++ = '0';
*buff = 0;
reverse(result);
}
return result;
}
A portable way of doing this would be to define a
const char* foo = "0123456789ABC...";
where ... are the rest of the characters that you want to consider.
Then and foo[value] will evaluate to a particular char. For example foo[0] will be '0', and foo[10] will be 'A'.
If you assume a particular encoding (such as the common but by no means ubiquitous ASCII) then your code is not strictly portable.
Characters use an encoding (typically ASCII) to map numbers to a particular character. The codes for the characters '0' to '9' are consecutive, so for values less than 10 you add the value to the character constant '0'. For values 10 or more, you add the value minus 10 to the character constant 'A':
char result;
if (value >= 10) {
result = 'A' + value - 10;
} else {
result = '0' + value;
}
Converting Int to Char
I take it that OP wants more that just a 1 digit conversion as radix was supplied.
To convert an int into a string, (not just 1 char) there is the sprintf(buf, "%d", value) approach.
To do so to any radix, string management becomes an issue as well as dealing the corner case of INT_MIN
The following C99 solution returns a char* whose lifetime is valid to the end of the block. It does so by providing a compound literal via the macro.
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
// Maximum buffer size needed
#define ITOA_BASE_N (sizeof(unsigned)*CHAR_BIT + 2)
char *itoa_base(char *s, int x, int base) {
s += ITOA_BASE_N - 1;
*s = '\0';
if (base >= 2 && base <= 36) {
int x0 = x;
do {
*(--s) = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"[abs(x % base)];
x /= base;
} while (x);
if (x0 < 0) {
*(--s) = '-';
}
}
return s;
}
#define TO_BASE(x,b) itoa_base((char [ITOA_BASE_N]){0} , (x), (b))
Sample usage and tests
void test(int x) {
printf("base10:% 11d base2:%35s base36:%7s ", x, TO_BASE(x, 2), TO_BASE(x, 36));
printf("%ld\n", strtol(TO_BASE(x, 36), NULL, 36));
}
int main(void) {
test(0);
test(-1);
test(42);
test(INT_MAX);
test(-INT_MAX);
test(INT_MIN);
}
Output
base10: 0 base2: 0 base36: 0 0
base10: -1 base2: -1 base36: -1 -1
base10: 42 base2: 101010 base36: 16 42
base10: 2147483647 base2: 1111111111111111111111111111111 base36: ZIK0ZJ 2147483647
base10:-2147483647 base2: -1111111111111111111111111111111 base36:-ZIK0ZJ -2147483647
base10:-2147483648 base2: -10000000000000000000000000000000 base36:-ZIK0ZK -2147483648
Ref How to use compound literals to fprintf() multiple formatted numbers with arbitrary bases?
Check out the ascii table
The values stored in a char are interpreted as the characters corresponding to that table. The value of 10 is a newline
So characters in C are based on ASCII (or UTF-8 which is backwards-compatible with ascii codes). This means that under the hood, "A" is actually the number "65" (except in binary rather than decimal). All a "char" is in C is an integer with enough bytes to represent every ASCII character. If you want to convert an int to a char, you'll need to instruct the computer to interpret the bytes of an int as ASCII values - and it's been a while since I've done C, but I believe the compiler will complain since char holds fewer bytes than int. This means we need a function, as you've written. Thus,
if(value < 10) return '0'+value;
return 'A'+value-10;
will be what you want to return from your function. Keep your bounds checks with "radix" as you've done, imho that is good practice in C.
1. Converting int to char by type casting
Source File charConvertByCasting.c
#include <stdio.h>
int main(){
int i = 66; // ~~Type Casting Syntax~~
printf("%c", (char) i); // (type_name) expression
return 0;
}
Executable charConvertByCasting.exe command line output:
C:\Users\boqsc\Desktop\tcc>tcc -run charconvert.c
B
Additional resources:
https://www.tutorialspoint.com/cprogramming/c_type_casting.htm
https://www.tutorialspoint.com/cprogramming/c_data_types.htm
2. Convert int to char by assignment
Source File charConvertByAssignment.c
#include <stdio.h>
int main(){
int i = 66;
char c = i;
printf("%c", c);
return 0;
}
Executable charConvertByAssignment.exe command line output:
C:\Users\boqsc\Desktop\tcc>tcc -run charconvert.c
B
You can do
char a;
a = '0' + 5;
You will get character representation of that number.
Borrowing the idea from the existing answers, i.e. making use of array index.
Here is a "just works" simple demo for "integer to char[]" conversion in base 10, without any of <stdio.h>'s printf family interfaces.
Test:
$ cc -o testint2str testint2str.c && ./testint2str
Result: 234789
Code:
#include <stdio.h>
#include <string.h>
static char digits[] = "0123456789";
void int2str (char *buf, size_t sz, int num);
/*
Test:
cc -o testint2str testint2str.c && ./testint2str
*/
int
main ()
{
int num = 234789;
char buf[1024] = { 0 };
int2str (buf, sizeof buf, num);
printf ("Result: %s\n", buf);
}
void
int2str (char *buf, size_t sz, int num)
{
/*
Convert integer type to char*, in base-10 form.
*/
char *bufp = buf;
int i = 0;
// NOTE-1
void __reverse (char *__buf, int __start, int __end)
{
char __bufclone[__end - __start];
int i = 0;
int __nchars = sizeof __bufclone;
for (i = 0; i < __nchars; i++)
{
__bufclone[i] = __buf[__end - 1 - i];
}
memmove (__buf, __bufclone, __nchars);
}
while (num > 0)
{
bufp[i++] = digits[num % 10]; // NOTE-2
num /= 10;
}
__reverse (buf, 0, i);
// NOTE-3
bufp[i] = '\0';
}
// NOTE-1:
// "Nested function" is GNU's C Extension. Put it outside if not
// compiled by GCC.
// NOTE-2:
// 10 can be replaced by any radix, like 16 for hexidecimal outputs.
//
// NOTE-3:
// Make sure inserting trailing "null-terminator" after all things
// done.
NOTE-1:
"Nested function" is GNU's C Extension. Put it outside if not
compiled by GCC.
NOTE-2:
10 can be replaced by any radix, like 16 for hexidecimal outputs.
NOTE-3:
Make sure inserting trailing "null-terminator" after all things
done.
I would like to convert a string to an int and calling the function from main. Where the first character is a letter declaring the base of the number and the rest of the characters in the string are the number. I am able to get the function to work separately, but when using the main function to call it will not print out the correct values.
Example of user input using binary:
b1000
b1010
result should be:
b
b
1000
1010
Here is the code:
#include <stdio.h>
#include <string.h>
#include <math.h>
int str_to_int(inputbase) {
char num1[50];
num1[50] = inputbase;
char numcpy1[sizeof(num1) - 1];
int i, len1;
int result1 = 0;
//printf("String: ");
//gets(num1);
//Access first character for base
printf("%c \n", num1[0]);
//Remove first character for number1 and number 2
if (strlen(num1) > 0) {
strcpy(numcpy1, &(num1[1]));
} else {
strcpy(numcpy1, num1);
}
len1 = strlen(numcpy1);
//Turn remaining string characters into an int
for (i = 0; i < len1; i++) {
result1 = result1 * 10 + ( numcpy1[i] - '0' );
}
printf("%d \n", result1);
return result1;
}
int main() {
char *number1[50], *number2[50];
int one, two;
printf("\nAsk numbers: \n");
gets(number1);
gets(number2);
one = str_to_int(number1);
two = str_to_int(number2);
printf("\nVerifying...\n");
printf("%d\n", one);
printf("%d\n", two);
return 0;
}
I suppose your code cannot be compiled because some errors.
The first one is in the line
int str_to_int(inputbase)
where inputbase are defined without type.
If this changed to
int str_to_int(char * inputbase)
the next point for improvement is in line
num1[50] = inputbase;
assignement like that has set of errors:
num1[50] means access to 51th item, but there is only 50 items indexed from 0 to 49
statement num1[0] = inputbase; (as well as with any other correct index) is wrong because of difference in types: num1[0] is char, but inputbase is pointer
num1 = inputbase; will be also wrong (for copying string = cannot be used in C, so consider making loop or using standard library function strncpy)
And since this is only the beginning of problems, I suggest starting from decimal input using some standard function for conversion char* string to int (e.g. atoi, or sscanf), then after you check the program and find it correct if it is required you can avoid using standard conversion and write your own str_to_int
The prototype for your function str_to_int() should specify the type of intputbase. You are passing a string and there is no reason for str_to_int to modify this string, so the type should be const char *inputbase.
Furthermore, you do not need a local copy for the string, just access the first character to determine the base and parse the remaining digits accordingly:
#include <stdlib.h>
int str_to_int(const char *inputbase) {
const char *p = inputbase;
int base = 10; // default to decimal
if (*p == 'b') { // binary
p++;
base = 2;
} else
if (*p == 'o') { // octal
p++;
base = 8;
} else
if (*p == 'h') { // hexadecimal
p++;
base = 16;
}
return strtol(p, NULL, base);
}
I have this code:
char kbits[k];
long int bits;
The kbits string is always filled with '1', for example:
If k = 5,
kbits = "11111"
If k = 9,
kbits = "111111111"
To do this I use this code:
for(i = 0; i < k; i++)
kbits[i] = '1';
And then I run:
bits = atoi(kbits);
So I have the integer of kbits, for example, if kbits = "1111", bits = 1111;
For k <= 10, it runs perfectly fine.
For k > 10 it puts a 7 in last position of kbits (for example, if k = 11, kbits = 11111111117) and bits = 2147483647 (this value is for any value of k, I think it is random?)
atoi interprets its input as a decimal number, not a binary number. Your longer string is causing the value to overflow, in which case the return value of atoi is undefined (although returning INT_MAX is common, which is equal to the value you're seeing).
To interpret the input as a binary number you can use strtol like this:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <limits.h>
int main()
{
char line[256];
while (printf("\nInput: "),
fgets(line, sizeof line, stdin))
{
// skip initial whitespace and break if input is nothing but whitespace
char *p = line;
while (isspace(*p)) ++p;
if (*p == '\0') break;
// interpret input as a binary number
long n = strtol(line, &p, 2);
if (p == line)
printf("** Can't convert any characters. **\n");
else if (n == LONG_MAX || n == LONG_MIN)
printf("** Range error **\n");
else
printf("Value: %ld\n", n); // prints value in decimal
while (isspace(*p)) ++p; // skip whitespace after the value
if (*p != '\0')
printf("** Excess characters in input **\n");
}
return 0;
}
Your input is too large for the return type. Per the docs:
If the converted value cannot be represented, the behavior is undefined.
An int is a signed type of at least 16-bits. On your platform it may be 32-bits and, in that case, its max value is 2^31-1. Too small for your input of ~11 billion. Use strtol, which returns a long int, instead.
Also, make sure you're terminating that string.
2147483647 (this value is for any value of k, I think it is random?)
No, it is not random; it is the largest value a signed 32-bit integer can hold.
Your inputs are too big.
When this happens, the return value of atoi is undefined (per the documentation); the result you're seeing is common in practice.
Define kbits to be one byte longer then the number of 1s you want to store and do initialise it to all 0s.
#define K (5)
char kbits[K + 1] = ""; /* One more for the `0`temrination neseccary to terminate a C-"string". */
I am looking for a (relatively) simple way to parse a random string and extract all of the integers from it and put them into an Array - this differs from some of the other questions which are similar because my strings have no standard format.
Example:
pt112parah salin10n m5:isstupid::42$%&%^*%7first3
I would need to eventually get an array with these contents:
112 10 5 42 7 3
And I would like a method more efficient then going character by character through a string.
Thanks for your help
A quick solution. I'm assuming that there are no numbers that exceed the range of long, and that there are no minus signs to worry about. If those are problems, then you need to do a lot more work analyzing the results of strtol() and you need to detect '-' followed by a digit.
The code does loop over all characters; I don't think you can avoid that. But it does use strtol() to process each sequence of digits (once the first digit is found), and resumes where strtol() left off (and strtol() is kind enough to tell us exactly where it stopped its conversion).
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
int main(void)
{
const char data[] = "pt112parah salin10n m5:isstupid::42$%&%^*%7first3";
long results[100];
int nresult = 0;
const char *s = data;
char c;
while ((c = *s++) != '\0')
{
if (isdigit(c))
{
char *end;
results[nresult++] = strtol(s-1, &end, 10);
s = end;
}
}
for (int i = 0; i < nresult; i++)
printf("%d: %ld\n", i, results[i]);
return 0;
}
Output:
0: 112
1: 10
2: 5
3: 42
4: 7
5: 3
More efficient than going through character by character?
Not possible, because you must look at every character to know that it is not an integer.
Now, given that you have to go though the string character by character, I would recommend simply casting each character as an int and checking that:
//string tmp = ""; declared outside of loop.
//pseudocode for inner loop:
int intVal = (int)c;
if(intVal >=48 && intVal <= 57){ //0-9 are 48-57 when char casted to int.
tmp += c;
}
else if(tmp.length > 0){
array[?] = (int)tmp; // ? is where to add the int to the array.
tmp = "";
}
array will contain your solution.
Just because I've been writing Python all day and I want a break. Declaring an array will be tricky. Either you have to run it twice to work out how many numbers you have (and then allocate the array) or just use the numbers one by one as in this example.
NB the ASCII characters for '0' to '9' are 48 to 57 (i.e. consecutive).
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
int main(int argc, char **argv)
{
char *input = "pt112par0ah salin10n m5:isstupid::42$%&%^*%7first3";
int length = strlen(input);
int value = 0;
int i;
bool gotnumber = false;
for (i = 0; i < length; i++)
{
if (input[i] >= '0' && input[i] <= '9')
{
gotnumber = true;
value = value * 10; // shift up a column
value += input[i] - '0'; // casting the char to an int
}
else if (gotnumber) // we hit this the first time we encounter a non-number after we've had numbers
{
printf("Value: %d \n", value);
value = 0;
gotnumber = false;
}
}
return 0;
}
EDIT: the previous verison didn't deal with 0
Another solution is to use the strtok function
/* strtok example */
#include <stdio.h>
#include <string.h>
int main ()
{
char str[] = "pt112parah salin10n m5:isstupid::42$%&%^*%7first3";
char * pch;
printf ("Splitting string \"%s\" into tokens:\n",str);
pch = strtok (str," abcdefghijklmnopqrstuvwxyz:$%&^*");
while (pch != NULL)
{
printf ("%s\n",pch);
pch = strtok (NULL, " abcdefghijklmnopqrstuvwxyz:$%&^*");
}
return 0;
}
Gives:
112
10
5
42
7
3
Perhaps not the best solution for this task, since you need to specify all characters that will be treated as a token. But it is an alternative to the other solutions.
And if you don't mind using C++ instead of C (usually there isn't a good reason why not), then you can reduce your solution to just two lines of code (using AXE parser generator):
vector<int> numbers;
auto number_rule = *(*(axe::r_any() - axe::r_num())
& *axe::r_num() >> axe::e_push_back(numbers));
now test it:
std::string str = "pt112parah salin10n m5:isstupid::42$%&%^*%7first3";
number_rule(str.begin(), str.end());
std::for_each(numbers.begin(), numbers.end(), [](int i) { std::cout << "\ni=" << i; });
and sure enough, you got your numbers back.
And as a bonus, you don't need to change anything when parsing unicode wide strings:
std::wstring str = L"pt112parah salin10n m5:isstupid::42$%&%^*%7first3";
number_rule(str.begin(), str.end());
std::for_each(numbers.begin(), numbers.end(), [](int i) { std::cout << "\ni=" << i; });
and sure enough, you got the same numbers back.
#include <stdio.h>
#include <string.h>
#include <math.h>
int main(void)
{
char *input = "pt112par0ah salin10n m5:isstupid::42$%&%^*%7first3";
char *pos = input;
int integers[strlen(input) / 2]; // The maximum possible number of integers is half the length of the string, due to the smallest number of digits possible per integer being 1 and the smallest number of characters between two different integers also being 1
unsigned int numInts= 0;
while ((pos = strpbrk(pos, "0123456789")) != NULL) // strpbrk() prototype in string.h
{
sscanf(pos, "%u", &(integers[numInts]));
if (integers[numInts] == 0)
pos++;
else
pos += (int) log10(integers[numInts]) + 1; // requires math.h
numInts++;
}
for (int i = 0; i < numInts; i++)
printf("%d ", integers[i]);
return 0;
}
Finding the integers is accomplished via repeated calls to strpbrk() on the offset pointer, with the pointer being offset again by an amount equaling the number of digits in the integer, calculated by finding the base-10 logarithm of the integer and adding 1 (with a special case for when the integer is 0). No need to use abs() on the integer when calculating the logarithm, as you stated the integers will be non-negative. If you wanted to be more space-efficient, you could use unsigned char integers[] rather than int integers[], as you stated the integers will all be <256, but that isn't a necessity.