Hexadecimal string to integer value

  • (2 Pages)
  • +
  • 1
  • 2

28 Replies - 23397 Views - Last Post: 02 October 2013 - 08:31 PM

#16 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5643
  • View blog
  • Posts: 12,359
  • 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: 1617
  • View blog
  • Posts: 5,707
  • 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 offline

  • Code herder
  • member icon

Reputation: 3168
  • View blog
  • Posts: 9,581
  • 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: 1617
  • View blog
  • Posts: 5,707
  • 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: 614
  • View blog
  • Posts: 1,873
  • 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 offline

  • Code herder
  • member icon

Reputation: 3168
  • View blog
  • Posts: 9,581
  • 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: 249
  • View blog
  • Posts: 582
  • 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: 1046
  • View blog
  • Posts: 4,451
  • 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 offline

  • Code herder
  • member icon

Reputation: 3168
  • View blog
  • Posts: 9,581
  • 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
  • +
  • -

#27 code_m  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 21
  • View blog
  • Posts: 194
  • Joined: 21-April 09

Re: Hexadecimal string to integer value

Posted 02 October 2013 - 10:29 AM

I still trying to get a C++ environment running since my ArchLinux install bit the dust. So here's my tested code in python 3.x:

Spoiler


And here is my C++ code (untested, and would need #include <string>, been a while since I worked with c-strings):

Spoiler


Note that I never check for invalid characters. I could do that, just felt it was out of the scope of the challenge.
Was This Post Helpful? 0
  • +
  • -

#28 Mike Chambers  Icon User is offline

  • D.I.C Head

Reputation: 22
  • View blog
  • Posts: 59
  • Joined: 30-March 09

Re: Hexadecimal string to integer value

Posted 02 October 2013 - 04:29 PM

This is copy and pasted straight out of a program I wrote a while back where it's used to parse hex values given in command line parameters. If there are any invalid characters, it simply returns a value of 0.

Spoiler

This post has been edited by Mike Chambers: 02 October 2013 - 04:30 PM

Was This Post Helpful? 0
  • +
  • -

#29 code_m  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 21
  • View blog
  • Posts: 194
  • Joined: 21-April 09

Re: Hexadecimal string to integer value

Posted 02 October 2013 - 08:31 PM

I really like your solution Mike. Mostly cause it's along the same lines of my idea. The major advantage is readability here, you don't have strange numbers to do the calculations, you can easily identify every calculation.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2