0

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=100F
100F+3=1012
2
  • ??? Are you using c++ for this or visual basic (and is that VB6 or VB.NET)? Commented Dec 30, 2010 at 16:41
  • sorry using visual C++ 2008 Express edition Commented Dec 30, 2010 at 16:47

5 Answers 5

2

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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);
}


Comments

0

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

10 Comments

no no ,, the string intially contains a number 1000, this number is already represented in hexadecimal format so that when i store it in a variable it must be in hexadecimal format not decimal, what ur saying that its already in decimal and i need to convert it to hexadecimal, no ,, its already hexadecimal
@Shadi Al Mahallawy - You are just making life difficult for yourself. Convert the string to an int to begin with, do your calculations. A decimal is a decimal it is a value - you can display it in whatever format you want later on.
I dont think ur gettin me or i am not gettin something , the string contains 1000, its a string , it needs to be converted to an integer so I used atoi() and stored it in variable x,,know x contains 1000, as x is a decimal representation if I perform the following calculations 1000+3=1003 1003+3=1006 1006+3=1009 1009+3=1012 What I need is that the number would be represented in hexadecimal not decimal so that when i perform these calculations 1000+3=1003 1003+3=1006 1006+3=1009 1009+3=100C
@Shadi Al Mahallawy - Why can't you convert the Hex String to an integer? Later on calculations will be easier. See this: codeproject.com/KB/string/hexstrtoint.aspx
@Shadi Al Mahallawy - Again. An integer is an integer. The computer will store it in some sort of internal representation. When you want to dispaly it, convert to whatever representation you want. Decimal 10 is binary 1010 is hex A.
|
0

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
}

Comments

0

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;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.