C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old May 14th, 2007, 08:16 PM
decompiledJava decompiledJava is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2005
Posts: 32 decompiledJava User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
  #2  
Old May 14th, 2007, 08:49 PM
sizablegrin's Avatar
sizablegrin sizablegrin is offline
Dev Shed God 1st Plane (5500 - 5999 posts)
 
Join Date: Jun 2005
Posts: 5,964 sizablegrin User rank is General 58th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 58th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 58th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 58th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 58th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 58th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 58th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 58th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 58th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 58th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 58th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 58th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 58th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 58th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 58th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 58th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 3 Weeks 2 Days 12 h 47 m 19 sec
Warnings Level: 10
Number of bans: 1
Reputation Power: 4851
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.

Reply With Quote
  #3  
Old May 14th, 2007, 09:55 PM
decompiledJava decompiledJava is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2005
Posts: 32 decompiledJava User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
  #4  
Old May 14th, 2007, 09:59 PM
Lux Perpetua Lux Perpetua is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Feb 2004
Location: San Francisco Bay
Posts: 1,938 Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 1 Week 2 h 43 m 16 sec
Reputation Power: 1312
As opposed to what? There's no "hex addition" or "decimal addition," only "addition."

Reply With Quote
  #5  
Old May 14th, 2007, 10:04 PM
Oler1s Oler1s is offline
Contributing User
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Jul 2006
Posts: 2,270 Oler1s User rank is General 11st Grade (Above 100000 Reputation Level)Oler1s User rank is General 11st Grade (Above 100000 Reputation Level)Oler1s User rank is General 11st Grade (Above 100000 Reputation Level)Oler1s User rank is General 11st Grade (Above 100000 Reputation Level)Oler1s User rank is General 11st Grade (Above 100000 Reputation Level)Oler1s User rank is General 11st Grade (Above 100000 Reputation Level)Oler1s User rank is General 11st Grade (Above 100000 Reputation Level)Oler1s User rank is General 11st Grade (Above 100000 Reputation Level)Oler1s User rank is General 11st Grade (Above 100000 Reputation Level)Oler1s User rank is General 11st Grade (Above 100000 Reputation Level)Oler1s User rank is General 11st Grade (Above 100000 Reputation Level)Oler1s User rank is General 11st Grade (Above 100000 Reputation Level)Oler1s User rank is General 11st Grade (Above 100000 Reputation Level)Oler1s User rank is General 11st Grade (Above 100000 Reputation Level)Oler1s User rank is General 11st Grade (Above 100000 Reputation Level)Oler1s User rank is General 11st Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 4 Days 15 h 34 m 57 sec
Reputation Power: 1735
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.

Reply With Quote
  #6  
Old May 14th, 2007, 10:14 PM
decompiledJava decompiledJava is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2005
Posts: 32 decompiledJava User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
  #7  
Old May 14th, 2007, 10:48 PM
sizablegrin's Avatar
sizablegrin sizablegrin is offline
Dev Shed God 1st Plane (5500 - 5999 posts)
 
Join Date: Jun 2005
Posts: 5,964 sizablegrin User rank is General 58th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 58th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 58th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 58th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 58th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 58th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 58th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 58th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 58th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 58th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 58th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 58th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 58th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 58th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 58th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 58th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 3 Weeks 2 Days 12 h 47 m 19 sec
Warnings Level: 10
Number of bans: 1
Reputation Power: 4851
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.

Reply With Quote
  #8  
Old May 15th, 2007, 03:56 AM
clifford's Avatar
clifford clifford is offline
Contributing User
Dev Shed Demi-God (4500 - 4999 posts)
 
Join Date: Aug 2003
Location: UK
Posts: 4,824 clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 2 Days 21 h 1 m
Reputation Power: 1800
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.

Reply With Quote
  #9  
Old May 15th, 2007, 06:56 AM
decompiledJava decompiledJava is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2005
Posts: 32 decompiledJava User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Altering Char via hex, octal or decimal within C.

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap