to convert int to hex


 
Thread Tools Search this Thread
Top Forums Programming to convert int to hex
# 1  
Old 12-15-2007
to convert int to hex

Hi,

Can you help me in converting int value to hex in a single command.

Thanks
# 2  
Old 12-15-2007
One way fo doing it is with the printf utility i.e.

Code:
printf "%x\n", 123
7b

# 3  
Old 12-15-2007
I am looking for a function/call which does that
# 4  
Old 12-15-2007
Quote:
Originally Posted by naan
I am looking for a function/call which does that
printf is a function call.

also sprintf, vsprintf etc.
# 5  
Old 12-15-2007
The thing to remember is that you are not converting the value, but merely displaying it in a different format.

Last edited by prowla; 12-15-2007 at 03:14 PM..
# 6  
Old 12-18-2007
Here's a C++ function

#include <string>
#include <sstream>
using namespace std;

//This spends some extra cycles, offers simplicity, elegance and safety.
string IntToHexString(int i)
{
stringstream ss;
ss<<"0x"<<hex<<i;
string retval; retval=ss.str().c_str();
return retval;
}


//usage
cout<<"Hex for 32 is "<<IntToHexString(32)<<endl;
// as always handle your seh exceptions with a try / catch.

Last edited by WebKruncher; 12-18-2007 at 02:51 PM.. Reason: typo.
# 7  
Old 12-19-2007
Quote:
Originally Posted by WebKruncher
#include <string>
#include <sstream>
using namespace std;

//This spends some extra cycles, offers simplicity, elegance and safety.
string IntToHexString(int i)
{
stringstream ss;
ss<<"0x"<<hex<<i;
string retval; retval=ss.str().c_str();
return retval;
}


//usage
cout<<"Hex for 32 is "<<IntToHexString(32)<<endl;
// as always handle your seh exceptions with a try / catch.
Correct me if I am wrong, but it also either 1) leaks memory, 2) reads a wild pointer, or 3) is thread unsafe:

Logically:

The .c_str call must return a "string".

Given:

A string is a pointer to a "NULL terminated string."

A "NULL terminated string" is a sequence of characters terminated by a '\0' character; we shall call this a "buffer".

A buffer resides somewhere in memory.

Therefore:

The .c_str call must return a pointer a buffer containing its contents.

In order not to leak, the above buffer must be freed at some point. To be freed the buffer must still referenced. Since the caller of IntToHexString does not take a reference then to not leak, either a) someone else must free it or b) someone else has a reference to it.

If (a) then the "somewhere" else may be the destructor of stringstream class. I presume this destructor is called when the stack for your function is cleaned up. This means that the string returned by IntToHexString becomes "wild" as soon as it returns.

If (b) then the buffer can be overwritten at some point by the class still holding a reference to it and therefore can not be thread safe.

If neither a nor b occurs then the code leaks. The only way this is untrue is if the compiler takes care of destroying the string returned by IntToHexString.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Convert int to string in python

Hi, I have column 5 in a file which contains string like this for ex. RP11-125O5.2 SLCO1B1 CAPN1 FRMPD2 TXNL4B So I do by data = )] ValueError: invalid literal for int() with base 10: 'R' Can someday tell me how to convert this column into int successfully. Thank You in... (7 Replies)
Discussion started by: rossi
7 Replies

2. Shell Programming and Scripting

Convert hex to decimal

can someone help me in converting hex streams to decimal values using perl script Hex value: $my_hex_stream="0c07ac14001676"; Every hex value in the above stream should be converted in to decimal and separated by comma. The output should be: 12,07,172,20,00,22,118 (2 Replies)
Discussion started by: Arun_Linux
2 Replies

3. Shell Programming and Scripting

Convert to Hex in perl

Hi, i want to convert number 5860533159 to hexadecimal. i need to use perl. i used $foo = 5860533159; $hexval3 = sprintf("%#x", $foo); i am getting value as 0xffffffff. i need to get value as 0x15D50A3A7. when i converted using google calculator, i got the correct value, expected... (9 Replies)
Discussion started by: asak
9 Replies

4. Shell Programming and Scripting

Convert Hex - KSH

Hello, I woild like to convert hex on KSH not BASH: I tried to use: tmp=31 printf "\x"${tmp}"" it works on bash - Output is '1' but not on ksh. please advice on the right syntax. Thanks. (4 Replies)
Discussion started by: LiorAmitai
4 Replies

5. Programming

Handle int listen(int sockfd, int backlog) in TCP

Hi, from the manual listen(2): listen for connections on socket - Linux man page It has a parameter called backlog and it limits the maximum length of queue of pending list. If I set backlog to 128, is it means no more than 128 packets can be handled by server? If I have three... (3 Replies)
Discussion started by: sehang
3 Replies

6. Shell Programming and Scripting

Convert hex to decimal or reverse is better?

Please Help Me! about the problem down under. I have 2 files with nearly the same characteristics, I have to convert one to the other format or the other format to one's format. I want to write it with awk. The first file contain lines like this: 300000001#A#Y#Y#Y#Y The other file contain... (4 Replies)
Discussion started by: Axel82
4 Replies

7. Solaris

Convert DWORD to int on solaris for 64-bit

i am converting application from 32-bit to 64-bit on Solaris. How can I convert "DWORD" on Solaris 64-bit to int. Thanks. (1 Reply)
Discussion started by: amit_27
1 Replies

8. Programming

Convert HEX to EBCDIC IN C

i want to convert Hex value To EBCDIC value. i tried to convert hex to ascii and then to ebcdic but it doesn't give desired results . it doesn't give corresponding ebcdic value instead it gives some junk values. e.g; Hex EBCDIC ----------------- 81 a 82 b 83 c 84 d 85 e 86 f 87... (6 Replies)
Discussion started by: junaid.nehvi
6 Replies

9. Shell Programming and Scripting

How to Convert Hex value to Dec ?

Hi All, I want to convert below Hex value to Dec value in each column .How to do it ? This data is in a 1 file. 4e20 0475 2710 010f 7530 69a2 7530 7e2f 4e20 02dd 7530 6299 4e20 0c0a 7530 69a2 4e20 0a0b 2710 0048 7530 7955 4e20 0d23 7530 622d 7530 9121 2710 001f 7530 7d3f (6 Replies)
Discussion started by: Nayanajith
6 Replies

10. Shell Programming and Scripting

how to convert a string to int

Hi, i want to read a text file whose content(single line) will be a number like 1 or 2 or 3 ..... what i want to do is to read the file and increment the content of the file, using unix scripting. Regards, Senthil Kumar Siddhan. (2 Replies)
Discussion started by: senthilk615
2 Replies
Login or Register to Ask a Question