473,804 Members | 3,182 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Convert hex string to struct

Anyone have some code to help me understand how I can convert a given
hex string, say "0009dbaa00004c 00000000fb82ca6 21c," into a struct of
this form:

struct uniqueid {
ulong32_t word1;
ulong32_t word2;
ulong32_t word3;
ulong32_t word4;
};

Thanks very much.

-Ross

Nov 15 '05 #1
4 6436


Ross wrote:
Anyone have some code to help me understand how I can convert a given
hex string, say "0009dbaa00004c 00000000fb82ca6 21c," into a struct of
this form:

struct uniqueid {
ulong32_t word1;
ulong32_t word2;
ulong32_t word3;
ulong32_t word4;
};

Thanks very much.

-Ross


Nov 15 '05 #2
Ross wrote:
Anyone have some code to help me understand how I can convert a given
hex string, say "0009dbaa00004c 00000000fb82ca6 21c," into a struct of
this form:

struct uniqueid {
ulong32_t word1;
ulong32_t word2;
ulong32_t word3;
ulong32_t word4;
};

Thanks very much.

-Ross


#include <stdio.h>
#include <string.h>
#include <stdlib.h>

struct uniqueid
{
unsigned long word1;
unsigned long word2;
unsigned long word3;
unsigned long word4;
};

int main(void)
{
char input[] = "0009dbaa00004c 00000000fb82ca6 21c";
struct uniqueid x;
if (strlen(input) != 32) {
fprintf(stderr, "The string \"%s\" is not 32 characters long.\n"
"Bailing ... ", input);
exit(EXIT_FAILU RE);
}

if (4 !=
sscanf(input, "%8lx%8lx%8lx%8 lx", &x.word1, &x.word2, &x.word3,
&x.word4)) {
fprintf(stderr,
"The string \"%s\" did not lead to 4 conversions.\n"
"Bailing ... ", input);
exit(EXIT_FAILU RE);
}
printf
("It is possible that the string \"%s\"\n"
"was not parsed correctly.\n"
"To be sure of a correct conversion, use more code\n"
"more sophisticated than this simple scanf conversion.\n\n "
"In any case, it was parsed as the unsigned longs\n"
" %#0lx %#0lx %#0lx %0lx\n", input, x.word1, x.word2, x.word3,
x.word4);
return 0;
}
It is possible that the string "0009dbaa00004c 00000000fb82ca6 21c"
was not parsed correctly.
To be sure of a correct conversion, use more code
more sophisticated than this simple scanf conversion.

In any case, it was parsed as the unsigned longs
0x9dbaa 0x4c00 0xfb 82ca621c
Nov 15 '05 #3


Ross wrote:
Anyone have some code to help me understand how I can convert a given
hex string, say "0009dbaa00004c 00000000fb82ca6 21c," into a struct of
this form:

struct uniqueid {
ulong32_t word1;
ulong32_t word2;
ulong32_t word3;
ulong32_t word4;
};

Thanks very much.


