Added bin2hex
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
all: healthcheck findlongest entcheck hex2bin flipbits
|
||||
all: healthcheck findlongest entcheck hex2bin bin2hex flipbits
|
||||
|
||||
healthcheck: ../healthcheck.c
|
||||
gcc -Wall -std=c99 -O3 -D TEST_HEALTHCHECK -o healthcheck ../healthcheck.c -lm -lrt
|
||||
@@ -12,8 +12,11 @@ findlongest: findlongest.c
|
||||
hex2bin: hex2bin.c
|
||||
gcc -Wall -std=c99 -O3 -o hex2bin hex2bin.c
|
||||
|
||||
bin2hex: bin2hex.c
|
||||
gcc -Wall -std=c99 -O3 -o bin2hex bin2hex.c
|
||||
|
||||
flipbits: flipbits.c
|
||||
gcc -Wall -std=c99 -O3 -o flipbits flipbits.c
|
||||
|
||||
clean:
|
||||
rm -f healthcheck findlongest entcheck hex2bin flipbits
|
||||
rm -f healthcheck findlongest entcheck hex2bin bin2hex flipbits
|
||||
|
||||
15
software/tools/bin2hex.c
Normal file
15
software/tools/bin2hex.c
Normal file
@@ -0,0 +1,15 @@
|
||||
#include <stdio.h>
|
||||
|
||||
int main() {
|
||||
int value = getchar();
|
||||
int count = 0;
|
||||
while(value != EOF) {
|
||||
printf("%02x", value);
|
||||
value = getchar();
|
||||
if(++count == 64) {
|
||||
putchar('\n');
|
||||
}
|
||||
}
|
||||
putchar('\n');
|
||||
return 0;
|
||||
}
|
||||
@@ -1,17 +1,67 @@
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
int value = getchar();
|
||||
while(value != EOF) {
|
||||
int i, revVal = 0;
|
||||
for(i = 0; i < 8; i++) {
|
||||
revVal <<= 1;
|
||||
revVal |= value & 1;
|
||||
value >>= 1;
|
||||
static uint8_t availableInBits = 0;
|
||||
static uint8_t availableOutBits = 0;
|
||||
static uint8_t currentInByte = 0;
|
||||
static uint8_t currentOutByte = 0;
|
||||
|
||||
// Read a width-bits value from the input.
|
||||
static uint64_t readVal(uint8_t width, bool *endOfInput) {
|
||||
*endOfInput = false;
|
||||
uint64_t value = 0;
|
||||
uint8_t mask = 1 << (availableInBits-1);
|
||||
while(width--) {
|
||||
value <<= 1;
|
||||
if(availableInBits == 0) {
|
||||
int c = getchar();
|
||||
if(c == EOF) {
|
||||
*endOfInput = true;
|
||||
return value;
|
||||
}
|
||||
currentInByte = c;
|
||||
availableInBits = 8;
|
||||
mask = 1 << 7;
|
||||
}
|
||||
putchar(revVal);
|
||||
value = getchar();
|
||||
if(currentInByte & mask) {
|
||||
value |= 1;
|
||||
}
|
||||
mask >>= 1;
|
||||
availableInBits--;
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
// Write out the value in reverse order.
|
||||
static void writeValReverse(uint64_t value, uint8_t width) {
|
||||
while(width--) {
|
||||
currentOutByte <<= 1;
|
||||
currentOutByte |= value & 1;
|
||||
value >>= 1;
|
||||
availableOutBits++;
|
||||
if(availableOutBits == 8) {
|
||||
putchar(currentOutByte);
|
||||
availableOutBits = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
uint8_t width = 8;
|
||||
if(argc == 2) {
|
||||
width = atoi(argv[1]);
|
||||
}
|
||||
if(width == 0 || width > 64 || argc > 2) {
|
||||
fprintf(stderr, "Usage: flipbits [width]\n");
|
||||
return 1;
|
||||
}
|
||||
bool endOfInput;
|
||||
uint64_t value = readVal(width, &endOfInput);
|
||||
while(!endOfInput) {
|
||||
writeValReverse(value, width);
|
||||
value = readVal(width, &endOfInput);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user