Hi

I have declared BYTE myKey[10];
and on executing the value stored in myKey is 0x9100
i need to do string compare this value with user defined value (9100)
how can i do the comparison?

i converted to int
int tempkey = (int)myKey[i]; //(have put it in loop based on myKey length)
and trying to convert tempKey to string using sstream , but getting errors
any simple logic?
Thanks

This is slightly confusing. I assume that sizeof( BYTE ) is 1, but the value 0x9100 is at least two bytes big? Also, the size of myKey[ 10 ] is going to 10, so that isn't expressed by 0x9100 either.

I think that you can do what you want to do by using a bunch of stream manipulators:

#include <iostream>
#include <iomanip>
#include <sstream>

typedef unsigned char BYTE;

int main()
{
    // A test array of BYTEs
    BYTE a[10] = { 54, 23, 1, 9, 64, 91, 17, 85, 22, 10 };

    // A stringstream to push the output into
    std::stringstream ss;

    // Add a prefix to the start of the output stream.  You could also do this
    // with the std::showbase manipulator, but it would do it for every character
    // and we don't want to do that.
    ss << "0x";

    // Push the elements of the array into the stream:
    // std::hex            - prints the item in hex representation
    // std::uppercase      - Makes the letters in the hex codes uppercase
    // std::setfill( '0' ) - Fills the width of each output with zeros
    // std::setw( ... )    - makes all of the output values 2 characters wide
    // static_cast< int >()- std::streams treat 'char' variables specially, so you need to cast to an int to see the hex values
    for ( size_t i = 0; i < 10; ++i )
        ss << std::hex << std::uppercase << std::setfill( '0' ) << std::setw( 2 * sizeof( BYTE ) ) << static_cast< unsigned >( a[ i ] );

    std::cout << ss.str() << std::endl;

    return 0;
}

This code will output the string 0x36170109405B1155160A.

Is that what you were getting at?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.