Ascii String to Intger

W

Willem

What is the best way to calculate an ascii string into an integer (not
talking about an atoi conversion):

For examle if I have the ascii string: "/b" then in hex it would be
2F7A and if I convert that to decimal I would get 12154.

I can't figure out how to concatenate? my hex values together if that
makes any sense?

Any pointers would be greatly appreciated!
 
B

Ben Pfaff

Willem said:
What is the best way to calculate an ascii string into an integer (not
talking about an atoi conversion):

For examle if I have the ascii string: "/b" then in hex it would be
2F7A and if I convert that to decimal I would get 12154.

Assuming that the string is already in ASCII:
(s[0] << 8) | s[1]
Otherwise you'll have to convert the string to ASCII first.
 
W

Willem

Holy cow thank you,

That was so simple! Thank you so very much - now that it works I have
to figure out why it works.

Willem said:
What is the best way to calculate an ascii string into an integer (not
talking about an atoi conversion):

For examle if I have the ascii string: "/b" then in hex it would be
2F7A and if I convert that to decimal I would get 12154.

Assuming that the string is already in ASCII:
(s[0] << 8) | s[1]
Otherwise you'll have to convert the string to ASCII first.
 
A

August Derleth

Willem said:
What is the best way to calculate an ascii string into an integer (not
talking about an atoi conversion):

For examle if I have the ascii string: "/b" then in hex it would be
2F7A and if I convert that to decimal I would get 12154.

Assuming that the string is already in ASCII:
(s[0] << 8) | s[1]
Otherwise you'll have to convert the string to ASCII first.
Holy cow thank you,

That was so simple! Thank you so very much - now that it works I have
to figure out why it works.

First, don't top-post. Replies go /below/ what you are replying to. I've
fixed it because it's very annoying to most people on this group.

Second, /why/ it works is an exercise in binary logic. Draw it on a paper,
remembering that in ASCII, each character is eight bits wide, and that |
is the bitwise OR operator.

(If you can /see/ why it works, bitwise operations will forever be much
easier for you. And finding it out for yourself should be rewarding.
Otherwise, you can make more money getting an MBA instead of programming.)
 
R

Ravi Uday

Ben Pfaff said:
Willem said:
What is the best way to calculate an ascii string into an integer (not
talking about an atoi conversion):

For examle if I have the ascii string: "/b" then in hex it would be
2F7A and if I convert that to decimal I would get 12154.

Assuming that the string is already in ASCII:
(s[0] << 8) | s[1]
Otherwise you'll have to convert the string to ASCII first.


But this conversion does not give 12154 ! as the OP asked for.
It prints 12130 instead.

- Ravi
 
S

Stephen Sprunk

Ravi Uday said:
Ben Pfaff <[email protected]> wrote in message
Willem said:
What is the best way to calculate an ascii string into an integer (not
talking about an atoi conversion):

For examle if I have the ascii string: "/b" then in hex it would be
2F7A and if I convert that to decimal I would get 12154.

Assuming that the string is already in ASCII:
(s[0] << 8) | s[1]
Otherwise you'll have to convert the string to ASCII first.

But this conversion does not give 12154 ! as the OP asked for.
It prints 12130 instead.

That's because the ASCII string "/b" is 0x2F 0x62 0x00, not 0x2F 0x7A 0x00.

S
 
C

Case

Ravi said:
Ben Pfaff said:
What is the best way to calculate an ascii string into an integer (not
talking about an atoi conversion):

For examle if I have the ascii string: "/b" then in hex it would be
2F7A and if I convert that to decimal I would get 12154.

Assuming that the string is already in ASCII:
(s[0] << 8) | s[1]
Otherwise you'll have to convert the string to ASCII first.



But this conversion does not give 12154 ! as the OP asked for.
It prints 12130 instead.

- Ravi

The OP is wrong: 'b' == 0x62. (0x7a == 'z')

Kees
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,141
Messages
2,570,817
Members
47,367
Latest member
mahdiharooniir

Latest Threads

Top