The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
Altering Char via hex, octal or decimal within C.
Discuss Altering Char via hex, octal or decimal within C. in the C Programming forum on Dev Shed. Altering Char via hex, octal or decimal within C. C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

May 14th, 2007, 08:16 PM
|
|
Contributing User
|
|
Join Date: Feb 2005
Posts: 32
Time spent in forums: 8 h 31 m 35 sec
Reputation Power: 9
|
|
|
Altering Char via hex, octal or decimal within C.
Is it possible to alter a char as a decimal, hex or octal value with the C language using + - etc?
For example:
Char c = 'a';
c = \061
c = \061 + 1
I dunno if that explains it because well I don't know
Thanks in advance.
|

May 14th, 2007, 08:49 PM
|
|
|
|
I don't know if that explains it either. All data stored in the processor is binary. Floats are stored differently, in order to get range at the expense of precision, but it's still a binary medium, the bit positions just have different meanings.
The appearance of binary or octal or decimal or hex or whatever else, in the output, is just a convention of presentation. A binary value of 0x01000001 stored as a char or an int (any non-float, essentially) remains the same, whether it's presented as 01000001 binary or 101 octal or 65 decimal or 41 hex or A Ascii. Those values are just printed symbols. Your brain has to adjust to the represented magnitude.
Last edited by sizablegrin : May 14th, 2007 at 08:51 PM.
|

May 14th, 2007, 09:55 PM
|
|
Contributing User
|
|
Join Date: Feb 2005
Posts: 32
Time spent in forums: 8 h 31 m 35 sec
Reputation Power: 9
|
|
|
So I guess what I'm saying is I would like to shift the char using hex addition.
|

May 14th, 2007, 09:59 PM
|
|
Contributing User
|
|
Join Date: Feb 2004
Location: San Francisco Bay
|
|
|
As opposed to what? There's no "hex addition" or "decimal addition," only "addition."
|

May 14th, 2007, 10:04 PM
|
|
|
|
No, that's not what is being said (if you're interpreting sizablegrin's answer the way I think you are).
First, start with all data is binary. You see 'a' on the screen. But, wait, the computer works in binary. That means, somewhere along the way, that binary number was converted to 'a'. Probably using the ASCII system.
So a char is a binary number. A binary number is also a hex or octal number, simply converted appropriately. Whether you alter a char as a char, or as a decimal int, or in hex, or octal is irrelevant really.
If you're looking for an answer, I guess we could say yes, although sizeablegrin wisely realised that the background context was needed.
__________________
When you ask a question, be prepared to tell us: what have you tried? If you think you don't need to try anything, we will never be interested in helping you. If you agree with the link, and you refuse to answer that question, you are being a hypocrite.
Need help with broken code? Your question should be like a good bug report: (1) It has the smallest number of steps to reproduce the problem you see (2) It tells us precisely what you expected to see and (3) It tells us what you saw and how it differed from what you expected. We need all three to help you.
Want better answers? Tell us what you Googled for and what steps you took to answer your own question.
|

May 14th, 2007, 10:14 PM
|
|
Contributing User
|
|
Join Date: Feb 2005
Posts: 32
Time spent in forums: 8 h 31 m 35 sec
Reputation Power: 9
|
|
|
I understand the base number systems and how the information is stored in the registers on the cpu.
I guess the piece I'm missing is in C how I can manipulate that information to change the char.
Char a = 'a';
There fore a = 96 dec, 60 hex, 140 oct.
So if I wanted to manipulate variable a in Assembly I'd load it to the accumulator and add 1. Then I'd output it as a character.
So in C do I cast the char a as an Int then add one then cast back to char?
So if I have a char [] that's storing a,b,c. And I wanted to shift them to be b,c,d. I could just add one to the existing value.
|

May 14th, 2007, 10:48 PM
|
|
|
|
That's correct. However, a char is just a shorter form of an int (usually), so there's no need to cast unless you want to handle possible overflow without rolling around.
In fact, many char handling functions (such as getchar) return an int. This is so that a non-char value can be used as an error indication, for one.
You should also be aware that 'a' is not necessarily the value you have indicated. In the past, it almost universally was ASCII, but locales and the prevalence of alphabets that aren't English-derived have changed a few things.
Just think of a char as a 1-byte int. If you anticipate overflow, you don't actually have to cast, just assign to a larger type of integer. You might want to check your C/C++ docs regarding conversions, automatic and otherwise.
|

May 15th, 2007, 03:56 AM
|
 |
Contributing User
|
|
Join Date: Aug 2003
Location: UK
|
|
You could just try it!
The following are valid:
Code:
char foo = 'a' ;
foo += 0x01 ; // foo == 'b' (add hex integer)
foo += '\01' ; // foo == 'c' (add octal character constant)
foo += 01 ; // foo == 'd' (add octal integer)
foo += '\x01' ; // foo == 'e' (add hex character constant)
foo += 1 ; // foo == 'f' (add decimal integer)
foo++ ; // foo == 'g' (post-increment)
The octal, hex, and decimal notations are not different kinds on number; they are different source code representations of the same kind of number (an int).
The only subtle peculiarity you might ever need to be aware of is that in C the character constant 'a' has type int, and in C++ it has type char - strange but true, but hardly ever likely to cause you to worry.
Clifford
Last edited by clifford : May 15th, 2007 at 04:02 AM.
|

May 15th, 2007, 06:56 AM
|
|
Contributing User
|
|
Join Date: Feb 2005
Posts: 32
Time spent in forums: 8 h 31 m 35 sec
Reputation Power: 9
|
|
Thank you all for the input. This has cleared it up for me. 
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|