473,399 Members | 2,278 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,399 software developers and data experts.

String to HEX & BIN Conversion?

I have a char array say
-->char in[13] = "0004 000A"
with a space between the 2 HEX numbers that I am trying to read into a
long so I can test individual bits of the number. using

sscanf(in,"%x", &l1);

the result is 4, this is great but I also want the other half of the
char array . How can I access the other part of the HEX using a similar
method? like

sscanf(in,"%x", &l1); //--> Gives me l1 = 4
sscanf(in,"%x", &l2); //--> Gives me l2 = 10

Can anyone help me out on this please?

Nov 15 '05 #1
4 2583
salsipius wrote:
I have a char array say
-->char in[13] = "0004 000A"
with a space between the 2 HEX numbers that I am trying to read into a
long so I can test individual bits of the number. using

sscanf(in,"%x", &l1);

the result is 4, this is great but I also want the other half of the
char array . How can I access the other part of the HEX using a similar
method? like

sscanf(in,"%x", &l1); //--> Gives me l1 = 4
sscanf(in,"%x", &l2); //--> Gives me l2 = 10

Can anyone help me out on this please?


int ret = sscanf(in, "%x %x", &l1, &l2);

Of course you should make sure sscanf returns 2 before
acting as if it worked. If you want to do it with multiple
statements or with better understanding/error handling of what
is being converted, I'd suggest using strtoul with base 16
and an endpointer.

-David

Nov 15 '05 #2
Thanks alot, I broke down and used a for loop to copy in[j+5] to copy
into a new array, but your solutiion works much better. Thanks

Nov 15 '05 #3
David Resnick wrote:
salsipius wrote:
I have a char array say
-->char in[13] = "0004 000A"
with a space between the 2 HEX numbers that I am trying to read into a
long so I can test individual bits of the number. using

sscanf(in,"%x", &l1);

the result is 4, this is great but I also want the other half of the
char array . How can I access the other part of the HEX using a similar
method? like

sscanf(in,"%x", &l1); //--> Gives me l1 = 4
sscanf(in,"%x", &l2); //--> Gives me l2 = 10

Can anyone help me out on this please?
int ret = sscanf(in, "%x %x", &l1, &l2);


Or just...

int ret = sscanf(in, "%x%x", &l1, &l2);
Of course you should make sure sscanf returns 2 before
acting as if it worked. If you want to do it with multiple
statements or with better understanding/error handling of what
is being converted, I'd suggest using strtoul with base 16
and an endpointer.


--
Peter

Nov 15 '05 #4
On 12 Oct 2005 11:06:19 -0700, "David Resnick" <ln********@gmail.com>
wrote:
salsipius wrote:
I have a char array say
-->char in[13] = "0004 000A"
with a space between the 2 HEX numbers that I am trying to read into a
long <snip> How can I access the other part of the HEX using a similar
method? like

sscanf(in,"%x", &l1); //--> Gives me l1 = 4
sscanf(in,"%x", &l2); //--> Gives me l2 = 10
If you really use a long you should use %lx. In fact for that you
should use _unsigned_ long, which is better for bitbashing anyway.
%x expects (& of) unsigned int, which may happen to be the same as
unsigned long on some systems but not all, which in turn is guaranteed
the same as signed (default) long only for positive values.
Can anyone help me out on this please?


int ret = sscanf(in, "%x %x", &l1, &l2);

Of course you should make sure sscanf returns 2 before
acting as if it worked. If you want to do it with multiple
statements or with better understanding/error handling of what
is being converted, I'd suggest using strtoul with base 16
and an endpointer.

Agree with both, but for completeness another possibility:

int ret, off,
ret = sscanf (in, "%lx%n", &l1, &off);
if( ret != 1 ) error;
ret = sscanf (in+off, "%lx", &l2)
if( ret != 1 ) error;

- David.Thompson1 at worldnet.att.net
Nov 15 '05 #5

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

Similar topics

27
by: Daniel Vallstrom | last post by:
I'm having problems with inconsistent floating point behavior resulting in e.g. assert( x > 0.0 && putchar('\n') && x == 0.0 ); holding. (Actually, my problem is the dual one where I get...
72
by: Paminu | last post by:
In math this expression: (a < b) && (b < c) would be described as: a < b < c But why is it that in C these two expressions evaluate to something different for the same values of a, b and...
1
by: Eugene Anthony | last post by:
Private Function BStr2UStr(BStr) 'Byte string to Unicode string conversion Dim lngLoop BStr2UStr = "" For lngLoop = 1 to LenB(BStr) BStr2UStr = BStr2UStr & Chr(AscB(MidB(BStr,lngLoop,1))) Next...
10
by: lovecreatesbeauty | last post by:
Is parameter type conversion required for the 2nd argument on printf("%p", (void *)&i); ? But one would never call memcpy like: memcpy((void *)pi, (void *)pj, sizeof *pj); /*memcpy((void *)pi,...
5
by: AMP | last post by:
Hello, I want to add some variables to a string and this isnt working. textBox1.Text="'BSL version='+ bslVerHi+ bslVerLo"; What am I doing wrong? Thanks Mike
16
by: Hugh Janus | last post by:
Hi all, I am using the below functions in order to convert strings to bytes and vice versa. I totally ans shamefully stole these functions from this group btw! Anyway, they work great but as...
1
by: gabriel.becedillas | last post by:
I have a lot of functions returning "const std::string&". Every time I wrap one of those I have to do it like this: class_.def("name", &someclass::bla,...
42
by: coder_lol | last post by:
Thanks everyone again for contributing to helping me clear C++ confusions. I did some serious reading on copy constructors and assignments and I think I've got a good handle on the memory stuff. ...
2
by: coder_lol | last post by:
MS VS 7.0 happily resolves by SmartPointer and Inheritance, but I got to use another target compiler and it does not accept user conversion for templates. Can I forced a recast somehow? I have...
8
by: Edson Manoel | last post by:
I have some C++ unmanaged code that takes std::string& arguments (as reference), and fills them (possibly growing the string). I want to call this code through PInvoke (DllImport), possibly using...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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,...
0
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...

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.