![]() |
|
|||||||
|
|
Thread Tools | Search this Thread | Rate Thread |
|
#1
|
|||
|
|||
Conversion of datatypes (probably not what you think :p)Hi all,
I have the following problem in C++: I have a string with the following contents. (I cannot change this because this is incoming data from a library (libpcap).) String t = "\x00\x81"; // this is the value I get from pcap I want to convert this hexadecimal to it's decimal value (not as ascii value). So eventually I should have: int i = 129; // 192 is the value of 81 in hex (00 is just 0) How can I do this? I tried alot but always seem to get stuck :s Strange thing is: int i = 0x81; cout << i << endl; # gives the correct decimal value (129) cout << hex << i <<endl; # gives the correct value (81) If I do: int i = '\x81'; # the char is 81 in hex. But char just represents bits, so it should equal to 0x81. cout << i << endl; # gives the correct decimal value (129) cout << hex << i <<endl; # gives the correct value (81) # I get a box with a little number in it: OR: What is the difference between '\x81' and 0x81; ? Help will be very appreciated! I have already spend hours on this problem. |
|||
|
#2
|
|||
|
|||
Re: Conversion of datatypes (probably not what you think :p)'\x81' this is a char like 'a' it only helps you to print a character counter of an ascii code the number after x is taken as hexadecimal and 0x81 this is an integer but not in decimal,in hexadecimal.compiler can differ them by the 0x you had put before.you can assign them to integers.
|
|
#3
|
|||
|
|||
Re: Conversion of datatypes (probably not what you think :p)Quote:
0x81 is an integer literal Its value is hex 81 (which is 129 decimal) When you have an expression that converts a char to an int (that is, the char is "promoted" to an int), the char is expanded by the following rules: If the char data type is an unsigned char, the integer value is obtained by putting zeros in front. If the char data type is signed char, the integer value is obtained by putting upper bits equal to the sign (the msb of the char). Note that the C language standard leaves it up to the implementation (the compiler writer) to decide whether a variable declared as "char" will be a signed char or an unsigned char. All of the ones that I have seen treat it as a signed char. Note that the bits in the char representation are the same, whether it is signed or unsigned. They are treated differently in expressions involving comparisons, and they are treated differently in expressions where the char is promoted to an int. CPP / C++ / C Code:
Output: Code:
How to make it work for your case: Just store the char value in an unsigned char variable (or use a cast if you want to use a char in an expression and the char is greater than 0x7f and you don't want it to be promoted as a negative value). Regards, Dave |
Recent GIDBlog
Programming ebook direct download available by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Regular expression to DFA conversion c code | sandipmw | C Programming Language | 1 | 23-Apr-2007 13:47 |
| File Conversion Software | sdluthier | Computer Software Forum - Windows | 3 | 04-Mar-2007 19:18 |
| NFA to DFA conversion | fiddler_808 | C++ Forum | 3 | 02-Jun-2006 13:49 |
| Re: Conversion: Binary, Decimal, Hexadecimal | WaltP | C Programming Language | 1 | 10-May-2006 07:49 |
| conversion double to float | donaldk | C Programming Language | 5 | 13-Feb-2006 00:16 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The