Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > help needed on coversin of an char array to an integer

Reply
Thread Tools

help needed on coversin of an char array to an integer

 
 
Bartc
Guest
Posts: n/a
 
      02-21-2008

"MAx" <> wrote in message
news:6f4d18ab-ae46-4064-9d51-...
> Hi,
> Im kinda stuck in a project at a point where i need an array to
> be converted to a
> integer using some kind of math.
> This board does not support functions like scanf, sscanf etc as
> it does not have enough memory to hold their stack.
>
> given a string char str[8] = {'0', '0', '0' , '1' , 'f' , 'b' ,
> 'c' , 'a' };
> i need a function which can convert this array to an integer
> ( 0x0001fbca )


I assume sometimes the string will be something else? Otherwise it's rather
trivial.

Where are the hex digits coming from? If from a hex keyboard, then each
digit may already be 0 to 15 then combining to an integer value is very easy
(each digit corresponds to a 4-bit chunk).

Complete solutions have already been posted, the only tricky bit is
converting each character, 0 1 2 3 4 5 6 7 8 9 a b c d e f (and maybe A B C
D E F?) to a value 0 to 15.

--
Bart


 
Reply With Quote
 
 
 
 
MAx
Guest
Posts: n/a
 
      02-22-2008
On Feb 22, 12:55*am, "Bartc" <b...@freeuk.com> wrote:
> "MAx" <mahesh1...@gmail.com> wrote in message
>
> news:6f4d18ab-ae46-4064-9d51-...
>
> > Hi,
> > * * *Im kinda stuck in a project at a point where i need an array to
> > be converted to a
> > integer using some kind of math.
> > * * *This board does not support functions like scanf, sscanf etc as
> > it does not have enough memory to hold their stack.

>
> > * * *given a string *char str[8] = {'0', '0', '0' , '1' , 'f' , 'b' ,
> > 'c' , 'a' };
> > * * *i need a function which can convert this array to an integer
> > ( 0x0001fbca )

>
> I assume sometimes the string will be something else? Otherwise it's rather
> trivial.
>
> Where are the hex digits coming from? If from a hex keyboard, then each
> digit may already be 0 to 15 then combining to an integer value is very easy
> (each digit corresponds to a 4-bit chunk).
>
> Complete solutions have already been posted, the only tricky bit is
> converting each character, 0 1 2 3 4 5 6 7 8 9 a b c d e f (and maybe A B C
> D E F?) to a value 0 to 15.
>
> --
> Bart





Hey,
Thanks guys, got the code to work. MisterE's solution did the
trick. Thanks man.

 
Reply With Quote
 
 
 
 
Richard Heathfield
Guest
Posts: n/a
 
      02-22-2008
MAx said:

<snip>

> Hey,
> Thanks guys, got the code to work. MisterE's solution did the
> trick. Thanks man.



Unfortunately, there are circumstances in which that code *won't* do the
trick. Presumably you have investigated these circumstances and determined
that they will never apply to your code's user base?


--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
 
Reply With Quote
 
MisterE
Guest
Posts: n/a
 
      02-22-2008

> What makes you think 'A'..'F' (and lowercase) are indeed sequential?
> If you plan on posting C code for the OP, then.. at least post code
> that works.


I don't bother assuming he is one of 0.0000000000000000001% of people who
don't use ASCII character set for raw chars in c.


 
Reply With Quote
 
Richard Heathfield
Guest
Posts: n/a
 
      02-22-2008
MisterE said:

> I don't bother assuming he is one of 0.0000000000000000001% of people who
> don't use ASCII character set for raw chars in c.


If we assume everyone on the planet - around 6600000000 people - uses a
computer, then 0.0000000000000000001% of this would be 0.0000000000066
people. Effectively, you're claiming that at most one person on the planet
uses a non-ASCII system.

You are mistaken.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
 
Reply With Quote
 
Bartc
Guest
Posts: n/a
 
      02-22-2008

"Richard Heathfield" <> wrote in message
news:...
> MAx said:
>
> <snip>
>
>> Hey,
>> Thanks guys, got the code to work. MisterE's solution did the
>> trick. Thanks man.

>
>
> Unfortunately, there are circumstances in which that code *won't* do the
> trick. Presumably you have investigated these circumstances and determined
> that they will never apply to your code's user base?


MisterE has already indicated what he thinks the probability of that is:
something less than one person in the world (was it one trillionth of a
person?) having such a system. Well I wouldn't go that far myself..

MAx was talking about a board, which either gets distributed with the code,
or the customers have the same model.

But, you might be right, maybe in Europe they like keying in hex numbers
with accents (0001fbcá). That wouldn't work.

--
Bart




 
Reply With Quote
 
Old Wolf
Guest
Posts: n/a
 
      02-22-2008
On Feb 21, 10:43 pm, vipps...@gmail.com wrote:
>
> What makes you think 'A'..'F' (and lowercase) are indeed sequential?


Because they were/are sequential on every single
system that ever had a C compiler?

 
Reply With Quote
 
Willem
Guest
Posts: n/a
 
      02-22-2008
Richard wrote:
) Unfortunately, there are circumstances in which that code *won't* do the
) trick. Presumably you have investigated these circumstances and determined
) that they will never apply to your code's user base?

For the record, EBCDIC has A..F in sequential order, (and a..f as well).


SaSW, Willem
--
Disclaimer: I am in no way responsible for any of the statements
made in the above text. For all I know I might be
drugged or something..
No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT
 
Reply With Quote
 
vippstar@gmail.com
Guest
Posts: n/a
 
      02-22-2008
On Feb 22, 2:55 pm, Old Wolf <oldw...@inspire.net.nz> wrote:
> On Feb 21, 10:43 pm, vipps...@gmail.com wrote:
>
>
>
> > What makes you think 'A'..'F' (and lowercase) are indeed sequential?

>
> Because they were/are sequential on every single
> system that ever had a C compiler?

Let us assume you are correct here, what does that guarantee about the
future?
The standard only mentions '0'..'9' to be sequential.
 
Reply With Quote
 
Richard Tobin
Guest
Posts: n/a
 
      02-22-2008
In article <8f9482d4-5c87-4624-ab19->,
<> wrote:

>> > What makes you think 'A'..'F' (and lowercase) are indeed sequential?


>> Because they were/are sequential on every single
>> system that ever had a C compiler?


>Let us assume you are correct here, what does that guarantee about the
>future?


That the chance of your program being used on a computer with a
character set with non-sequential A-F is less than, say, the chance of
the computer being eaten by beetles?

-- Richard
--
:wq
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
length of 2D Array >> char **myString= (char **) malloc (sizeof (char *)); davidb C++ 0 09-01-2006 03:22 PM
(const char *cp) and (char *p) are consistent type, (const char **cpp) and (char **pp) are not consistent lovecreatesbeauty C Programming 1 05-09-2006 08:01 AM
Problem- strcat with char and char indexed from char array aldonnelley@gmail.com C++ 3 04-20-2006 07:32 AM
/usr/bin/ld: ../../dist/lib/libjsdombase_s.a(BlockGrouper.o)(.text+0x98): unresolvable relocation against symbol `std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostre silverburgh.meryl@gmail.com C++ 3 03-09-2006 12:14 AM
char->integer, integer->char commands christophergraber@gmail.com Perl Misc 5 11-22-2005 07:13 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57