Problem converting GUID strings to hexadecimal in C [DUVIDA]
by backtrack5R1 from LinuxQuestions.org on (#6MW6D)
I am facing difficulties in implementing a function in C to convert GUID strings into hexadecimal representations. The function I am using is the following:
Code:#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdint.h>
#define MAX_GUID_STRING_LENGTH 39
typedef struct {
unsigned int data1;
unsigned short data2;
unsigned short data3;
unsigned char data4[8];
} GUID;
void stringToHexGuid(const char *input, char *output)
{
unsigned int dword1, dword2, dword3;
unsigned int byte1, byte2, byte3, byte4, byte5, byte6, byte7, byte8, byte9, byte10, byte11;
sscanf(input, "{%8X-%4X-%4X-%2X%2X-%2X%2X%2X%2X%2X%2X}", &dword1, &dword2, &dword3,
&byte1, &byte2, &byte3, &byte4, &byte5, &byte6, &byte7, &byte8);
byte9 = (byte2 << 8) + byte3;
byte10 = (byte4 << 8) + byte5;
byte11 = (byte6 << 8) + byte7;
snprintf(output, 100, "0x%08X, 0x%04X, 0x%04X, 0x%02X, 0x%02X, 0x%02X, 0x%02X, 0x%02X, 0x%02X, 0x%02X, 0x%02X",
dword1, dword2, dword3, byte9, byte10, byte7, byte8, byte9, byte10, byte11, byte8);
}This function receives the inputs:
Code:7FFEC5C92D0049B789413EA10A5586B7
7FFEC5C9-2D00-49B7-8941-3EA10A5586B7
{7FFEC5C9-2D00-49B7-8941-3EA10A5586B7}and should convert the string to an exadecimal GUID structure as well as the original GUID, reaching the following result:
Code:{0x7FFEC5C9, 0x2D00, 0x49B7, 0x89, 0x41, 0x3E, 0xA1, 0x0A, 0x55, 0x86, 0xB7}but the function does not correctly convert to hexadecimal and produces the following results:
Code://origen: {0x7FFEC5C9, 0x2D00, 0x49B7, 0x89, 0x41, 0x3E, 0xA1, 0x0A, 0x55, 0x86, 0xB7}
//error: 0x7FFEC5C9, 0x2D00, 0x49B7, 0x413E, 0xA10A, 0x86, 0xB7, 0x413E, 0xA10A, 0x5586, 0xB7
//error: 0x7FFEC5C9, 0x2D00, 0x49B7, 0x8D1, 0x7E1, 0x86, 0xB7, 0x8D1, 0x7E1, 0x2D5, 0xB7
//error: 0x7FFEC5C9, 0x2D00, 0x49B7, 0x8D1, 0x3E1, 0x86, 0xB7, 0x8D1, 0x3E1, 0xF5, 0xB7
//error: 0x7FFEC5C9, 0x2D00, 0x49B7, 0x89, 0x3E, 0x86, 0xB7, 0x89, 0x3E, 0x0A, 0xB7
//error: 0x7FFEC5C9, 0x2D00, 0x49B7, 0x41, 0x3E, 0x86, 0xB7, 0x41, 0x3E, 0xA1, 0xB7
//error: 0x7FFEC5C9, 0x2D00, 0x49B7, 0x41, 0x3E, 0x86, 0xB7, 0x41, 0x3E, 0xA1, 0xB7
//error: 0x7FFEC5C9, 0x2D00, 0x49B7, 0x89, 0x41, 0x86, 0xB7, 0x89, 0x41, 0x3E, 0xB7
//error: 0x7FFEC5C9, 0x2D00, 0x49B7, 0x8941, 0xA155, 0x86, 0xB7, 0x8941, 0xA155, 0xB73E, 0xB7
//error: 0x7FFEC5C9, 0x2D00, 0x49B7, 0x893E, 0x410A, 0x86, 0xB7, 0x893E, 0x410A, 0xA155, 0xB7
//error: 0x7FFEC5C9, 0x2D00, 0x49B7, 0x413E, 0xA10A, 0x86, 0xB7, 0x413E, 0xA10A, 0x5586, 0xB7
//error: 0x7FFEC5C9, 0x2D00, 0x49B7, 0x8D1, 0x481, 0x86, 0xB7, 0x8D1, 0x481, 0xF5, 0xB7How do I convert the function to hexadecimal as it was in the original GUID?
Code:#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdint.h>
#define MAX_GUID_STRING_LENGTH 39
typedef struct {
unsigned int data1;
unsigned short data2;
unsigned short data3;
unsigned char data4[8];
} GUID;
void stringToHexGuid(const char *input, char *output)
{
unsigned int dword1, dword2, dword3;
unsigned int byte1, byte2, byte3, byte4, byte5, byte6, byte7, byte8, byte9, byte10, byte11;
sscanf(input, "{%8X-%4X-%4X-%2X%2X-%2X%2X%2X%2X%2X%2X}", &dword1, &dword2, &dword3,
&byte1, &byte2, &byte3, &byte4, &byte5, &byte6, &byte7, &byte8);
byte9 = (byte2 << 8) + byte3;
byte10 = (byte4 << 8) + byte5;
byte11 = (byte6 << 8) + byte7;
snprintf(output, 100, "0x%08X, 0x%04X, 0x%04X, 0x%02X, 0x%02X, 0x%02X, 0x%02X, 0x%02X, 0x%02X, 0x%02X, 0x%02X",
dword1, dword2, dword3, byte9, byte10, byte7, byte8, byte9, byte10, byte11, byte8);
}This function receives the inputs:
Code:7FFEC5C92D0049B789413EA10A5586B7
7FFEC5C9-2D00-49B7-8941-3EA10A5586B7
{7FFEC5C9-2D00-49B7-8941-3EA10A5586B7}and should convert the string to an exadecimal GUID structure as well as the original GUID, reaching the following result:
Code:{0x7FFEC5C9, 0x2D00, 0x49B7, 0x89, 0x41, 0x3E, 0xA1, 0x0A, 0x55, 0x86, 0xB7}but the function does not correctly convert to hexadecimal and produces the following results:
Code://origen: {0x7FFEC5C9, 0x2D00, 0x49B7, 0x89, 0x41, 0x3E, 0xA1, 0x0A, 0x55, 0x86, 0xB7}
//error: 0x7FFEC5C9, 0x2D00, 0x49B7, 0x413E, 0xA10A, 0x86, 0xB7, 0x413E, 0xA10A, 0x5586, 0xB7
//error: 0x7FFEC5C9, 0x2D00, 0x49B7, 0x8D1, 0x7E1, 0x86, 0xB7, 0x8D1, 0x7E1, 0x2D5, 0xB7
//error: 0x7FFEC5C9, 0x2D00, 0x49B7, 0x8D1, 0x3E1, 0x86, 0xB7, 0x8D1, 0x3E1, 0xF5, 0xB7
//error: 0x7FFEC5C9, 0x2D00, 0x49B7, 0x89, 0x3E, 0x86, 0xB7, 0x89, 0x3E, 0x0A, 0xB7
//error: 0x7FFEC5C9, 0x2D00, 0x49B7, 0x41, 0x3E, 0x86, 0xB7, 0x41, 0x3E, 0xA1, 0xB7
//error: 0x7FFEC5C9, 0x2D00, 0x49B7, 0x41, 0x3E, 0x86, 0xB7, 0x41, 0x3E, 0xA1, 0xB7
//error: 0x7FFEC5C9, 0x2D00, 0x49B7, 0x89, 0x41, 0x86, 0xB7, 0x89, 0x41, 0x3E, 0xB7
//error: 0x7FFEC5C9, 0x2D00, 0x49B7, 0x8941, 0xA155, 0x86, 0xB7, 0x8941, 0xA155, 0xB73E, 0xB7
//error: 0x7FFEC5C9, 0x2D00, 0x49B7, 0x893E, 0x410A, 0x86, 0xB7, 0x893E, 0x410A, 0xA155, 0xB7
//error: 0x7FFEC5C9, 0x2D00, 0x49B7, 0x413E, 0xA10A, 0x86, 0xB7, 0x413E, 0xA10A, 0x5586, 0xB7
//error: 0x7FFEC5C9, 0x2D00, 0x49B7, 0x8D1, 0x481, 0x86, 0xB7, 0x8D1, 0x481, 0xF5, 0xB7How do I convert the function to hexadecimal as it was in the original GUID?