Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
And currently I'm using "Uri" (Uniform Resource Identifier) method that's available to me as I use VS1010. Everything works ok now but I'm thinking there must be an older more C++ style method to convert a "15" to a "F".
Posted

It's not clear at all what you are looking for, but here's the standard "old school" answers:

Standard K&R C:

C++
char buffer[256];

sprintf(buffer,"%04x", integerValueYouWantToDisplayAsHex);


Seriously old school (if you don't have the C-runtime):
C++
char output_buffer[5];
char toHex[] = "0123456789ABCDEF";

for (int i = 3; i > 0; i--) {
    output_buffer[3-i] = toHex[(inputValue >> (i * 4)) & 0xff];
}
output_buffer[4] = 0;
 
Share this answer
 
Comments
JackDingler 20-Jan-12 17:34pm    
The correct answer seems to be, that he didn't want an answer...
TRK3 20-Jan-12 17:40pm    
Yeah. Oh well, I always like to dust of my K&R C...
RedDk 20-Jan-12 17:39pm    
YES!

That is the type of thing I set out to do when I started. I ran into Uri::HexEscape by searching the assemblies through the VS2010 interface on "hex" and it's only thing that was returned. I thought, "where's the STL function"?

Thank-you for the code. Unicode is/has-been/always-will-be a thorn in my side.
TRK3 20-Jan-12 17:45pm    
Great. I highly recommend the original Kernighan & Richie "C" book if you haven't read it.

http://www.amazon.com/Programming-Language-2nd-Brian-Kernighan/dp/0131103628

The original C is so simple and concise that I find it far easier to do things using the basic tools of C then it is to go searching through vast new frameworks and libraries to find the thing that does it -- especially when it's something like this that really ought to be simple to do.
The number itself is stored as binary... the representation is just a human readable thing. So if you want to view it differently, use a string and display it however you want by just formatting your string accordingly.

Choose a string type that you want to use, and search for available format specifiers for details.
 
Share this answer
 
v2
Comments
RedDk 20-Jan-12 16:29pm    
Without a note on "Uri" here in the answer I suspect that you're possibly suggesting I do something in C++ and leverage a lower level operator. Anyway, I probably should have flagged a key word ... How about Uri::HexEscape?

It works.
Albert Holguin 20-Jan-12 16:49pm    
What I'm saying is.... your question is trivial... as posted, what do you mean you want to convert a 15 to F? ...is the 15 a string already? ...if it's an int value, then the decimal or hexadecimal representations are merely human readable representations.
One old school way to do this, is to create a static array of 256 strings that denote the hex data.

C++
char szHexLookup[256][4] = 
{
"00","01","02","03","04","05","06","07","08","09","0A","0B","0C","0D","0E","0F", 
"10","11","12","13","14","15","16","17","18","19","1A","1B","1C","1D","1E","1F", 
"20","21","22","23","24","25","26","27","28","29","2A","2B","2C","2D","2E","2F", 
"30","31","32","33","34","35","36","37","38","39","3A","3B","3C","3D","3E","3F", 
"40","41","42","43","44","45","46","47","48","49","4A","4B","4C","4D","4E","4F", 
"50","51","52","53","54","55","56","57","58","59","5A","5B","5C","5D","5E","5F", 
"60","61","62","63","64","65","66","67","68","69","6A","6B","6C","6D","6E","6F", 
"70","71","72","73","74","75","76","77","78","79","7A","7B","7C","7D","7E","7F", 
"80","81","82","83","84","85","86","87","88","89","8A","8B","8C","8D","8E","8F", 
"90","91","92","93","94","95","96","97","98","99","9A","9B","9C","9D","9E","9F", 
"A0","A1","A2","A3","A4","A5","A6","A7","A8","A9","AA","AB","AC","AD","AE","AF", 
"B0","B1","B2","B3","B4","B5","B6","B7","B8","B9","BA","BB","BC","BD","BE","BF", 
"C0","C1","C2","C3","C4","C5","C6","C7","C8","C9","CA","CB","CC","CD","CE","CF", 
"D0","D1","D2","D3","D4","D5","D6","D7","D8","D9","DA","DB","DC","DD","DE","DF", 
"E0","E1","E2","E3","E4","E5","E6","E7","E8","E9","EA","EB","EC","ED","EE","EF", 
"F0","F1","F2","F3","F4","F5","F6","F7","F8","F9","FA","FB","FC","FD","FE","FF", 
};


You then walk through your source string, and use each character as an index to look up the corresponding hex representation.

I set the length of each string to 4 bytes so that they would all be aligned on a 4 byte boundary.

An alternative would be to make this just one 256 byte string and find the bytes by multiplying the index by 2 (shift left 1 bit for speed "Index << 1;) to find the string start.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900