CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Dec 2001
    Posts
    29

    presenting a string as a byte

    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

  2. #2
    Join Date
    May 2000
    Location
    Phoenix, AZ [USA]
    Posts
    1,347
    Code:
    #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);
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured