|
-
May 12th, 2004, 12:28 PM
#1
hex char to int in C
I'm trying to feed a single char (as an int) to this function an then get back the hex value but I"m getting complier errors...
Code:
254>int hex_to_int(int c)
255>{
256>
257> if((c - (int)'0' >= 0 )&&(c - (int)'0' < 10))
258> {
259> return (c - (int)'0');
260> }
261>
262> char hexalpha[] = "aAbBcCdDeEfF";
263> int i;
264> int answer = 0;
265>
266> for(i = 0; answer == 0 && hexalpha[i] != '\0'; i++)
267> {
268> if(hexalpha[i] == c)
269> {
270> answer = 10 + (i / 2);
271> }
272> }
273>
274> return answer;
275>}
gives me:
C:\uiox_dev\UIOX Sources\Rcl.c(262) : error C2143: syntax error : missing ';' before 'type'
C:\uiox_dev\UIOX Sources\Rcl.c(263) : error C2143: syntax error : missing ';' before 'type'
C:\uiox_dev\UIOX Sources\Rcl.c(264) : error C2143: syntax error : missing ';' before 'type'
C:\uiox_dev\UIOX Sources\Rcl.c(266) : error C2065: 'i' : undeclared identifier
C:\uiox_dev\UIOX Sources\Rcl.c(266) : error C2065: 'answer' : undeclared identifier
C:\uiox_dev\UIOX Sources\Rcl.c(266) : error C2065: 'hexalpha' : undeclared identifier
C:\uiox_dev\UIOX Sources\Rcl.c(266) : error C2109: subscript requires array or pointer type
C:\uiox_dev\UIOX Sources\Rcl.c(268) : error C2109: subscript requires array or pointer type
any ideas where these are coming from?
thanks
-
May 12th, 2004, 12:50 PM
#2
In 'C' variable declarations must be at the beginning of a scope block. C++ allows them to be moved to the point of nearest use, but not 'C'...
TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
2008, 2009,2010
In theory, there is no difference between theory and practice; in practice there is.
* Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions 
* How NOT to post a question here
* Of course you read this carefully before you posted
* Need homework help? Read this first
-
May 12th, 2004, 08:41 PM
#3
Originally posted by TheCPUWizard
In 'C' variable declarations must be at the beginning of a scope block. C++ allows them to be moved to the point of nearest use, but not 'C'...
I think C99 standard allows that. But of course, since most C compiler is not C99 compliant yet, declaration must be placed right at the beginning of the scope block.
-
May 13th, 2004, 01:48 AM
#4
You can also try this by changing you file extension to .cpp from .c. As 'TheCPUWizard' mentioned, you can define the scope of those variables, by using the '{' and '}' braces.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|