Hexadecimal string to integer value

  • (2 Pages)
  • +
  • 1
  • 2

25 Replies - 14358 Views - Last Post: 26 February 2013 - 07:37 AM

#16 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 4951
  • View blog
  • Posts: 11,358
  • Joined: 16-October 07

Re: Hexadecimal string to integer value

Posted 27 July 2012 - 11:04 AM

View PostSkydiver, on 27 July 2012 - 01:29 PM, said:

I'm curious which compiler you are using that has 64-bit integers as its default.


Using gcc.

This is the behavior:
[baavgai@DIC]$ cat > a.cc
#include <iostream>

int main() {
	int n = 0x800000000;
	std::cout << "n = " << n << std::endl;
	return 0;
}
[baavgai@DIC]$ g++ -Wall a.cc
a.cc: In function ‘int main()’:
a.cc:4: warning: overflow in implicit constant conversion
[baavgai@DIC]$ ./a.out 
n = 0
[baavgai@DIC]$


Was This Post Helpful? 0
  • +
  • -

#17 ishkabible  Icon User is offline

  • spelling expret
  • member icon





Reputation: 1541
  • View blog
  • Posts: 5,540
  • Joined: 03-August 09

Re: Hexadecimal string to integer value

Posted 27 July 2012 - 12:34 PM

I was bound and determined to fit this into 1 line so here it is.

Spoiler

This post has been edited by ishkabible: 27 July 2012 - 12:45 PM

Was This Post Helpful? 0
  • +
  • -

#18 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 2039
  • View blog
  • Posts: 6,072
  • Joined: 05-May 12

Re: Hexadecimal string to integer value

Posted 27 July 2012 - 06:49 PM

Here's a different approach that does things the slow way. The approach is better suited for Base64 unencoding rather than hex2bin:
Spoiler

Was This Post Helpful? 0
  • +
  • -

#19 ishkabible  Icon User is offline

  • spelling expret
  • member icon





Reputation: 1541
  • View blog
  • Posts: 5,540
  • Joined: 03-August 09

Re: Hexadecimal string to integer value

Posted 27 July 2012 - 07:20 PM

here's the clean version of mine; I think the bitwise OR trick is really helpful even for serious code...serious code is just more boring :P

Spoiler

This post has been edited by ishkabible: 27 July 2012 - 07:36 PM

Was This Post Helpful? 0
  • +
  • -

#20 Aphex19  Icon User is offline

  • Born again Pastafarian.
  • member icon

Reputation: 605
  • View blog
  • Posts: 1,868
  • Joined: 02-August 09

Re: Hexadecimal string to integer value

Posted 28 July 2012 - 06:01 PM

I modified the style a bit and added a small "mystrlen" utility function. Seems that all tests pass.

Spoiler

This post has been edited by Aphex19: 28 July 2012 - 06:29 PM

Was This Post Helpful? 0
  • +
  • -

#21 auch  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 3
  • Joined: 08-November 12

Re: Hexadecimal string to integer value

Posted 08 November 2012 - 08:43 PM

How about that:

int hex2bin(const char *hexString)
{
   return strtol(hexString, (char **)NULL, 16);
}


Was This Post Helpful? 0
  • +
  • -

#22 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 2039
  • View blog
  • Posts: 6,072
  • Joined: 05-May 12

Re: Hexadecimal string to integer value

Posted 09 November 2012 - 07:34 AM

Unfortunately, stdlib.h or cstdlib was not included in the template program as shown in post #1, so strtol() would not be declared.
Was This Post Helpful? 0
  • +
  • -

#23 Xupicor  Icon User is offline

  • Nasal Demon
  • member icon

Reputation: 247
  • View blog
  • Posts: 581
  • Joined: 31-May 11

Re: Hexadecimal string to integer value

Posted 10 November 2012 - 12:17 PM

I had to correct the OP code to get it to compile without warnings. :P const correctness! :P
Here's my - probably most unoriginal - implementation.
Spoiler

This post has been edited by Xupicor: 10 November 2012 - 03:08 PM

Was This Post Helpful? 0
  • +
  • -

#24 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 885
  • View blog
  • Posts: 4,038
  • Joined: 09-June 09

Re: Hexadecimal string to integer value

Posted 10 November 2012 - 06:15 PM

Here's mine, I decided to do it in C :)/>/>
Spoiler

This post has been edited by jimblumberg: 10 November 2012 - 07:13 PM
Reason for edit:: Added spoiler tags

Was This Post Helpful? 0
  • +
  • -

#25 JDX  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 21-February 13

Re: Hexadecimal string to integer value

Posted 25 February 2013 - 01:58 PM

I know this isn't efficient but it was good practice for using chars.
Spoiler

Was This Post Helpful? 0
  • +
  • -

#26 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 2039
  • View blog
  • Posts: 6,072
  • Joined: 05-May 12

Re: Hexadecimal string to integer value

Posted 26 February 2013 - 07:37 AM

On your line 6, you have no guarantee that buffer[1] is zero, so you have no guarantees that the string in buffer is null terminated.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2