LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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


Reply
  Search this Thread
Old 05-16-2024, 06:28 PM   #1
backtrack5R1
LQ Newbie
 
Registered: Jul 2012
Posts: 8

Rep: Reputation: Disabled
Post 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?
 
Old 05-17-2024, 02:44 AM   #2
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,880
Blog Entries: 1

Rep: Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871
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.
 
Old 05-17-2024, 03:20 AM   #3
GazL
LQ Veteran
 
Registered: May 2008
Posts: 6,918

Rep: Reputation: 5035Reputation: 5035Reputation: 5035Reputation: 5035Reputation: 5035Reputation: 5035Reputation: 5035Reputation: 5035Reputation: 5035Reputation: 5035Reputation: 5035
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!
 
Old 05-17-2024, 07:28 AM   #4
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,786

Rep: Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083
Quote:
Originally Posted by backtrack5R1 View Post
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] GPT GUID's accidentally randomized - is this a problem? dkeith Linux - Hardware 2 03-19-2015 08:33 PM
Script for converting Hexadecimal IP to Decimal IP PKrishna Linux - Newbie 5 07-04-2008 07:49 PM
winbind Active directory guid mapping problem fc6 BarryLinux Linux - Software 3 05-02-2007 08:36 AM
how to find duplicate strings in vertical column of strings markhod Programming 7 11-02-2005 04:04 AM
converting integer value to hexadecimal string in C - any suggestions?? woodywellhung Programming 3 04-24-2004 05:27 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:38 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration