Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game. |
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
05-16-2024, 06:28 PM
|
#1
|
LQ Newbie
Registered: Jul 2012
Posts: 8
Rep:
|
Problem converting GUID strings to hexadecimal in C [DUVIDA]
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, 0xB7
How do I convert the function to hexadecimal as it was in the original GUID?
|
|
|
05-17-2024, 02:44 AM
|
#2
|
Senior Member
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,923
|
First remove every syntactical character ({-}) then check the length (32) and the characters one-by-one (0-9a-fA-F), and finally do the conversion, the output should be `uint8_t[16]` or similar.
|
|
|
05-17-2024, 03:20 AM
|
#3
|
LQ Veteran
Registered: May 2008
Posts: 7,002
|
I really would have expected the placement of the hyphen delimiters to match the layout/size of the members of the GUID struct, but apparently not. Microsoft are weird!
|
|
|
05-17-2024, 07:28 AM
|
#4
|
Senior Member
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,793
|
Quote:
Originally Posted by backtrack5R1
Code:
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);
}
|
I don't understand why you are splitting up and then combining bytes like that, but it looks like you skipped byte1 and are using byte7, byte9, byte10, byte11, and byte8 twice.
|
|
|
All times are GMT -5. The time now is 03:42 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|