how would i convert a Hex Endian to an int example:
0100 = 256
0001 = 1
etc...
please help!!
Printable View
how would i convert a Hex Endian to an int example:
0100 = 256
0001 = 1
etc...
please help!!
Sounds like a school project... you need to know the value of every character from 0..9 and A..F, you could use an array for that. Also, you need to know that each value to left of a another value is worth 16x its value (just like the 100's place in decimal is worth 10x the 10's place, since it is one digit to the left). This should be enough knowledge to get you going in the right direction.
i don't understand 00 = to the left right which = 0 and 01 = to the right so 00 < 01
so how can you do that ? confusing ! shouldnt there be a function for it to make life easyer?
You have me confused now... are you speaking about converting a hex string into an integer, or coverting two adjacent bytes in memory into an integer?
converting a Hex string to integer, sorry
Hex strings are just like decimal strings - the numbers to the left (that appear first in the string) are the more significant numbers. I would try and make a program that converts a decimal string into an integer first, and then doing it with hex digits becomes elementary.
The first problem is that you don't know how large the string is (and you don't need to call a function to get the strings length). Each additional digit means that the digits you already look at are 1 digit to left than you originally thought - and what happens when you move all digits 1 to the left?
printf("%d",0x0100"); works but how would i make it use a string like printf("%d%s","0x","0100); ?
ah sorry about that last post
printf("%d",0x0001) - works so how would i do
char *HexStr="0001";
printf("%d%s","0x",HexStr)?
Are you asking how to print an integer in hexadecimal notation? If so, then you wish to convert an integer into a hex string - not convert a hex string into an integer. Please state what you are trying to do exactly to resolve the confusion.
sorry about the confusin and wasteing your time, i've given up need sleep
Here's one way to do it:
Code:int hexStringToInt(const std::string& str)
{
int num = 0;
for (int p=0;p<str.length();++p)
{
num *= 16;
if (num >= '0' && num <= '9')
num += str[p] - '0';
else // a...f
num += std::tolower(str[p]) - 'a' + 10;
}
return num;
}
Quote:It also uses void main. I'd say they get a C-, not a B in that assignment ;) :DQuote:
From the source example
Note: Yes, I used GOTOs in this program.
Don't give me grief over it. I needed to concentrate on other homework, and take a "B" in this assignment.
hmm can some one tell me if these functions look ok please?
thanks
Code:int BigEndian8(char *data,int start)
{
unsigned short endian;
endian = data[start];
return endian;
}
int BigEndian16(char *data,int start)
{
unsigned char Big;
unsigned char Little;
unsigned short endian;
Big = data[start+1];
Little = data[start];
endian = (Big << 8) + Little;
return endian;
}
int BigEndian32(char *data,int start)
{
unsigned char Big;
unsigned char Little;
unsigned short endian;
Big = data[start+1];
Little = data[start];
endian = (Big << 8) + Little;
Big = data[start+3];
Little = data[start+2];
endian = endian + (Big << 24) + Little;
return endian;
}
hmm another Question: how do i get the length of a packet with null's in example of a packet:
<"name"><null><endian><data><null>
if i do strlen(packet) it returns 4 when it's really > 4 :(