int main() { char str[] = "7DE"; //enter Hex number in UPPERCASE //perform convert function & print result printf("hex number: %d", convertHexToInt(str)); return 0; }
void fetchKey(char* key, unsigned char keyBytes[16]) { int i=0; for(i=0; i<=30; i+=2) { char next[3] = {key[i], key[i+1], '\0'}; keyBytes[i/2] = convertHexToInt(next); } }
void computeModprod(char* poly1, char* poly2) { //check if length of hexstring is 8 if(strlen(poly1) != 8 || strlen(poly2) != 8) { fprintf(stderr, "Either poly1 or poly2 or both are not represented by a hexstring of length 8.\n"); return; } else { int k=0; for(k=0; k<8; k++) //check if poly1 contains valid hex symbols { if(poly1[k] < 48 || (poly1[k] > 57 && poly1[k] < 97) || poly1[k] > 102) { fprintf(stderr, "poly1 is not in hexstring format.\n"); return; } } for(k=0; k<8; k++) //check if poly2 contains valid hex symbols { if(poly2[k] < 48 || (poly2[k] > 57 && poly2[k] < 97) || poly2[k] > 102) { fprintf(stderr, "poly2 is not in hexstring format.\n"); return; } } } //convert hexstring to int unsigned int a = convertHexToInt(poly1); unsigned int b = convertHexToInt(poly2); unsigned result = circleX(a, b); //print results unsigned char d3 = (result & (255<<24)) >> 24; unsigned char d2 = (result & (255<<16)) >> 16; unsigned char d1 = (result & (255<<8)) >> 8; unsigned char d0 = (result & 255); printf("{%c%c}{%c%c}{%c%c}{%c%c} CIRCLEX {%c%c}{%c%c}{%c%c}{%c%c} = {%02x}{%02x}{%02x}{%02x}\n", poly1[0], poly1[1], poly1[2], poly1[3], poly1[4], poly1[5], poly1[6], poly1[7], poly2[0], poly2[1], poly2[2], poly2[3], poly2[4], poly2[5], poly2[6], poly2[7], d3, d2, d1, d0); }
void fetchFromTable(char* tablefileName, unsigned char S[16][16], unsigned int* P, unsigned int* INVP) { FILE *tableFile = fopen(tablefileName, "rb"); char line[600]; char *keyword; while(fgets(line, 600, tableFile) != NULL) //Read 1 line at a time { //Get table name keyword = strtok(line, "="); //Get values char *value; value = strtok(NULL, "="); if(strcmp(keyword, "S") == 0) { int j=0; for(j=0; j<=510; j+=2) { char next[3] = {value[j], value[j+1], '\0'}; S[j/32][(j%32)/2] = convertHexToInt(next); } } if(strcmp(keyword, "P") == 0 || strcmp(keyword, "INVP") == 0) { value[8] = '\0'; //eliminate \n at the end so that convertHexToInt() will work properly //P (or INVP) is valid if(strcmp(keyword, "P") == 0) *P = convertHexToInt(value); else *INVP = convertHexToInt(value); } } }
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); QString n = "(?:[0-1]?[0-9]?[0-9]|2[0-4][0-9]|25[0-5])"; ipValidator = new QRegExpValidator(QRegExp("^" + n + "\\." + n + "\\." + n + "\\." + n + "$")); intValidator = new QRegExpValidator(QRegExp("[0-9]+")); hexValidator = new QRegExpValidator(QRegExp("[0-9a-fA-F]+")); base64Validator = new QRegExpValidator(QRegExp("^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$")); setIpValidator(); machine = new QStateMachine(this); QState *ipToInt = new QState(); QState *intToHex = new QState(); QState *hexToBase64 = new QState(); QState *base64ToHex = new QState(); QState *hexToInt = new QState(); QState *intToIp = new QState(); ipToInt->addTransition(ui->pushButton, SIGNAL(clicked()), intToHex); intToHex->addTransition(ui->pushButton, SIGNAL(clicked()), hexToBase64); hexToBase64->addTransition(ui->pushButton, SIGNAL(clicked()), base64ToHex); base64ToHex->addTransition(ui->pushButton, SIGNAL(clicked()), hexToInt); hexToInt->addTransition(ui->pushButton, SIGNAL(clicked()), intToIp); intToIp->addTransition(ui->pushButton, SIGNAL(clicked()), ipToInt); QObject::connect(ipToInt, SIGNAL(exited()), this, SLOT(convertIpToInt())); QObject::connect(intToHex, SIGNAL(exited()), this, SLOT(convertIntToHex())); QObject::connect(hexToBase64, SIGNAL(exited()), this, SLOT(convertHexToBase64())); QObject::connect(base64ToHex, SIGNAL(exited()), this, SLOT(convertBase64ToHex())); QObject::connect(hexToInt, SIGNAL(exited()), this, SLOT(convertHexToInt())); QObject::connect(intToIp, SIGNAL(exited()), this, SLOT(convertIntToIp())); machine->addState(ipToInt); machine->addState(intToHex); machine->addState(hexToBase64); machine->addState(base64ToHex); machine->addState(hexToInt); machine->addState(intToIp); machine->setInitialState(ipToInt); machine->start(); }
void SerialInterface::readData(float deltaTime) { #ifdef __APPLE__ int initialSamples = totalSamples; if (USING_INVENSENSE_MPU9150) { unsigned char sensorBuffer[36]; // ask the invensense for raw gyro data write(_serialDescriptor, "RD683B0E\n", 9); read(_serialDescriptor, sensorBuffer, 36); int accelXRate, accelYRate, accelZRate; convertHexToInt(sensorBuffer + 6, accelZRate); convertHexToInt(sensorBuffer + 10, accelYRate); convertHexToInt(sensorBuffer + 14, accelXRate); const float LSB_TO_METERS_PER_SECOND2 = 1.f / 16384.f * GRAVITY_EARTH; // From MPU-9150 register map, with setting on // highest resolution = +/- 2G _lastAcceleration = glm::vec3(-accelXRate, -accelYRate, -accelZRate) * LSB_TO_METERS_PER_SECOND2; int rollRate, yawRate, pitchRate; convertHexToInt(sensorBuffer + 22, rollRate); convertHexToInt(sensorBuffer + 26, yawRate); convertHexToInt(sensorBuffer + 30, pitchRate); // Convert the integer rates to floats const float LSB_TO_DEGREES_PER_SECOND = 1.f / 16.4f; // From MPU-9150 register map, 2000 deg/sec. glm::vec3 rotationRates; rotationRates[0] = ((float) -pitchRate) * LSB_TO_DEGREES_PER_SECOND; rotationRates[1] = ((float) -yawRate) * LSB_TO_DEGREES_PER_SECOND; rotationRates[2] = ((float) -rollRate) * LSB_TO_DEGREES_PER_SECOND; // update and subtract the long term average _averageRotationRates = (1.f - 1.f/(float)LONG_TERM_RATE_SAMPLES) * _averageRotationRates + 1.f/(float)LONG_TERM_RATE_SAMPLES * rotationRates; rotationRates -= _averageRotationRates; // compute the angular acceleration glm::vec3 angularAcceleration = (deltaTime < EPSILON) ? glm::vec3() : (rotationRates - _lastRotationRates) / deltaTime; _lastRotationRates = rotationRates; // Update raw rotation estimates glm::quat estimatedRotation = glm::quat(glm::radians(_estimatedRotation)) * glm::quat(glm::radians(deltaTime * _lastRotationRates)); // Update acceleration estimate: first, subtract gravity as rotated into current frame _estimatedAcceleration = (totalSamples < GRAVITY_SAMPLES) ? glm::vec3() : _lastAcceleration - glm::inverse(estimatedRotation) * _gravity; // update and subtract the long term average _averageAcceleration = (1.f - 1.f/(float)LONG_TERM_RATE_SAMPLES) * _averageAcceleration + 1.f/(float)LONG_TERM_RATE_SAMPLES * _estimatedAcceleration; _estimatedAcceleration -= _averageAcceleration; // Consider updating our angular velocity/acceleration to linear acceleration mapping if (glm::length(_estimatedAcceleration) > EPSILON && (glm::length(_lastRotationRates) > EPSILON || glm::length(angularAcceleration) > EPSILON)) { // compute predicted linear acceleration, find error between actual and predicted glm::vec3 predictedAcceleration = _angularVelocityToLinearAccel * _lastRotationRates + _angularAccelToLinearAccel * angularAcceleration; glm::vec3 error = _estimatedAcceleration - predictedAcceleration; // the "error" is actually what we want: the linear acceleration minus rotational influences _estimatedAcceleration = error; // adjust according to error in each dimension, in proportion to input magnitudes for (int i = 0; i < 3; i++) { if (fabsf(error[i]) < EPSILON) { continue; } const float LEARNING_RATE = 0.001f; float rateSum = fabsf(_lastRotationRates.x) + fabsf(_lastRotationRates.y) + fabsf(_lastRotationRates.z); if (rateSum > EPSILON) { for (int j = 0; j < 3; j++) { float proportion = LEARNING_RATE * fabsf(_lastRotationRates[j]) / rateSum; if (proportion > EPSILON) { _angularVelocityToLinearAccel[j][i] += error[i] * proportion / _lastRotationRates[j]; } } } float accelSum = fabsf(angularAcceleration.x) + fabsf(angularAcceleration.y) + fabsf(angularAcceleration.z); if (accelSum > EPSILON) { for (int j = 0; j < 3; j++) { float proportion = LEARNING_RATE * fabsf(angularAcceleration[j]) / accelSum; if (proportion > EPSILON) { _angularAccelToLinearAccel[j][i] += error[i] * proportion / angularAcceleration[j]; } } } } } // rotate estimated acceleration into global rotation frame _estimatedAcceleration = estimatedRotation * _estimatedAcceleration; // Update estimated position and velocity float const DECAY_VELOCITY = 0.975f; float const DECAY_POSITION = 0.975f; _estimatedVelocity += deltaTime * _estimatedAcceleration; _estimatedPosition += deltaTime * _estimatedVelocity; _estimatedVelocity *= DECAY_VELOCITY; // Attempt to fuse gyro position with webcam position Webcam* webcam = Application::getInstance()->getWebcam(); if (webcam->isActive()) { const float WEBCAM_POSITION_FUSION = 0.5f; _estimatedPosition = glm::mix(_estimatedPosition, webcam->getEstimatedPosition(), WEBCAM_POSITION_FUSION); } else { _estimatedPosition *= DECAY_POSITION; } // Accumulate a set of initial baseline readings for setting gravity if (totalSamples == 0) { _gravity = _lastAcceleration; } else { if (totalSamples < GRAVITY_SAMPLES) { _gravity = (1.f - 1.f/(float)GRAVITY_SAMPLES) * _gravity + 1.f/(float)GRAVITY_SAMPLES * _lastAcceleration; } else { // Use gravity reading to do sensor fusion on the pitch and roll estimation estimatedRotation = safeMix(estimatedRotation, rotationBetween(estimatedRotation * _lastAcceleration, _gravity) * estimatedRotation, 1.0f / SENSOR_FUSION_SAMPLES); // Without a compass heading, always decay estimated Yaw slightly const float YAW_DECAY = 0.999f; glm::vec3 forward = estimatedRotation * glm::vec3(0.0f, 0.0f, -1.0f); estimatedRotation = safeMix(glm::angleAxis(glm::degrees(atan2f(forward.x, -forward.z)), glm::vec3(0.0f, 1.0f, 0.0f)) * estimatedRotation, estimatedRotation, YAW_DECAY); } } _estimatedRotation = safeEulerAngles(estimatedRotation); totalSamples++; } if (initialSamples == totalSamples) { timeval now; gettimeofday(&now, NULL); if (diffclock(&lastGoodRead, &now) > NO_READ_MAXIMUM_MSECS) { printLog("No data - Shutting down SerialInterface.\n"); resetSerial(); } } else { gettimeofday(&lastGoodRead, NULL); } #endif }