Solved

Buffer to integers

Posted on 2003-10-28
8
470 Views
Last Modified: 2013-12-14
Hi,
I have a buffer which contains hex numbers

e.g ff 22 5f 4f 4f ff 4a 6a aa ff 9a 9b af .....etc

I want to copy each of the hex numbers from the buffer in turn, to an integer.
i.e.
int numa = 0xff;
int numb = 0x22;
int numc = 0x5f;
int numd = 0x4f;  
int nume = 0x4f; ...and so forth

I need the code to be efficient please,

Thanks
John
0
Comment
Question by:XPUSR
  • 2
  • 2
  • 2
  • +2
8 Comments
 
LVL 2

Expert Comment

by:MeiaDose
ID: 9637329
This buffer is a array of hex numbers? or is a string?

Post the declaration of that buffer please.

0
 

Author Comment

by:XPUSR
ID: 9637502
----------------------------------
char * buffer;
buffer = (char*) malloc (10000);
fgets ( buffer, 1000,"file.txt" );
-------------------------------------
the buffer is a string of hex values(without the 0x) "ff 22 5f 4f 4f ff 4a 6a aa ff 9a 9b af ". These values are taken from a text file "file.txt" and copied to the buffer.

sorry for being so ambiguous...

J
0
 
LVL 19

Assisted Solution

by:Dexstar
Dexstar earned 75 total points
ID: 9637764
XPUSR:

> I want to copy each of the hex numbers from the buffer in turn, to an integer.

Try this:

      int GetHexValue( char chr )
      {
            switch( toupper(chr) )
            {
            case '0': return 0x00;      break;
            case '1': return 0x01;      break;
            case '2': return 0x02;      break;
            case '3': return 0x03;      break;
            case '4': return 0x04;      break;
            case '5': return 0x05;      break;
            case '6': return 0x06;      break;
            case '7': return 0x07;      break;
            case '8': return 0x08;      break;
            case '9': return 0x09;      break;
            case 'A': return 0x0A;      break;
            case 'B': return 0x0B;      break;
            case 'C': return 0x0C;      break;
            case 'D': return 0x0D;      break;
            case 'E': return 0x0E;      break;
            case 'F': return 0x0F;      break;
            default:  return 0;            break;
            }
      }      

      // lpszChars = Array of Chars to Convert
      // pInts = Array of Ints to Hold Converted Values
      // nSize = # of Ints to Convert
      void ConvertHex( char* lpszChars, int* pInts, size_t nSize )
      {
            char*      lpCurrent = lpszChars;
            
            for ( unsigned register nCounter = 0 ; nCounter < nSize ; ++nCounter )
            {
                  pInts[nCounter] = (GetHexValue(*(lpCurrent++)) << 8) |
                                                GetHexValue(*(lpCurrent++));
            }
      }

Just allocate the array of integers that you want to convert, and then ConvertHex().


Hope that helps,
Dex*
0
 
LVL 15

Expert Comment

by:efn
ID: 9638446
Or you could let the library do more of the work:

#include <sstream>

using namespace std;

istringstream iss(buffer);
// Tell iss to interpret numbers as hexadecimal.
iss.setf(ios::hex, ios::basefield);

iss >> numa;
iss >> numb;
//  etc.

--efn
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 3

Accepted Solution

by:
EarthQuaker earned 75 total points
ID: 9638754
Developping efn's path of stopping wheel's reinvention :

#include <algorithm>
#include <sstream>
#include <vector>

const char* buffer="ff 22 5f 4f 4f ff 4a 6a aa ff 9a 9b af";
istringstream ss(buffer);
ss >> hex;
vector<int> v;
copy(istream_iterator<int>(ss), istream_iterator<int>(), back_inserter(v));

cout << "Item 1 is " << v[0] << endl;
0
 
LVL 19

Expert Comment

by:Dexstar
ID: 9643280
Well, those are good too... I just wanted to give him a solution where it was very clear what was going on and how it worked.  You start invoking the STL like that on someone who doesn't know it, and they will find it difficult to learn how you did what you did.

Dex*
0
 
LVL 3

Expert Comment

by:EarthQuaker
ID: 9644031
Dex : Nothing wrong with your solution, it's prolly faster.

I just tend to use something premade & robust & fast instead of doing all by hand.
0
 

Author Comment

by:XPUSR
ID: 9708505
Thanks both solutions work 100%, appreciate that you were trying to explain it....

John
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

943 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

5 Experts available now in Live!

Get 1:1 Help Now