C

Aus brainelectronics Wiki
Wechseln zu: Navigation, Suche

Tricks

Integer

Zwei uint8_t kombinieren

Soll aus zwei uint8_t ein "großer" uint16_t entstechen Combining two uint8_t as uint16_t

uint8_t d1=0x01; 
uint8_t d2=0x02; 

uint16_t wd = ((uint16_t)d2 << 8) | d1;

uint16_t wd = 0x0201;
 

atoi

Eine Char array in einen Integer umwandeln C++ Referenz

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

int main()
{
    int val;
    char str[20];

    // convert char array to int
    strcpy(str, "1234567890");
    val = atoi(str);
    printf("String value = %s\n", str);
    printf("Int value = %d\n", val);

    // convert single char to int
    val = str[strlen(str)-3]-'0';
    printf("Single Int value = %d\n", val);

    strcpy(str, "testBla");
    val = atoi(str);
    printf("String value = %s\n", str);
    printf("Int value = %d\n", val);

    return(0);
}
 
String value = 1234567890
Int value = 1234567890
Single Int value = 8
String value = testBla
Int value = 0
 

itoa

Einen Integer in ein char array umwandeln C++ Referenz

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

int main()
{
    int a=54325;
    char buffer[20];
    itoa(a,buffer,2);   // here 2 means binary
    printf("Binary value = %s\n", buffer);

    itoa(a,buffer,10);   // here 10 means decimal
    printf("Decimal value = %s\n", buffer);

    itoa(a,buffer,16);   // here 16 means Hexadecimal
    printf("Hexadecimal value = %s\n", buffer);

    return 0;
}

Binary value = 1101010000110101
Decimal value = 54325
Hexadecimal value = D435
 

Strings

strchr

Finde char in char array C++ ReferenzStackoverflow Frage

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
 
int main()
{
    char str[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    char *firstPart = (char *)malloc(strlen(str));;
    char *secondPart = (char *)malloc(strlen(str));;
    char find = 'E';

    const char *ptr = strchr(str, find);
    if(ptr)
    {
        int index = ptr - str;
        printf("Found at index '%d'\n", index);

        // copy everything up to 'find'
        strncpy(firstPart, str, index);
        printf("String up to found: '%s'\n", firstPart);

        // copy everything after 'find'
        int remainingChars = strlen(str) - index;
        strncpy(secondPart, str+index, remainingChars);
        printf("String after found (copy usage): '%s'\n", secondPart);
        printf("String after found (pointer usage): '%s'\n", ptr);
    }

    return 0;
}
 
Found at index '14'
String up to found: '0123456789ABCD'
String after found: 'FGHIJKLMNOPQRSTUVWXYZ'
 


strcmp/strncmp

Vergleiche char array mit anderem char array C++ Referenz strcmpC++ Referenz strncmp
Die Werte sind dabei 0 falls beide identisch sind, -n mit n als "Abstand" des ersten unterschiedlichen Zeichen gegenüber des anderen char array, respektive +n falls dieser "Abstand" positiv ist, also z.B. für strncmp("aaDaa", "aaAaa", 3) liefert es 3

#include <stdio.h>
#include <string.h>

int main()
{
    char str1[] = "aaXaa";
    char str2[] = "aaZaa";

    printf("Comparing the first 2 chars: %d\n", strncmp(str1, str2, 2));
    printf("Comparing the first 3 chars: %d\n", strncmp(str1, str2, 3));

    return(0);
}
 
Comparing the first 2 chars: 0
omparing the first 3 chars: -2
 

strcpy/strncpy

Finde char in char array C++ Referenz strcpyC++ Referenz strncpy

#include <stdio.h>
#include <string.h>

int main()
{
    char src[40];
    char part[12];

    memset(part, '\0', sizeof(part));

    strcpy(src, "test string bla asdf");
    printf("Copied string: '%s'\n", src);

    strncpy(part, src, 11);
    printf("Partly copied string: '%s'\n", part);

    return(0);
}
 
Copied string: 'test string bla asdf'
Partly copied string: 'test string'
 

strtok

Finde char in char array C++ Referenz

#include <string.h>
#include <stdio.h>

int main()
{
   char str[80] = "This is - an easy - example";
   const char s[2] = "-";
   char *token;
   
   // get the first token
   token = strtok(str, s);
   
   // walk through other tokens
   while(token != NULL) 
   {
      printf("%s\n", token);
    
      token = strtok(NULL, s);
   }
   
   return(0);
}
 
This is 
 an easy 
 example
 

Credits