GIDForums  

Go Back   GIDForums > Computer Programming Forums > CPP / C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 08-May-2007, 06:02
temporary temporary is offline
New Member
 
Join Date: May 2007
Posts: 1
temporary is on a distinguished road

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  
Old 08-May-2007, 06:39
tufan tufan is offline
Junior Member
 
Join Date: Aug 2006
Posts: 32
tufan is on a distinguished road

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  
Old 08-May-2007, 08:22
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,627
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Conversion of datatypes (probably not what you think :p)


Quote:
Originally Posted by temporary
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)
.
.
.
What is the difference between '\x81' and 0x81; ?

'\x81' is a character literal. Its value is hex 81 (which is 129 decimal).

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:
#include <iostream>

using namespace std;

int main()
{
    char c1 = '\x81';
    char c2 = 0x81;
    unsigned char c3 = '\x81';
    unsigned char c4 = 0x81;
    int i1, i2, i3, i4, i5, i6;

    i1 = c1;      // The char is promoted to an int
    i2 = c2;      // The char is promoted to an int
    i3 = c3;      // The unsigned char is promoted to an int
    i4 = c4;      // The unsigned char is promored to an int
    i5 = '\x81';  // The char is promoted to an int and then assigned to i5. 
    i6 = 0x81;    // The integer value 0x81 is assigned to i5.

    cout << "i1 = 0x" << hex << i1 << ", = " << dec << i1 << " decimal"<< endl;
    cout << "i2 = 0x" << hex << i2 << ", = " << dec << i2 << " decimal"<< endl;
    cout << "i3 = 0x" << hex << i3 << ", = " << dec << i3 << " decimal"<< endl;
    cout << "i4 = 0x" << hex << i4 << ", = " << dec << i4 << " decimal"<< endl;
    cout << "i5 = 0x" << hex << i5 << ", = " << dec << i5 << " decimal"<< endl;
    cout << "i6 = 0x" << hex << i6 << ", = " << dec << i6 << " decimal"<< endl;

    return 0;
}

Output:
Code:
i1 = 0xffffff81, = -127 decimal i2 = 0xffffff81, = -127 decimal i3 = 0x81, = 129 decimal i4 = 0x81, = 129 decimal i5 = 0xffffff81, = -127 decimal i6 = 0x81, = 129 decimal

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 GIDBlogNARMY by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Regular expression to DFA conversion c code sandipmw C Programming Language 1 23-Apr-2007 12:47
File Conversion Software sdluthier Computer Software Forum - Windows 3 04-Mar-2007 18:18
NFA to DFA conversion fiddler_808 CPP / C++ Forum 3 02-Jun-2006 12:49
Re: Conversion: Binary, Decimal, Hexadecimal WaltP C Programming Language 1 10-May-2006 06:49
conversion double to float donaldk C Programming Language 5 12-Feb-2006 23:16

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 22:35.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.