473,765 Members | 1,994 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

ASCII -> long (hex)

Hi there,

I've got a little problem. My aim is to send a hex file to a
microcontroller . So I'm reading the hex-File into a buffer and then have to
convert it to send the hex numbers. So now I want to know how I can get a
long out of something like
char* buf = "0xFF";

I tried
long number = atoi((const char*) buf);
but this only returns 0, instead of 255. So how to do this?

Thanks for any help

Thomas
Jul 22 '05 #1
4 4417
"Thomas Baier" <th****@tho-bai.de> wrote in message news:40d5f4c0$0 $25279
I've got a little problem. My aim is to send a hex file to a
microcontroller . So I'm reading the hex-File into a buffer and then have to convert it to send the hex numbers. So now I want to know how I can get a
long out of something like
char* buf = "0xFF";

I tried
long number = atoi((const char*) buf);
but this only returns 0, instead of 255. So how to do this?

Thanks for any help


Does this work?

std::istringstr eam in(buf);
long number;
in >> std::hex >> number;
Jul 22 '05 #2
On Sun, 20 Jun 2004 22:34:11 +0200 in comp.lang.c++, Thomas Baier
<th****@tho-bai.de> wrote,
char* buf = "0xFF";

I tried
long number = atoi((const char*) buf);


long number = strtol(buf, buf+strlen(buf) , 0);

Did you even look up atoi(). Did you have any reason to suspect that it
might do what you want? Of course it returns zero; the first character
if what you passed to it is zero and the character after that is not a
digit.

Jul 22 '05 #3
Siemel Naran wrote:
"Thomas Baier" <th****@tho-bai.de> wrote in message news:40d5f4c0$0 $25279

I've got a little problem. My aim is to send a hex file to a
microcontroll er. So I'm reading the hex-File into a buffer and then have


to
convert it to send the hex numbers. So now I want to know how I can get a
long out of something like
char* buf = "0xFF";

I tried
long number = atoi((const char*) buf);
but this only returns 0, instead of 255. So how to do this?

Thanks for any help

Does this work?

std::istringstr eam in(buf);
long number;
in >> std::hex >> number;


also look at std::strtol()
Jul 22 '05 #4
"red floyd" <no*****@here.d ude> wrote in message news:RZnBc.7576 3
also look at std::strtol()


That's a good function too, though I didn't know it before.

Evidently if you pass the third argument (the radix) as 0, then the function
will determine the base from the first 2 letters, and "0x" means
hexadecimal. The second argument will store a pointer to the character that
stopped the scan, and we can pass NULL if we don't care to know this
information. It returns a long. So

long number = strltol("0xFF", NULL, 0);

I looked for a function strtoi to convert a string to an integer, but failed
to find one.
Jul 22 '05 #5

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

Similar topics

11
4362
by: arekkusu | last post by:
Hello, I have the following problem: when declaring hex data in C, you typically do something like: const char = {0xde, 0xad, 0xbe, 0xef, 0x01, 0x02, 0x03, 0x04 ... }; This is quite verbose and makes the source hard to read. An alternative is something like const char = "deadbeef01020304";
8
10988
by: Ben Bloom | last post by:
Hi - I was speaking with someone who mentioned that it's possible to encode an ascii string as hex(?) in order to fit more data into the same # of chars. Can anyone enlighten me? The scenario is - I've got a CSV with a field that has a 16 character limit. I need to fit potentially 24 ASCII characters into it. Thanks.
5
51512
by: kuukelekuu | last post by:
I need to convert ascii chars to hex chars. I searched the internet, found hex to ascii, but nowhere is there a ascii to hex method created by anyone. Can anyone help me with that? Chears
11
2668
by: Steven Woody | last post by:
long i = nnn; long j; double d; d = i; j = ( long )d; in this case, i == j ? thanks.
0
9566
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10153
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9832
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8830
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7371
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5272
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5413
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3530
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2800
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.