474,042 Members | 57,613 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Integer and character manipulation

Hello,

I need some help with the following:

1) I need to split a 16bit INT into two 8bit characters, and then be
able to join these two characters to form again the original 16bit
integer.

For example with values expressed in binary:

int n= 000000001111111 ;
--> char a=00000000;
--> char b=11111111;

and then be able to join a and b to form back n.
2) And last, I need to convert an integer into a string.
For example,

int n=3.142 ---> char string[5] = ['3','.','1','4' ,'2']
Thank you very much!! I've tried working with casting but as far as I
know they don't seem to be the answer. I've also searched numerous
tutorials and web pages, but nothing productive. Any help or
suggestion will be highly appreciated. Thank you very much!!

Nov 15 '05 #1
10 4040

ru****@gmail.co m wrote:
Hello,

I need some help with the following:

1) I need to split a 16bit INT into two 8bit characters, and then be
able to join these two characters to form again the original 16bit
integer.

For example with values expressed in binary:

int n= 000000001111111 ;
--> char a=00000000;
--> char b=11111111;

and then be able to join a and b to form back n.

You can use a mask for that.

a = n & 0xff00;
b = n & 0x00ff;

And use 'unsigned char' instead of 'char'. 'char' can be either
equivalent to 'unsigned char' or 'signed char'. If you rely on signed
char and use some value that cannot be represented with signed char,
then you may invoke implementation defined behavior or raise an
implementation defined signal.

2) And last, I need to convert an integer into a string.
For example,

int n=3.142 ---> char string[5] = ['3','.','1','4' ,'2']

'int' is a part of the standard integer types and can't be used to
represent rationals. So you should either use an object type of float
or double. To produce a string from a given value of type float or
double, use sprintf/snprintf

Thank you very much!! I've tried working with casting but as far as I
know they don't seem to be the answer. I've also searched numerous
tutorials and web pages, but nothing productive. Any help or
suggestion will be highly appreciated. Thank you very much!!


casting is not a magical thing. The reason casting exists in the
language is for a few corner cases, namely: %p expecting a (void *)
with *printf family of functions. To compare the return value of mktime
to (time_t)-1 in case of error. For the is*, to* functions/macros.

There may be one or two other places that escape me at the moment but
not much else beyond that. The reason it doesn't produce a string is
because a cast yields a value of a given type, given a value. As there
does not exist a string type in C, then you can't expect that there
will be a string produced by a cast.
--
aegis

Nov 15 '05 #2
On 5 Sep 2005 12:03:12 -0700, ru****@gmail.co m wrote:
Hello,

I need some help with the following:

1) I need to split a 16bit INT into two 8bit characters, and then be
able to join these two characters to form again the original 16bit
integer.

For example with values expressed in binary:

int n= 000000001111111 ;
--> char a=00000000;
--> char b=11111111;

and then be able to join a and b to form back n.
Look at the bitwise operators, especially "and" and shift.


2) And last, I need to convert an integer into a string.
For example,

int n=3.142 ---> char string[5] = ['3','.','1','4' ,'2']
3.142 is not an integer value of any kind. Your array is not a
string. Perhaps you'd like to tell us what you really need rather
than provide an incorrect example.


Thank you very much!! I've tried working with casting but as far as I
know they don't seem to be the answer. I've also searched numerous
tutorials and web pages, but nothing productive. Any help or
suggestion will be highly appreciated. Thank you very much!!


You will get a lot more help if you post your code. We won't do you
homework for you.
<<Remove the del for email>>
Nov 15 '05 #3
On 5 Sep 2005 13:49:26 -0700, "aegis" <ae***@mad.scie ntist.com> wrote:

ru****@gmail.c om wrote:
Hello,

I need some help with the following:

1) I need to split a 16bit INT into two 8bit characters, and then be
able to join these two characters to form again the original 16bit
integer.

For example with values expressed in binary:

int n= 000000001111111 ;
--> char a=00000000;
--> char b=11111111;

and then be able to join a and b to form back n.

You can use a mask for that.

a = n & 0xff00;


You are missing a shift. On most systems, the result of your
expression will not fit in a char.
b = n & 0x00ff;

And use 'unsigned char' instead of 'char'. 'char' can be either
equivalent to 'unsigned char' or 'signed char'. If you rely on signed
char and use some value that cannot be represented with signed char,
then you may invoke implementation defined behavior or raise an
implementati on defined signal.

2) And last, I need to convert an integer into a string.
For example,

int n=3.142 ---> char string[5] = ['3','.','1','4' ,'2']


'int' is a part of the standard integer types and can't be used to
represent rationals. So you should either use an object type of float
or double. To produce a string from a given value of type float or
double, use sprintf/snprintf

