Thread: String to Numeric Value

  1. #16
    Registered User
    Join Date
    Sep 2012
    Posts
    15
    My mistake with the code above (double quotes). In my actual program I am sending a single char (e.g. HexValue(str[0])). It works fine however I figure I might be going about it the hard way. Regarding PRINTF() ... If I use the %d format specifier then it displays the ASCII decimal value (e.g. 'A' = 97) and if I use the %X it displays the ASCII hexadecimal value (e.g. '7' = 37). I don't want that. I need to do math calcs so I need the actual integer so that 'A' is 10 and 'F' is 15 and '7' is 7, etc. So I ask again is my HexValue() function a bad solution??

  2. #17
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Yes. I would call it <yikes!>, but you're a beginner, and I'm being nice here.

    Char's have a value that is "bumped up" by ascii. So a '1' for instance, is not a 1 in ascii. You know all this. C has more than one way to swap char values for int values, and both values for hex values, and octal values, and on and on.

    You're HexValue function is re-inventing the wheel. I'm not saying it's not an OK logic/code puzzle to figure out, but it seems like it's such an uninteresting puzzle to spend time on.

    When I saw your devotion to the HexValue function, I stopped reading this thread for awhile. I'll go back and re-read the problem, and get you nudged into something more practical -- unless of course, you want to continue working on this HexValue, as a puzzle.

    I would suggest changing the entire string to a number via atoi() or strtoi(). Both functions can convert with a change in the last letter of the function name, to long int's, etc. strtoi() also has the extra ability to convert to different number bases, (not just the normal base 10).

    Either one should be able to do what you want, in one or two lines of code.

    If that is NOT what you want, give me a step by step simple example of exactly what you want: input, and output.
    Last edited by Adak; 09-23-2012 at 06:06 PM.

  3. #18
    Registered User
    Join Date
    Sep 2012
    Posts
    15
    I'm just amazed at the relative barbaric nature of C. I've done a little programming in ASP VBScript, Excel/Word Macro languages and this C language is like pulling teeth. Doing simple things are like monumental tasks. I don't doubt the power of C after all the Mars Lander Curiosity is completely programmed using C. Of course, this exercise is for college and I think they like to make you figure out how exactly the language works versus just giving you the tools and letting you program away. Any help is appreciated.

  4. #19
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Understand that C needs to do a lot of the same stuff, over and over, and it wasn't designed by "dentists". You're just not using it well.

    Show input and output you want in an example. Obviously, you're smart, and I'm NOT seeing why the "dentists" are flocking to your problem, sans novocaine.

  5. #20
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    948
    Quote Originally Posted by rwagstaff View Post
    I'm just amazed at the relative barbaric nature of C. I've done a little programming in ASP VBScript, Excel/Word Macro languages and this C language is like pulling teeth. Doing simple things are like monumental tasks. I don't doubt the power of C after all the Mars Lander Curiosity is completely programmed using C. Of course, this exercise is for college and I think they like to make you figure out how exactly the language works versus just giving you the tools and letting you program away. Any help is appreciated.
    C is much older than all of the languages you listed, and despite C being called a "high-level language", it was designed for relatively low-level tasks like systems programming. This exercise does sound pointless on its own (because C has functions like strtol that do what you want already), but apparently your college class wants you to figure out how to do this particular task without using any built-in function. You shouldn't blame C for that. Blame your instructor. :P

    Edit: Adak: strtoi is a non-standard function (it doesn't exist in the standard library). strtol can be used instead, and its return value can be cast to int if needed.
    Last edited by christop; 09-23-2012 at 06:18 PM.

  6. #21
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Thanks, christop. My compiler has every non-standard function in the book, I believe -- I love it. (Pelles C).

    Is that it then? Is this an assignment to do things the hard way, because it's a good brain teaser?

    We need a straight answer on this question, rwagstaff.

  7. #22
    Registered User
    Join Date
    Sep 2012
    Posts
    15
    Thanks again guys. What's a good web site that has a C function reference?? Basic definitions, use, examples and the like. Just to clear up some of the mystery the assignment is to input a hexadecimal string and determine if it is negative and output the decimal equivalent using 2's complement. So I guess an example is: input>>CAFE output>>negative number...-13570 (0xCAFE = 51966, 2s complement makes it 0x3502 >> -13570 in decimal). I think I did that right. Seems pointless to me. I really don't care how the damn computer processes a negative number but apparently it is critically important to the professor. =)

  8. #23
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    You'd be better off using the other technique mentioned above using the values from the ASCII chart and doing a simple math equation. You'd save yourself about 65 line and get rid of the switch. Posts #2-6.
    Last edited by WaltP; 09-23-2012 at 07:45 PM.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  9. #24
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by WaltP
    And when you get to the next language that doesn't have an strtol-like function, you are a cripple.
    You missed my point: since the function exists in the standard library, you should use it unless you have a special reason to do otherwise (learning how to implement it being one of the legitimate special reasons).

    Quote Originally Posted by WaltP
    I've never had a function like that in any other language I've used, and I have never used that function in over 25 years of professional C work.
    I have seen functions like that in other languages that I have used, and some of them are even part of the core language, but so what? Yes, a programmer should learn how to implement something as basic as this, but if there is the need to convert a string to long (or int), the standard library function should be preferred, so grumpy mentioning strtol an option for (future) use makes sense. Unless you're telling me that you only use your own personal library that duplicates the functionality in the standard library?

    Quote Originally Posted by rwagstaff
    Please review the function below
    You do not need all the break statements because you are already returning a value before each of them. You can also make use of follow through, e.g.,
    Code:
    case 'A':
    case 'a':
        return 10;
    Quote Originally Posted by rwagstaff
    What's a good web site that has a C function reference?
    Are you using an introductory book? It may have such a reference for you. That said...

    Quote Originally Posted by rwagstaff
    Of course, this exercise is for college and I think they like to make you figure out how exactly the language works versus just giving you the tools and letting you program away.
    ... you should learn both how to use the tools and how the tools work. The "how the tools work" part is the crux of WaltP's objection in post #12. Based on your assignment requirements ("using 2's complement"), it does sound like the point here is to learn how the tools work, rather than this being just incidental to completing your assignment.

    After you remove those breaks, your HexValue function would be acceptable, but you really don't gain much from using that switch instead of WaltP's suggestion. As iMalc stated, the digits are guaranteed to be in sequence from '0' to '9'. It isn't actually guaranteed that 'A' to 'F' will be in sequence, but ASCII or an ASCII based character set is very likely to be what you'll ever use, so that isn't a realistic problem.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #25
    Registered User
    Join Date
    Jul 2012
    Location
    Australia
    Posts
    242
    Quote Originally Posted by rwagstaff View Post
    What's a good web site that has a C function reference?? Basic definitions, use, examples and the like.
    I use the The GNU C Library Reference Manual. It has some Posix(Unix) extensions which I find quite useful. Functions that are non-ansi(or require the gcc compiler) will be clearly marked as so. Download the PDF and check it out. It's great.

    The GNU C Library - GNU Project - Free Software Foundation (FSF)
    Last edited by cfanatic; 09-23-2012 at 09:01 PM.
    IDE: Code::Blocks | Compiler Suite for Windows: TDM-GCC (MingW, gdb)

  11. #26
    Registered User
    Join Date
    Jul 2012
    Location
    Australia
    Posts
    242
    Quote Originally Posted by rwagstaff View Post
    I'm just amazed at the relative barbaric nature of C.
    Hahaha. I'll give you a like just for this sentence.
    IDE: Code::Blocks | Compiler Suite for Windows: TDM-GCC (MingW, gdb)

  12. #27
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by rwagstaff View Post
    I'm just amazed at the relative barbaric nature of C.
    Nah, barbaric would be manually pushing electrons around.
    C is at least a couple of levels of abstraction above that!
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  13. #28
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by WaltP View Post
    And when you get to the next language that doesn't have an strtol-like function, you are a cripple. C is not the only language out there. and presumably is not the last one to be learned.

    I've never had a function like that in any other language I've used, and I have never used that function in over 25 years of professional C work. Knowing how to do something has helped me greatly.
    It clearly hasn't helped you develop either manners or common sense though.

    I simply named a function in the C standard library that was directly relevant to the original problem, and did not insult anyone who offered an alternative technique.

    Yes, there is value in knowing how to do things. There is also value in knowing how to reuse existing facilities. I would suggest that someone who does not understand reuse is exaggerating in describing himself or herself as a professional.
    Last edited by grumpy; 09-24-2012 at 02:30 AM.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  14. #29
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    948
    Quote Originally Posted by WaltP View Post
    I've never had a function like that in any other language I've used, and I have never used that function in over 25 years of professional C work. Knowing how to do something has helped me greatly.
    Which languages have you used? Of the languages I've used/learned/read about, most have a function/method for converting a string to a number. This includes C, Java, Javascript, Ada, Perl, Python, C#, C++, FORTRAN, Lua, REXX, Ruby, PHP, and the list goes on. On the other hand, the assembly languages I use don't have built-in methods.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 04-09-2012, 10:30 PM
  2. alpha-numeric to numeric program
    By johnhuge in forum C Programming
    Replies: 2
    Last Post: 03-24-2012, 12:08 AM
  3. C++: Converting Numeric String to Alpha String
    By JosephCardsFan in forum C++ Programming
    Replies: 3
    Last Post: 02-16-2005, 07:07 AM
  4. Numeric to string help
    By StupidQuestion in forum C++ Programming
    Replies: 2
    Last Post: 01-08-2003, 12:56 PM
  5. Is there a function to convert a numeric value into a string?
    By sundeeptuteja in forum C Programming
    Replies: 6
    Last Post: 11-06-2002, 02:11 PM