Sponsored Content
Full Discussion: to convert int to hex
Top Forums Programming to convert int to hex Post 302152328 by DreamWarrior on Wednesday 19th of December 2007 03:35:19 PM
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.
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
gnutls_hex2bin(3)						      gnutls							 gnutls_hex2bin(3)

NAME
gnutls_hex2bin - convert hex string into binary buffer. SYNOPSIS
#include <gnutls/gnutls.h> int gnutls_hex2bin(const char * hex_data, size_t hex_size, char * bin_data, size_t * bin_size); ARGUMENTS
const char * hex_data string with data in hex format size_t hex_size size of hex data char * bin_data output array with binary data size_t * bin_size when calling *bin_size should hold size of bin_data, on return will hold actual size of bin_data. DESCRIPTION
Convert a buffer with hex data to binary data. RETURNS
GNUTLS_E_SUCCESS on success, otherwise an error. SINCE
2.4.0 REPORTING BUGS
Report bugs to <bug-gnutls@gnu.org>. GnuTLS home page: http://www.gnu.org/software/gnutls/ General help using GNU software: http://www.gnu.org/gethelp/ COPYRIGHT
Copyright (C) 2008 Free Software Foundation. Copying and distribution of this file, with or without modification, are permitted in any medium without royalty provided the copyright notice and this notice are preserved. SEE ALSO
The full documentation for gnutls is maintained as a Texinfo manual. If the info and gnutls programs are properly installed at your site, the command info gnutls should give you access to the complete manual. gnutls 2.8.6 gnutls_hex2bin(3)
All times are GMT -4. The time now is 06:51 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy