-3

My class has two unsigned int member variables like

unsigned int lowByte;
unsigned int highByte;

I need to store alphanumeric string value Ex: "1234##" or"5635$$" into the above two or any one of the variable...! & I should be able to reconstruct the string value back from the unsigned int..!

Any help please ?

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
  • What have you tried so far? How do you want the transformation to be made? And do you really need to use an `unsigned` to store a byte? – Bathsheba Dec 30 '13 at 11:14
  • Of course this is homework. – motiur Dec 30 '13 at 11:15
  • i tried atoi() any way that doesnt work for non digit string.. Even tried converting to BCD .. not getting much success..! – user3146162 Dec 30 '13 at 11:17
  • It is not homework.. its kind of requirement.. I have not found such kind of code in Google or any browse.. so thought to work on that.! – user3146162 Dec 30 '13 at 11:18
  • IMO this is related to [How to convert a number to string and vice versa in C++](http://stackoverflow.com/questions/5290089/how-to-convert-a-number-to-string-and-vice-versa-in-c). – Sebastian Dec 30 '13 at 11:20
  • your string is representation of a number in a number system with base 256. Use standard method to convert the number to base 10. like: `mystring[0]* (256^0) + mystring[1]* (256^1) + mystring[2]* (256^2) + ...` (`^` is `to the power of` operator) – Prashant Borde Dec 30 '13 at 11:24
  • void MyClass::SetIdString(const string& id) { string val = id; const char* s = val.c_str(); unsigned int bcd_high = 0xffffffff; unsigned int bcd_low = 0xffffffff; int i=0; while(*s) { bcd <<= 4; if(isdigit(*s)) { char c = *s - '0'; bcd_high |= c; //works fine for 0-9 ++s; } else { bcd_low <<= 4; char cl = *s - '#'; //works fine for #(0x23) -(0x2d) bcd_low |= cl; ++s; } } highByte =bcd_high; //highByte & lowByte are only supported class variables members..! lowByte = bcd_low; } – user3146162 Dec 30 '13 at 11:25
  • `atoi()` successfully converts the strings in your question. This [http://ideone.com/qWi9vj](http://ideone.com/qWi9vj) code works. I suppose you haven't write the universal parser if it's the "kind of requirement", so if you strings have the format "[spaces][digits][characters]" it's enough. – yakov Dec 30 '13 at 11:28
  • use `union` as follows: `union mix { int mynum[2]; char mystr[8]; } mymix={0,0}; ` – Prashant Borde Dec 30 '13 at 13:31
  • unsigned int result = (unsigned int)atoi("1234$$"); Hi. here you will get result as 1234... but how you convert from 1234 to back "1234$$"..? – user3146162 Dec 31 '13 at 05:08

1 Answers1

2

The posted code produces the same substring: value.substr(0, pos1). Note that std::string::substr() does not modify the object, but returns a new std::string.

Example:

#include <iostream>
#include <string>

int main ()
{
    std::string value ="12,fooBar";
    unsigned int myNum;
    std::string myStr;

    const size_t pos1 = value.find(',');    
    if (std::string::npos != pos1)
    {
        myNum = atoi(value.substr(0, pos1).c_str());
        myStr = value.substr(pos1 + 1);
    }

    std::cout << myNum << " and " 
              << myStr << std::endl;

    return 0;
}

Output:

12 and fooBar

If the unsigned int is the only piece required then the following will work:

unsigned int myNum = atoi(value.c_str());

as atoi() will stop at the first non-digit character (excluding optional leading - or +), in this case the ,.

codebase
  • 91
  • 1
  • 10