You are currently viewing a search engine-friendly (archive) version of this page.
View Full Version : Combining hex values
scatman
November 27, 2001, 10:41 am
Hey I was wondering what type of variable would store hex in C++. Also, how would you combine the values of these variables into one, like strings are combined.
Also, I don't know if anyone knows this but is it possible to construct a packet totally out of hex before sending it? Thanks.
Tonker
November 28, 2001, 10:32 am
It depends what you mean by "storing" a hex value. I'm not sure how much you know about hex values. I'm no math wizard, but here's a bit of background that you can ignore if you like. A hex value is simply an numeric value stored in a base-16 format. 0 through 9 have the same values that we know from base-10 (the math we learned in grade school). After that, A=10, B=11, C=12, D=13, E=14, F=15. 10 in hex is equal to 16 in base-10, etc. So if you have a value of 0x1A it's basically 26, just represented differently.
What I'm getting at is that you can store the value of a hex number in any integer type that's large enough. It's also possible to store the hex representation (ex, 0x0D) as a string. It depends what you want to do with it really. There are also functions that can be used to convert between string and numeric format, I think. I don't have them available right now, but I can check when I get home.
As for combining them, I think you mean can they be appended to each other as a string? Yes you can. I'm a little foggy on it right now, but I think a function like sprintf( ) will allow you to write into the string, appending the hex values, I believe. I can check on that when I get home if you like. You can also add hex values if you have them in an integer type.
A for the packets I have no clue. I'm sure someone more familiar with network programming can answer that, though. :)
scatman
November 29, 2001, 07:26 pm
Hey Tonker, this is just a follow-up from my original question. I don't know if this is what you meant by using the sprintf() function but what I wanted to do was to store hex in two string integers, say x and y.
Say x = "0x45DE63" and y = "0x25E5F78G".
Then I wanted to combine the two strings into one using some C++ function.
So after combining the two, say the new variable is z, I would get z = "0x45DE6325E5F78G".
Number one, is this possible, and number two, how is it done?
All help is greatly appreciated. Thanks!
Tonker
November 30, 2001, 10:56 am
What you're trying to do is a bit more difficult if you're dealing with strings to begin with. I'll show you one possible way, assuming that you have the hex value as an integer or numeric type first.
I'll post some source code that you can peek at below. I'll also try to do up some code so that you can begin with two strings and combine them into one longer string.
I'm not sure what you're doing with these strings, but you should keep in mind the value of:
long int x = 0x45DE63, y = 0x25E5F78G;
when added together are very different from:
long int z = 0x45DE6325E5F78G;
(Oh and if you see 'G' in your hex string, you can be sure that something is very wrong! ;) )
[ November 30, 2001: Message edited by: Tonker ]</p>
Tonker
November 30, 2001, 10:59 am
#include <string.h>
#include <stdio.h>
int main() {
int iHexValue1 = 0x1A;
int iHexValue2 = 0x0D;
/* Need an array to hold the string */
char myString[7];
/* Display our hex values; decimal values are displayed in () */
printf( "Here are the hex values: \n" );
printf( "iHexValue1 = %.2X (%i)\n", iHexValue1, iHexValue1 );
printf( "iHexValue2 = %.2X (%i)\n", iHexValue2, iHexValue2 );
/* put the hex values into the string.
arg1 is the string you want to write to, arg2 is the formatted string,
arg3... are the variables to be inserted into the formatted string */
sprintf( myString, "0x%.2X%.2X", iHexValue1, iHexValue2 );
/* display the string */
printf( "\nmyString is now: %s\n", myString );
}
Tonker
November 30, 2001, 11:04 am
Here's the output from that:
D:\projects\test>hex
Here are the hex values:
iHexValue1 = 1A (26)
iHexValue2 = 0D (13)
myString is now: 0x1A0D
D:\projects\test>
scatman
December 1, 2001, 03:37 pm
Thanks for the help Tonker!
There's just one little problem... :( When I use the sprintf() function, the hex in myString isn't seen as hex by C++... it's seen as just a bunch of characters with a null zero at the end. What I want to do is add hex values into one variable, but still have C++ identify it as hex. If you use integers to store the hex values, they cannot hold more than a few hex characters.
Sorry for the inconvenience Tonker, and thanks for the help!
Tonker
December 4, 2001, 12:35 pm
Scatman, sorry for the delay. Could you post your source code for me to take a look at, or e-mail/pm it to me: tonker@helpfromtechs.com if you prefer to keep it under wraps.
I'm still a bit fuzzy on what you're trying to do. Where are you getting the hex values from (and what format are they in when you begin to process them)? What is it you're trying to do with them?
vBulletin Copyright © Jelsoft Enterprises Ltd., 2000-2009.