Click to See Complete Forum and Search --> : presenting a string as a byte


Guy&
September 24th, 2002, 09:04 AM
Hi

I have the following string :

"1F"

I need to present it as a BYTE

so if I have : BYTE array[2]

then -> array[0] = 0x1F

How can it be done?

Thank u

PaulWendt
September 24th, 2002, 09:41 AM
#include <iomanip>
#include <iostream>
#include <string>
using namespace std;

int main(int argc, char *argv[])
{
const char* hexNum = "1F";
int actualByte = 0;

// one method
istringstream is(hexNum);
is >> hex >> actualByte;
cout << hex << actualByte << endl;

// another method
sscanf(hexNum, "%02x", &actualByte);
cout << hex << actualByte << endl;

return (0);
}