Thank you very much!! I've tried working with casting but as far as I
know they don't seem to be the answer. I've also searched numerous
tutorials and web pages, but nothing productive. Any help or
suggestion will be highly appreciated. Thank you very much!!


casting is not a magical thing. The reason casting exists in the
language is for a few corner cases, namely: %p expecting a (void *)
with *printf family of functions. To compare the return value of mktime
to (time_t)-1 in case of error. For the is*, to* functions/macros.

There may be one or two other places that escape me at the moment but
not much else beyond that. The reason it doesn't produce a string is
because a cast yields a value of a given type, given a value. As there
does not exist a string type in C, then you can't expect that there
will be a string produced by a cast.

<<Remove the del for email>>
Nov 15 '05 #4
Ian
ru****@gmail.co m wrote:
Hello,

I need some help with the following:

1) I need to split a 16bit INT into two 8bit characters, and then be
able to join these two characters to form again the original 16bit
integer.

For example with values expressed in binary:

int n= 000000001111111 ;
--> char a=00000000;
--> char b=11111111;

and then be able to join a and b to form back n.

Sounds like homework! Mug up on shifting and masking.
2) And last, I need to convert an integer into a string.
For example,

int n=3.142 ---> char string[5] = ['3','.','1','4' ,'2']

3.142.isn't an int, its a floating point value. Look up formatted output.

Ian
Nov 15 '05 #5

Barry Schwarz wrote:
On 5 Sep 2005 13:49:26 -0700, "aegis" <ae***@mad.scie ntist.com> wrote:

ru****@gmail.c om wrote:
Hello,

I need some help with the following:

1) I need to split a 16bit INT into two 8bit characters, and then be
able to join these two characters to form again the original 16bit
integer.

For example with values expressed in binary:

int n= 000000001111111 ;
--> char a=00000000;
--> char b=11111111;

and then be able to join a and b to form back n.

You can use a mask for that.

a = n & 0xff00;


You are missing a shift. On most systems, the result of your
expression will not fit in a char.


Oops. Indeed. That should have been a = (n & 0xff00) >> 8;

b = n & 0x00ff;

And use 'unsigned char' instead of 'char'. 'char' can be either
equivalent to 'unsigned char' or 'signed char'. If you rely on signed
char and use some value that cannot be represented with signed char,
then you may invoke implementation defined behavior or raise an
implementati on defined signal.

2) And last, I need to convert an integer into a string.
For example,

int n=3.142 ---> char string[5] = ['3','.','1','4' ,'2']


'int' is a part of the standard integer types and can't be used to
represent rationals. So you should either use an object type of float
or double. To produce a string from a given value of type float or
double, use sprintf/snprintf

Thank you very much!! I've tried working with casting but as far as I
know they don't seem to be the answer. I've also searched numerous
tutorials and web pages, but nothing productive. Any help or
suggestion will be highly appreciated. Thank you very much!!


casting is not a magical thing. The reason casting exists in the
language is for a few corner cases, namely: %p expecting a (void *)
with *printf family of functions. To compare the return value of mktime
to (time_t)-1 in case of error. For the is*, to* functions/macros.

There may be one or two other places that escape me at the moment but
not much else beyond that. The reason it doesn't produce a string is
because a cast yields a value of a given type, given a value. As there
does not exist a string type in C, then you can't expect that there
will be a string produced by a cast.

<<Remove the del for email>>


Nov 15 '05 #6
Hi to all,

Firstly thank you very much all of you for such a quick answer.

Using the bitwise operators was the important hint, and yes, after
using the masks and shifting 8 positions I've finally got the high byte
and low byte separated as two 8 bit characters as I needed.

As for the second part, sorry, of course 3.142 isn't an int, just quick
writing and bad example. I'havent tried that part yet, but after more
searching I think that fcvtf will probably do the job.

Posting the whole code wont probably help, as these are just two little
parts I need for doing the big task. It is not actually homework, but
something I need to do after some long time without coding (and wasn't
too good either when I used to code more often).

Thank you once again :) I'll let you know If I encounter problems with
the second part, though I hope I don't ;)

Ian wrote:
ru****@gmail.co m wrote:
Hello,

I need some help with the following:

1) I need to split a 16bit INT into two 8bit characters, and then be
able to join these two characters to form again the original 16bit
integer.

For example with values expressed in binary:

int n= 000000001111111 ;
--> char a=00000000;
--> char b=11111111;

and then be able to join a and b to form back n.

Sounds like homework! Mug up on shifting and masking.
2) And last, I need to convert an integer into a string.
For example,

int n=3.142 ---> char string[5] = ['3','.','1','4' ,'2']

3.142.isn't an int, its a floating point value. Look up formatted output.

Ian