Brevity in description can't help you get much help.Please be specific
e.g.
if `word4' holds the MS/LS part of the string.

You might try out the following:
*) copy the string to a local buffer
*) start with a pointer 8 chars before the end of the string,
*) call atoi()/atol()/strtol() or somesuch and store it to your
favoured
member, i.e. word4/word1
*) put a NULL character at the point where you started
*) repeat the above three steps, till you have nothing on your plate.

Of course, there are issues that need to be worked out. But till you
give us a better explanation.

HTH,
Suman.

Nov 15 '05 #4


Ross wrote:
Anyone have some code to help me understand how I can convert a given
hex string, say "0009dbaa00004c 00000000fb82ca6 21c," into a struct of
this form:

struct uniqueid {
ulong32_t word1;
ulong32_t word2;
ulong32_t word3;
ulong32_t word4;
};


One potential solution is to use function strtoul which
can give you some validation of the conversion.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef unsigned long ulong32_t;

struct uniqueid
{
ulong32_t word1;
ulong32_t word2;
ulong32_t word3;
ulong32_t word4;
};

int Convert(struct uniqueid *p, const char *s);

int main(void)
{
struct uniqueid test = {0};
char *id = "0009dbaa00004c 00000000fb82ca6 21c";

if(Convert(&tes t, id))
{
printf("For id \"%s\"\n",id );
printf("test.wo rd1 = %lu\n"
"test.word2 = %lu\n"
"test.word3 = %lu\n"
"test.word4 = %lu\n",
test.word1,test .word2,test.wor d2,
test.word4);
}
return 0;
}

int Convert(struct uniqueid *p, const char *s)
{
char buf[9] , *endp;
const char *cs;
int i;

if(strlen(s) != 32) return 0;
for(i = 0,cs = s; i < 4;i++,cs+=8)
{
sprintf(buf,"%. 8s",cs);
switch(i)
{
case 0: p->word1 = strtoul(buf,&en dp,16);
break;
case 1: p->word2 = strtoul(buf,&en dp,16);
break;
case 2: p->word3 = strtoul(buf,&en dp,16);
break;
case 3: p->word4 = strtoul(buf,&en dp,16);
break;
}
if(*endp != '\0') return 0;
}
return 1;
}
--
Al Bowers
Tampa, Fl USA
mailto: xa******@myrapi dsys.com (remove the x to send email)
http://www.geocities.com/abowers822/

Nov 15 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2763
by: Nathan | last post by:
Is there a way to convert a string to a CipherMessage? I am calling a function that decrypts a CipherMessage and returns the value. The only problem is when I want to use an encrypted value stored in a querystring, I can't figure out how to convert it back to a CipherMessage.
3
27567
by: news.hku.hk | last post by:
could you tell me how can i convet a string to integer?? #include <iostream> #include <string> using namespace std; int main(){ int integer; string buffer("123456789");
5
4339
by: sarab | last post by:
Hi, I have a managed C++ app in which i need to convert String * to char *. How can i achive this?. regards -sarab
3
3464
by: Petr Jakes | last post by:
Hi, I am trying to convert string to the "escaped string". example: from "0xf" I need "\0xf" I am able to do it like: a="0xf" escaped_a=("\%s" % a ).decode("string_escape") But it looks a little bit complicated in this beautiful language to me .....
5
12723
by: XML newbie: Urgent pls help! | last post by:
function to convert string to 1 dimensional array of long in VB.Net
3
7993
by: Ursula | last post by:
Is it possible to convert a string in a file. The problem is this: I have an object string that is a file xml and I want to pass to Deserialize function, but Deserialize function expect an object of FileStream type and the mode access. There is a function Convert.ChangeType(object value, Type conversionType), but I cant write for example: Convert.ChangeType(mystring, FileStream); CAN YOU HELP ME????? thanks
3
9896
by: Shawn Ferguson | last post by:
Hello All, I'm trying to do what I would think would be simple and straightforward, but it is not. I have a 2 textbox on a form, a label, and a button, when I click the botton I want to add the 2 textboxes and place the value in the label. Unfortunately, when I try to convert.Int32(txtBox1.Text) I get cannot convert string to int error. Basically, Im doing this: int x; int y; x = (int) Convert.ToInt32(TextBox1.Text); y = (int)...
4
17381
by: Man4ish | last post by:
HI , I am trying to convert string into char array of characters.but facing problem. #include <iostream> #include <string> using namespace std; int main() { string t="1,2,3,4,5,6";
12
13570
by: Peter | last post by:
Trying to convert string to byte array. the following code returns byte array of {107, 62, 194, 139, 64} how can I convert this string to a byte array of {107, 62, 139, 65} System.Text.UTF8Encoding str = new System.Text.UTF8Encoding(); string s = new string((char)107, 1); s += new string((char)62, 1);
9
8255
by: engteng | last post by:
How do I convert string to numeric in VB.NET 2003 ? Example convert P50001 to 50001 or 50001P to 50001 but if P is in middle then not convert. Regards, Tee
0
9587
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10588
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10324
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10085
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7623
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6857
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5527
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4302
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3827
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.