Hi and I am writing a small program but need to make a function that converts an integer to a hex. Below is an example of a similar function but need the result stored in a std::string variable.

printf("%x%x%x%x%x",h0, h1, h2, h3, h4);

Does anybody know how to create a hex() function which will do the same as the above except instead of printing the data it will store the data? I'm also using c++ and hoping this one will work in VC++.

Is this what you need :

#include <iostream>
#include <string>
#include <sstream>

using namespace std;

typedef size_t Type;

string toHex(const Type& number){
	stringstream stream;
	stream << hex << number;
	return stream.str();
}
int main(int argc, char *argv[])
{
	string num  = toHex(15);
	cout << num << endl;
 
    return 0;
}
Comments
Solved!

I just tried your function and it only outputs a bunch of numbers. Below is an example of what I'm doing.

#include<iostream>
#include <sstream>
#include <string>

#define rotateleft(x,n) ((x<<n) | (x>>(32-n)))   
#define rotateright(x,n) ((x>>n) | (x<<(32-n)))   



typedef size_t Type;
std::string hex(const Type& number){
	std::stringstream stream;
	stream << hex << number;
	return stream.str();
}

std::string SHA1(unsigned char * str1)   
{   
	int i;
    unsigned long int h0,h1,h2,h3,h4,a,b,c,d,e,f,k,temp;   
    
    h0 = 0x67452301;   
    h1 = 0xEFCDAB89;   
    h2 = 0x98BADCFE;   
    h3 = 0x10325476;   
    h4 = 0xC3D2E1F0;   
    
    unsigned char * str;   
    str = (unsigned char *)malloc(strlen((const char *)str1)+100);   
    strcpy((char *)str,(const char *)str1);   
    
    int current_length = strlen((const char *)str);   
    int original_length = current_length;   
    str[current_length] = 0x80;   
    str[current_length + 1] = '\0';   
    
    char ic = str[current_length];   
    current_length++;   
    
    int ib = current_length % 64;   
    if(ib<56)   
        ib = 56-ib;   
    else   
        ib = 120 - ib;   
    
    for(i=0;i < ib;i++)   
    {   
        str[current_length]=0x00;   
        current_length++;   
    }   
    str[current_length + 1]='\0';   
    
    for(i=0;i<6;i++)   
    {   
        str[current_length]=0x0;   
        current_length++;   
    }   
    str[current_length] = (original_length * 8) / 0x100 ;   
    current_length++;   
    str[current_length] = (original_length * 8) % 0x100;   
    current_length++;   
    str[current_length+i]='\0';   
    
    int number_of_chunks = current_length/64;   
    unsigned long int word[80];   
    for(i=0;i<number_of_chunks;i++)   
    {   
        for(int j=0;j<16;j++)   
        {   
            word[j] = str[i*64 + j*4 + 0] * 0x1000000 + str[i*64 + j*4 + 1] * 0x10000 + str[i*64 + j*4 + 2] * 0x100 + str[i*64 + j*4 + 3];   
        }   
        for(int j=16;j<80;j++)   
        {   
            word[j] = rotateleft((word[j-3] ^ word[j-8] ^ word[j-14] ^ word[j-16]),1);   
        }   
    
        a = h0;   
        b = h1;   
        c = h2;   
        d = h3;   
        e = h4;   
    
        for(int m=0;m<80;m++)   
        {   
            if(m<=19)   
            {   
                f = (b & c) | ((~b) & d);   
                k = 0x5A827999;   
            }   
            else if(m<=39)   
            {   
                f = b ^ c ^ d;   
                k = 0x6ED9EBA1;   
            }   
            else if(m<=59)   
            {   
                f = (b & c) | (b & d) | (c & d);   
                k = 0x8F1BBCDC;   
            }   
            else   
            {   
                f = b ^ c ^ d;   
                k = 0xCA62C1D6;    
            }   
    
            temp = (rotateleft(a,5) + f + e + k + word[m]) & 0xFFFFFFFF;   
            e = d;   
            d = c;   
            c = rotateleft(b,30);   
            b = a;   
            a = temp;   
    
        }   
        h0 = h0 + a;   
        h1 = h1 + b;   
        h2 = h2 + c;   
        h3 = h3 + d;   
        h4 = h4 + e;   
    
    }   
    
	//printf("Hash: %x %x %x %x %x",h0, h1, h2, h3, h4);
	std::string result;
	result=hex(h0);
	result=result.append(hex(h1));
	result=result.append(hex(h2));
	result=result.append(hex(h3));
	result=result.append(hex(h4));
	return result;
}

void main()   
{   
	coda:
	system("CLS");
	std::cout << SHA1((unsigned char *)"The quick brown fox jumps over the lazy dog") << std::endl;
	system("PAUSE");
	goto coda;
}

Is this not giving you an error :
"stream << hex << number;" ?

Make it stream <<std::hex << number; "

>> outputs a bunch of numbers
What what do you mean? In hex, just numbers could represent
some decimal numbers as well.

Comments
Great info with quick replies

Is this not giving you an error :
"stream << hex << number;" ?

Make it stream <<std::hex << number; "

>> outputs a bunch of numbers
What what do you mean? In hex, just numbers could represent
some decimal numbers as well.

Ow, the std::hex was what I was missing and surprisingly I didn't get any errors about that. Thanks for that - rep points added as your a genius.
Problem *Solved*

Hi and I am writing a small program but need to make a function that converts an integer to a hex. Below is an example of a similar function but need the result stored in a std::string variable.

printf("%x%x%x%x%x",h0, h1, h2, h3, h4);

Does anybody know how to create a hex() function which will do the same as the above except instead of printing the data it will store the data? I'm also using c++ and hoping this one will work in VC++.

Just use sprintf intead.
Ex:

char buffer[50];
sprintf(buffer, "%x%x%x%x%x",h0, h1, h2, h3, h4 );
Comments
nope
This question has already been answered. Start a new discussion instead.