开发者

How to convert a string containing a hexadecimal integer

I have a string for example containing 1000 i need to store it in a variable called x(for eg) so the whole thing is that i need to store a string as an hexadecimal base integer so that if i add 3 (for example)

1000+3=1003
1003+3=1006
1006+3=1009
1009+3=100C
100C+3=开发者_如何学C100F
100F+3=1012


From what I could tell, you have a number in hexadecimal representation in a string, and you wish to put that into an int. You then want to be able to add to that int, and output its new value, again in a hexadecimal representation to verify the result.

#include <sstream>
#include <ios>

int main() {
   std::string input = "1000";

   // Read the input string into a stream (streams have format conversion functionality)
   std::stringstream ss;
   ss << std::hex << input;

   // Read the numerical value back out into an int
   int x = 0;
   ss >> x;

   // Add 3 and display result in hex
   x += 3;
   std::cout << std::showbase << std::hex << x; // will output: 0x1003

   // Add another 10 and display result in hex
   x += 10;
   std::cout << std::showbase << std::hex << x; // will output: 0x100d
}

Hope this helps.


It sounds like you're getting a hexadecimal string, and need to store the numeric value in a variable. Then, at some point in the future, you need to convert back from the variable to a string.

You don't have to worry about the internal representation used by basic numeric types (int, long, float). While the computer will natively store the value in binary, programming languages are designed so that this fact is (somewhat) hidden from you, so you don't have to worry about it.

Now that we've let the language abstract away the internal storage as "a number" all we need is a way to read and to write that number. All languages have functions that will let you read/write data in different formats, and you can always roll your own (though that's not recommended unless you're just learning) so I'm using C.

C offers scanf() and printf() - there's other functions that will do the job, too, but these are a decent example of stuff you can use. These functions are very similar, so it's easy to write some code:

#include <errno.h>

#define FORMAT "%X"

// Converts string input into n integer
//
// args
//   str the string to convert
//   x   pointer to location for return value
//
// returns
//   0 on success
//   nonzero on failure
int get_num_from_hex_string(char* str, int* x)
{
  if( str == 0 ) return EINVAL;
  // we assume MAX_LEN is defined, somewhere...
  if( strlen(str) > MAX_LEN ) return EINVAL;

  int result = scanf(FORMAT, x);
  // there's prolly a better error, but good enough for now
  if( result != 1 ) return EIO; 

  return 0;
}

// Converts an integer into a hex string
//
// args
//   x    - the integer to convert
//   str  - the pre-allocated output buffer
//   len  - amount of space left in str.  Must be
//          at least 12 bytes.
//
// returns
//   0 on success
//   nonzero on failure
int get_hex_string(int x, char*str, int len)
{
  if( str == 0 ) return EINVAL;
  if( len < 12 ) return EINVAL;

  sprintf(str, FORMAT, x);
}



Just us a regular integer and when you need to display it, convert to a hexadecimal format.


Suppose you store your string in a variable:

std::string mystring;

Then, in order to do calculations on it, you can use a function that you define yourself:

void AddSomeNumber(std::string& mystring, int n)
{
    // Convert the string to integer
    unsigned mynumber;
    sscanf(mystring.c_str(), "%x", &mynumber);

    // Add a number
    mynumber += n;

    // Convert the number to string
    char temp[9]; // pardon my style :-)
    sprintf(temp, "%x", mynumber);
    mystring = temp;
}

int main()
{
    std::string mystring("1009");
    AddSomeNumber(mystring, 3);
    std::cout << mystring << '\n'; // will print 100c
}


sorry for errors in advance, I made it fast, not sure that this is the solution, it is just an idea

#include <iostream>
#include <string>

using namespace std;

class hexa
{
public:
    hexa(const char* x);
    hexa(int x=0);

    hexa& operator+(int x);
    operator int& ();
    operator string () const;
private:
    int value_;
    friend istream& operator >>(istream &is, hexa& x);
    friend ostream& operator <<(ostream &os, const hexa& x);
};


int main()
{
    hexa x(0x1009);
    cout << "x = " << x << endl;

    hexa y("6");
    cout << "y = " << y << endl;

    cout << "x + y = " << x+y << endl;
    return 0;
}

hexa::hexa(const char* x)
    : value_(stoi(string(x), 0, 16))
{}

hexa::hexa(int x)
    : value_(x)
{}

hexa& hexa::operator+(int x)
{
    value_ += x;
    return *this;
}

hexa::operator int& ()
{
    return value_;
}

hexa::operator string () const
{
    return to_string(_Longlong(value_));
}

istream& operator >>(istream& is, hexa& x)
{
    is >> hex >> x.value_;
    return is;
}
ostream& operator <<(ostream& os, const hexa& x)
{
    os << hex << x.value_;
    return os;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