473,320 Members | 2,035 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Ascii String to Intger

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!
Nov 14 '05 #1
6 2006
Willem <sw******@hotmail.com> writes:
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.
Nov 14 '05 #2
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.

On Sun, 25 Apr 2004 17:26:03 -0700, Ben Pfaff <bl*@cs.stanford.edu>
wrote:
Willem <sw******@hotmail.com> writes:
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.


Nov 14 '05 #3
On Mon, 26 Apr 2004 00:39:21 +0000, Willem wrote:
On Sun, 25 Apr 2004 17:26:03 -0700, Ben Pfaff <bl*@cs.stanford.edu>
wrote:
Willem <sw******@hotmail.com> writes:
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.)

--
yvoregnevna gjragl-guerr gjb-gubhfnaq guerr ng lnubb qbg pbz
To email me, rot13 and convert spelled-out numbers to numeric form.
"Makes hackers smile" makes hackers smile.

Nov 14 '05 #4
Ben Pfaff <bl*@cs.stanford.edu> wrote in message news:<87************@blp.benpfaff.org>...
Willem <sw******@hotmail.com> writes:
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
Nov 14 '05 #5
"Ravi Uday" <ra*****@yahoo.com> wrote in message
news:ec**************************@posting.google.c om...
Ben Pfaff <bl*@cs.stanford.edu> wrote in message

news:<87************@blp.benpfaff.org>...
Willem <sw******@hotmail.com> writes:
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

--
Stephen Sprunk "Stupid people surround themselves with smart
CCIE #3723 people. Smart people surround themselves with
K5SSS smart people who disagree with them." --Aaron Sorkin

Nov 14 '05 #6
Ravi Uday wrote:
Ben Pfaff <bl*@cs.stanford.edu> wrote in message news:<87************@blp.benpfaff.org>...
Willem <sw******@hotmail.com> writes:

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

Nov 14 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: b.stewart | last post by:
I have only been using C++ for a few afternoons and i still get confused at nearly every line i type so maybe someone here can help me. Im trying to write a small program that can change a...
11
by: Kai Bohli | last post by:
Hi all ! I need to translate a string to Ascii and return a string again. The code below dosen't work for Ascii (Superset) codes above 127. Any help are greatly appreciated. protected...
3
by: JSM | last post by:
Hi, I am just trying to port an existing simple encryption routine to C#. this routine simply adds/substracts 10 ascii characters to each character in a text file (except quotes). The routine...
18
by: Ger | last post by:
I have not been able to find a simple, straight forward Unicode to ASCII string conversion function in VB.Net. Is that because such a function does not exists or do I overlook it? I found...
24
by: ChaosKCW | last post by:
Hi I am reading from an oracle database using cx_Oracle. I am writing to a SQLite database using apsw. The oracle database is returning utf-8 characters for euopean item names, ie special...
18
by: John | last post by:
Hi, I'm a beginner is using C# and .net. I have big legacy files that stores various values (ints, bytes, strings) and want to read them into a C# programme so that I can store them in a...
399
by: =?UTF-8?B?Ik1hcnRpbiB2LiBMw7Z3aXMi?= | last post by:
PEP 1 specifies that PEP authors need to collect feedback from the community. As the author of PEP 3131, I'd like to encourage comments to the PEP included below, either here (comp.lang.python), or...
9
by: =?Utf-8?B?RGFu?= | last post by:
I have the following code section that I thought would strip out all the non-ascii characters from a string after decoding it. Unfortunately the non-ascii characters are still in the string....
13
by: Eps | last post by:
Hi there, I believe all strings in .net are unicode by default, I am looking for a way to remove all non ascii characters from a string (or optionally replace them). There is an article on...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.