convert signed char to hex

hi
i have a char * buffer; that stores a jpg file data. now i want to search for headers and extract data from it. as far i know , the data type is signed char (-128 to 127) and represent hex values.
how do i convert this signed chars to hex values or its dec equivalent?

  
unsigned char* unsigned_buffer = (unsigned char*) buffer;  
// assuming you're working on a big-endian system  
int16_t my_16_bit_int = unsigned_buffer[byte_index]*256+unsigned_buffer[byte_index+1];  
// or  
int16_t* short_buffer = (int16_t*) buffer;  
my_16_bit_int = short_buffer[byte_index/2];  
  

generally this will probably make you unhappy, though… why not just be lazy and pass the data to a jpeg handling library?

thats because i dont just decode the jfif data, i need to do some manipulation of huffman tables first. then decode the buffer with a jpg library and then draw the results

now this. one of the values that i need to extract is a two bytes hex. so at buffer[a] is the first byte and buffer[a+1] the second.
how do i get the two bytes toguether and get the dec value?

the code that damian posted above does exactly that…unless I’ve misunderstood the question…

ive got some progress, but still with problems
this is my func:

  
vector<int> dhtHeaders;  
  
vector< vector<int> > testApp::dumpHDT( char* data){ // data is buffer of jfif data  
  
	for (int i=0; i<lSize; i++){  
       if(data[i]==-1){ // find ff  
  
			if(data[i+1]==-60){ //find c4  
				if(int(data[i+4])>15){ // check fot dtc table  
				dhtHeaders.push_back( i);  
				}  
  
			}  
			if(data[i+1]==-38){ //check for next different marker (end of huffman marker)  
				break;  
			}  
		}  
	}  
	vector< vector<int> > result;  
	unsigned char* unsigned_buffer = (unsigned char*) data;   
	for(int i=0; i<dhtHeaders.size() ; i++){  
    
		int16_t my_16_bit_int = unsigned_buffer[dhtHeaders[i]+2]*256+unsigned_buffer[dhtHeaders[i]+3]-19; //lenght of the huffman table vals at ffc4 marker data  
		vector<int> huffV;  
		for(int j=0; j< my_16_bit_int ; j++){  
        
			int16_t huffval = unsigned_buffer[dhtHeaders[i]+j+21]; // huffmant vals  
			cout<<huffval<<endl;  
  
			huffV[j].push_back((int)huffval);  
			//cout<<"kkk "<<result[i][j]<<endl;  
  
		}  
		result[i].push_back(huffV);  
	}  
  
	return result;  
  
}  

im getting errors whe trying to populate the result vector of vector.

‘errors’? next time copy+paste the error message :slight_smile:

but from looking at the code i can see that you’re not initialising the result outer vector… before the start of the loop you need to go

  
results.resize( dhtHeaders.size() );  

otherwise when you go results[i] there’ll be nothing there to get.

here the errors:

testApp.cpp:81: error: request for member ‘push_back’ in ‘huffV. std::vector<_Tp, _Alloc>::operator[] with--Tp-=-int,--Alloc-=-std::allocator’, which is of non-class type ‘int’

testApp.cpp:85: error: no matching function for call to ‘std::vector<int, std::allocator >::push_back(std::vector<int, std::allocator >&)’

and the code again with the vector initialized:

  
vector<int> dhtHeaders;  
  
vector< vector<int> > testApp::dumpHDT( char* data){  
  
	for (int i=0; i<lSize; i++){  
       if(data[i]==-1){ // find ff  
  
			if(data[i+1]==-60){ //find c4  
				if(int(data[i+4])>15){ // check fot dtc table  
				dhtHeaders.push_back( i);  
				}  
  
			}  
			if(data[i+1]==-38){ //check for next different marker (end of huffman marker)  
				break;  
			}  
		}  
	}  
	vector< vector<int> > result;  
	result.resize( dhtHeaders.size() );  
	unsigned char* unsigned_buffer = (unsigned char*) data;   
	for(int i=0; i<dhtHeaders.size() ; i++){  
    
		int16_t my_16_bit_int = unsigned_buffer[dhtHeaders[i]+2]*256+unsigned_buffer[dhtHeaders[i]+3]-19; //lenght of the huffman table vals at ffc4 marker data  
		vector<int> huffV;  
		for(int j=0; j< my_16_bit_int ; j++){  
        
			int16_t huffval = unsigned_buffer[dhtHeaders[i]+j+21]; // huffmant vals  
			cout<<huffval<<endl;  
  
			huffV[j].push_back((int)huffval);  
			//cout<<"kkk "<<result[i][j]<<endl;  
  
		}  
		result[i].push_back(huffV);  
	}  
  
	return result;  
  
}  
  
  

you create a 1D vector

  
vector<int> huffV;  

… but then try and access as though it’s 2D

  
huffV[j].push_back((int)huffval);  

you probably mean

  
huffV.push_back(huffval);  

it works. thanks a lot!