Skip to content

Instantly share code, notes, and snippets.

@Estecka
Created January 16, 2020 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Estecka/e91dbe1f2beaf46b7d12f4921748269f to your computer and use it in GitHub Desktop.
Save Estecka/e91dbe1f2beaf46b7d12f4921748269f to your computer and use it in GitHub Desktop.
Converts the first few characters of a string into an int, for easier comparison.
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* .stringToInt.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: abaur <abaur@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/01/16 14:15:40 by abaur #+# #+# */
/* Updated: 2020/01/16 14:25:04 by abaur ### ########.fr */
/* */
/* ************************************************************************** */
#include <ctype.h>
#include <stdio.h>
int stringToInt(char* line)
{
int result = 0;
for(int i=0; i<4; i++)
{
if (!line[i])
break;
result |= line[i] << (i * 8);
}
return result;
}
int main(int argc, char **args)
{
for (int i=1; i<argc; i++)
printf ("%-4s %d\n", args[i], stringToInt(args[i]));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment