0

The array is set up like so:

string * str = new string[11];

Where the content of the string looks like:

str[0]=AAAAAAAA,BBBBBBBB,CCCCCCCC,DDDDDDDD,EEEE,FFFFFFFF,GGGGGGGG,HHHH,IIII,JJJJ,KKKK
str[1]=AAAAAAAA,BBBBBBBB,CCCCCCCC,DDDDDDDD,EEEE,FFFFFFFF,GGGGGGGG,HHHH,IIII,JJJJ,KKKK
str[2]=AAAAAAAA,BBBBBBBB,CCCCCCCC,DDDDDDDD,EEEE,FFFFFFFF,GGGGGGGG,HHHH,IIII,JJJJ,KKKK
...
str[12]=AAAAAAAA,BBBBBBBB,CCCCCCCC,DDDDDDDD,EEEE,FFFFFFFF,GGGGGGGG,HHHH,IIII,JJJJ,KKKK

Another array looks like:

string * type = new string[11];

Where the content is:

type[0]="1";
type[1]="1";
type[2]="1";
type[3]="1";
type[4]="2";
type[5]="1";
type[6]="1";
type[7]="2";
type[8]="2";
type[9]="2";
type[10]="2";

These types correspond to each value in the string, so, for the first string:

1=float , 2=integer

  • AAAAAAAA would be 1; or an float
  • BBBBBBBB would be 1; or an float
  • CCCCCCCC would be 1; or an float
  • DDDDDDDD would be 1; or an float
  • EEEE would be 2; or a integer
  • FFFFFFFF would be 1; or an float
  • GGGGGGGG would be 1; or an float
  • HHHH would be 2; or a integer
  • IIII would be 2; or a integer
  • JJJJ would be 2; or a integer
  • KKKK would be 2; or a integer

In addition the single type array works for all strings in the str array.

Now for my question: How do i use the above information to extract each individual values from the string and convert it to an integer or a float based on the value in the type array.

BE AWARE: Boost is not available to me

The conversion functions look like: (The other is formatted similarly except for an integer)

unsigned int BinaryParser::hexToFloat(std::string hexInput)
{   
    std::stringstream ss (hexInput);
    unsigned int floatOutput;
    ss >> hex >> floatOutput;
    return reinterpret_cast<float&>(floatOutput);
}
9
  • An array of size X can only be addressed from 0 to X-1. Both str and type initializations you do are invalid. Commented Jan 24, 2014 at 14:59
  • That was just a mistype on my part. Apologies. Commented Jan 24, 2014 at 15:01
  • Only 4 hex digits for an int? Unusual. Are hexadecimal digits in little endian or big endian order? Commented Jan 24, 2014 at 15:02
  • Where is the problem? You can convert a hexadecimal digit pair to a char value? You can store char values into a char array[2] or char array[4]? You can obtain the address of a float or an int? You can call memcpy? Commented Jan 24, 2014 at 15:04
  • @laune Can you show me an example of this. I am not very good with c++ and it's rather foreign to me. Commented Jan 24, 2014 at 15:06

1 Answer 1

1

OK, first part: extract the comma-separated strings. One way would be:

std::vector<std::string> split( std::string s ){
  std::vector<std::string> vec;
  int pos = 0;
  while( std::string::npos != (pos = s.find( ',', pos ) ) ){
    vec.push_back( s.substr( 0, pos ) );
    s = s.substr( pos + 1 );
  }
  vec.push_back( s );
  return vec;
}

Depends on the input string being "well-behaved".

This converts an int from hex digits:

int convInt( std::string hexInput ){
  std::istringstream iss (hexInput);
  uint16_t intOutput;
  iss >> std::hex >> intOutput;
  return intOutput;
}

Float cannot be read using std::hex, so we assume the HHHHHHHH is a float's bytes interpreted as an int32_t.

float convFloat( std::string & hexInput ){
  std::istringstream iss (hexInput);
  uint32_t intOutput;
  iss >> std::hex >> intOutput;
  return reinterpret_cast<float&>(intOutput);
}

For storing the results we can use:

enum TypeTag { eInt, eFloat };

class IntOrFloat {
public:
  IntOrFloat( int i ) : typeTag(eInt),integer(i),floating(0) { }
  IntOrFloat( float f ) : typeTag(eFloat),integer(0),floating(f) { }
  virtual ~IntOrFloat(){}
  int getInt() const { return integer; }
  float getFloat() const { return floating; }
  TypeTag getTypeTag() const { return typeTag; }
private:
  TypeTag typeTag;
  int integer;
  float floating;
};


std::ostream& operator<< (std::ostream& os, const IntOrFloat& iof){
  switch( iof.getTypeTag() ){
  case eInt:
    os << iof.getInt();
    break;
  case eFloat:
    os << iof.getFloat();
    break;
  }
  return os;
}

To convert one comma-separated string according to the type vector:

std::vector<IntOrFloat> convert( const std::vector<std::string> t, const std::string s ){
  std::vector<IntOrFloat> results;
  std::vector<std::string> hexes = split( s );
  for( int i = 0; i < hexes.size(); i++ ){
    if( t[i] == "1" ){
      results.push_back( IntOrFloat( convFloat( hexes[i] ) ) );
    } else {
      results.push_back( IntOrFloat( convInt( hexes[i] ) ) );
    }
  }
  return results;
}

That's it, then. - I've been using vector instead of the arrays. You can easily convert, e.g.

std::vector<std::string> fromArray( std::string strs[], int n ){
  std::vector<std::string> strings;
  for( int i = 0; i < n; i++ ) strings.push_back( std::string( strs[i] ) );
  return strings;
}
#define fromArray(a) fromArray( a, (sizeof(a)/sizeof(a[0])) )

And here is my test program:

#define LENGTH(a) (sizeof(a)/sizeof(a[0]))
int main(){
  std::string t[] = {"2","1","1","2"};
  std::string s[] = {
    "8000,4048f5c3,bf000000,FFFF",
    "0001,42f6e979,c44271ba,7FFF",
    "1234,00000000,447a0000,5678"
  };

  std::vector<std::string> types = fromArray( t );
  std::vector<std::string> strings  = fromArray( s );
  for( std::vector<std::string>::iterator it = strings.begin() ; it != strings.end(); ++it ){
    std::vector<IntOrFloat> results = convert( types, *it );
    std::cout << "converting string " << *it << ", " << results.size() << " values:" <<   std::endl;
    for( std::vector<IntOrFloat>::iterator iof = results.begin() ; iof != results.end(); ++iof ){
      std::cout << "  " << *iof << std::endl;
    }
  }
}
Sign up to request clarification or add additional context in comments.

3 Comments

Okay, try to follow me here. I'm not real familiar with vectors. This will separate each individual string and put it in it's own index in the vector, correct? If so then it won't work with an array of strings only a single string. Will i be required to create multiple vectors to receive the information is there a way to create a 2D vector similar to a 2D array? that way all data can be held in one vector.
The functions don't establish the way their results are being stored in vectors or whatever. If the original position of a hex string in one of the 11/12/13 strings doesn't matter you can file them all into a single vector.
Thank you so much!! I have to look into understanding exactly how the vectors work but i think i understand the concept for the most part. They seem more suitable for what i was trying to do than arrays...so thanks for pointing that out as well.

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.