#include <stdio.h>
#include <string.h>
#define BUFFER_SIZE 1024
void convert( const char * input) {
char buffer[ BUFFER_SIZE] ;
int index = 0 ;
int ch;
// Simulate reading from input string
for ( int i
= 0 ; i
< strlen ( input
) ; i
++ ) { ch = input[ i] ; // Read a character from the input string
if ( ch == '\0 ' ) { // Check for null terminator
break ;
}
if ( ch == '\n ' ) { // Check for newline
buffer[ index] = '\0 ' ; // Null-terminate the string
printf ( "Processed line: %s\n " , buffer
) ; // Print the processed line index = 0 ; // Reset index for the next line
continue ;
}
if ( index < BUFFER_SIZE - 1 ) { // Ensure we don't exceed buffer size
buffer[ index++ ] = ch; // Store character in buffer
} else {
buffer[ index] = '\0 ' ; // Null-terminate the string
printf ( "Buffer full, processed line: %s\n " , buffer
) ; // Print the processed line index = 0 ; // Reset index for the next line
}
if ( ch == 'x' ) { // Check for 'x'
printf ( "Found 'x' at index %d\n " , index
- 1 ) ; // Print index of 'x' }
}
// Check if there's any remaining data in the buffer after processing
if ( index > 0 ) {
buffer[ index] = '\0 ' ; // Null-terminate the string
printf ( "Processed line: %s\n " , buffer
) ; // Print the processed line }
}
int main( ) {
const char * input = "1020\n " ; // Input string containing the number 1020
printf ( "Processing input: %s" , input
) ; convert( input) ; // Call the convert function with the input string
return 0 ; // Return 0 upon completion
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxzdHJpbmcuaD4KCiNkZWZpbmUgQlVGRkVSX1NJWkUgMTAyNAoKdm9pZCBjb252ZXJ0KGNvbnN0IGNoYXIgKmlucHV0KSB7CiAgICBjaGFyIGJ1ZmZlcltCVUZGRVJfU0laRV07CiAgICBpbnQgaW5kZXggPSAwOwogICAgaW50IGNoOwoKICAgIC8vIFNpbXVsYXRlIHJlYWRpbmcgZnJvbSBpbnB1dCBzdHJpbmcKICAgIGZvciAoaW50IGkgPSAwOyBpIDwgc3RybGVuKGlucHV0KTsgaSsrKSB7CiAgICAgICAgY2ggPSBpbnB1dFtpXTsgIC8vIFJlYWQg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HsKICAgICAgICBhW2ldPXN0cnRvbCgmYW1wO2hleCwwLDE2KTsKCQlpKys7CiAgICB9CglpPTA7CglwcmludGYoJnF1b3Q7Mi5aZWlsZSBlaW5sZXNlbi5cbiZxdW90Oyk7CgogCXdoaWxlKChoZXg9ZmdldGMoZnApKSE9J1xuJyAmYW1wOyZhbXA7IGhleCAhPSBFT0YpCiAgICB7CiAgICAJYltpXT1zdHJ0b2woJmFtcDtoZXgsMCwxNik7CiAgICAgICAgaSsrOwogICAgfQoJZmNsb3NlKGZwKTsKCglwcmludGYoJnF1b3Q7SGFtbWluZy1BYndlaWNodW5nIHBybyBOaWJibGU6XG4mcXVvdDspOwoJZm9yIChpID0gMDsgaSAmbHQ7IEFSUkFZX1NJWkU7ICsraSkKCXsKCQlwcmludGYgKCZxdW90OyVpXHQlaVx0JWlcbiZxdW90OyxhW2ldLGJbaV0sSGFtZGlzdChhW2ldLGJbaV0pKTsKCQloYW1EaXN0ICs9IEhhbWRpc3QoYVtpXSxiW2ldKTsKCX0KCXByaW50ZiAoJnF1b3Q7XG5IYW1taW5nLUFid2VpY2h1bmcgZGVyIEhhc2gtV2VydGU6JWRcbiZxdW90OyxoYW1EaXN0KTsKfQoK
/* Berechnung des Hamming-Abstandes zwischen zwei 128-Bit Werten in */
/* einer Textdatei. */
/* Die Werte müssen auf einer separaten Zeile gespeichert sein */
/* */
/* Erstellt: 17.5.2010 */
/* Autor: Thomas Scheffler */
#include <stdio.h>
#include <stdlib.h>
#define ARRAY_SIZE 32
unsigned Hamdist(unsigned x, unsigned y)
{
unsigned dist = 0, val = x ^ y;
// Count the number of set bits
while(val)
{
++dist;
val &= val - 1;
}
return dist;
}
int main (void)
{
char hex;
int i;
int a[ARRAY_SIZE];
int b[ARRAY_SIZE];
int hamDist = 0;
FILE* fp;
//Arrays mit 0 initialisieren
for (i = 0; i < ARRAY_SIZE; ++i)
{
a[i] = 0;
b[i] = 0;
}
fp = fopen("hex.txt","r");
if (fp == NULL)
{
printf("Die Datei hex.txt wurde nicht gefunden!");
exit(EXIT_FAILURE);
}
i=0;
printf("1.Zeile einlesen.\n");
while((hex=fgetc(fp))!='\n' && hex != EOF)
{
a[i]=strtol(&hex,0,16);
i++;
}
i=0;
printf("2.Zeile einlesen.\n");
while((hex=fgetc(fp))!='\n' && hex != EOF)
{
b[i]=strtol(&hex,0,16);
i++;
}
fclose(fp);
printf("Hamming-Abweichung pro Nibble:\n");
for (i = 0; i < ARRAY_SIZE; ++i)
{
printf ("%i\t%i\t%i\n",a[i],b[i],Hamdist(a[i],b[i]));
hamDist += Hamdist(a[i],b[i]);
}
printf ("\nHamming-Abweichung der Hash-Werte:%d\n",hamDist);
}