Nov 15 '05 #7
On 5 Sep 2005 14:38:26 -0700, ru****@gmail.co m wrote:
Hi to all,

Firstly thank you very much all of you for such a quick answer.

Using the bitwise operators was the important hint, and yes, after
using the masks and shifting 8 positions I've finally got the high byte
and low byte separated as two 8 bit characters as I needed.

As for the second part, sorry, of course 3.142 isn't an int, just quick
writing and bad example. I'havent tried that part yet, but after more
searching I think that fcvtf will probably do the job.


Is fcvtf a standard function?
<<Remove the del for email>>
Nov 15 '05 #8
On Mon, 05 Sep 2005 14:30:25 -0700, aegis wrote:

Barry Schwarz wrote:
On 5 Sep 2005 13:49:26 -0700, "aegis" <ae***@mad.scie ntist.com> wrote:
....
>You can use a mask for that.
>
>a = n & 0xff00;


You are missing a shift. On most systems, the result of your
expression will not fit in a char.


Oops. Indeed. That should have been a = (n & 0xff00) >> 8;


That can have problems on systems with 16 bit ints because you could be
shifting a negative value.

Lawrence
Nov 15 '05 #9
Lawrence Kirby <lk****@netacti ve.co.uk> writes:
On Mon, 05 Sep 2005 14:30:25 -0700, aegis wrote:

Barry Schwarz wrote:
On 5 Sep 2005 13:49:26 -0700, "aegis" <ae***@mad.scie ntist.com> wrote:
...
You can use a mask for that.
>
>a = n & 0xff00;

You are missing a shift. On most systems, the result of your
expression will not fit in a char.


Oops. Indeed. That should have been a = (n & 0xff00) >> 8;


That can have problems on systems with 16 bit ints because you could be
shifting a negative value.


If ints were 16 bits, I'd be inclined to think 0xff00 would
be an unsigned int, which means n would be converted to unsigned
int before the '&' were done; the shift would then be working
on an unsigned int.
Nov 15 '05 #10

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

Similar topics

10
199291
by: dave | last post by:
I am reading input from a form. I want to validate the input by making sure that the string is actually an integer. How would I do this? Do i need to convert it to a character array and break down each character and test it? or is there an easier way? Thanks. --- Outgoing mail is certified Virus Free. Checked by AVG anti-virus system (http://www.grisoft.com). Version: 6.0.720 / Virus Database: 476 - Release Date: 7/14/04
5
22016
by: Mark | last post by:
Hello, I'm doing some data manipulation using an XML parser and am getting the following error message: Character reference "" is an invalid XML character. Any thoughts upon what this character could be? I've looked at all instances of "22" and "#" in the data I've got and can't find any obviously wrong with any of them.
9
2961
by: Adelson Anton | last post by:
I need to iterate characters in a loop. For example, all the letter from a to r. In C I would just do that: char c; for (c='a'; c<='r'; c++) It doesn't work in JS. Thanks.
15
2313
by: Beeeeeves | last post by:
Is there a quick way to find the index of the first character different in two strings? For instance, if I had the strings "abcdefghijkl" and "abcdefxyz" I would want the return value to be 6. Either in C# or C++. (I obviously know how to do it by looping through the
17
3503
by: Yogi_Bear_79 | last post by:
I have the following code: sscanf(line, "%d", n_ptr) !=1 || n_ptr <=0; It only partially works. If the user types a character other than 0-9 to start the string it fails. However as long as the first character is an integer it will allow letters in the following places for Example:
13
3250
by: Freaker85 | last post by:
Hello, I am new at programming in C and I am searching a manner to parse a string into an integer. I know how to do it in Java, but that doesn't work in C ;o) I searched the internet but I didn't found it yet. help please thank you Freaker85
232
13582
by: robert maas, see http://tinyurl.com/uh3t | last post by:
I'm working on examples of programming in several languages, all (except PHP) running under CGI so that I can show both the source files and the actually running of the examples online. The first set of examples, after decoding the HTML FORM contents, merely verifies the text within a field to make sure it is a valid representation of an integer, without any junk thrown in, i.e. it must satisfy the regular expression: ^ *?+ *$ If the...
3
3565
by: rob.ahlberg | last post by:
I got an integer what I trying to use with allegro function textout_ex() but it wants an char/char* as arg... And I really don't know how to cast it to one...
3
12613
by: amija0311 | last post by:
Hi, I am new using DB2 9.1 database by windows base. I want to query the data that contain string then translate the string into integer using DB2. The problems is If the data is null, i got the problem to translate. How to translate string also allow null data to integer. If null data it will read as space. My Data :- GEOSEG_ID SEQNO
0
10336
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11599
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
11137
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
10304
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...
0
6648
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
6827
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
5405
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4937
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3964
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.