Connecting Tech Pros Worldwide Forums | Help | Site Map

bitwise operations.

Member
 
Join Date: Mar 2009
Location: San Diego , CA
Posts: 45
#1: Apr 22 '09
Hey everyone. Quick question. Say I have a hex string = "6c". I want to be able to turn that into a bit representation of 0000 0000. would i have to convert it to a integer first then do some sort of left shift on it?? Thanks.

JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#2: Apr 22 '09

re: bitwise operations.


Quote:

Originally Posted by yeshello54 View Post

Hey everyone. Quick question. Say I have a hex string = "6c". I want to be able to turn that into a bit representation of 0000 0000. would i have to convert it to a integer first then do some sort of left shift on it?? Thanks.

You first want to convert that string value to a number; the string must be interpreted as a hexadecimal number. The *scanf family of functions can do it; read here about them.

The next step is to create a string that represents a binary representation of the number. The *printf family of functions can't help you (they only do octal, decimal or hexadecimal representations of a number). A bit of bit shifting can do the job though.

kind regards,

Jos
Familiar Sight
 
Join Date: Jan 2007
Posts: 191
#3: Apr 22 '09

re: bitwise operations.


In the context if what JosAH has said
The following algotithm will perform the transformation also.
1. make the 4 LH bits equal to the LH hex numeral or letter.
2. make the next 4 bits equal the next hex numeral or letter
3. repeat 2 above until the hex integer is exhausted.

e.g. convert 0x 5 d 2 3 f a
bin equivalent is 0101 1101 0010 0011 1111 1010

hth's
Banfa's Avatar
AdministratorVoR
 
Join Date: Feb 2006
Location: South West UK
Posts: 6,195
#4: Apr 22 '09

re: bitwise operations.


I find it hard to believe that you are recommending using a *scanf function Jos, I personally have always believed that they are a curse on the C language.

Myself I would use strtol
JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#5: Apr 22 '09

re: bitwise operations.


Quote:

Originally Posted by Banfa View Post

I find it hard to believe that you are recommending using a *scanf function Jos, I personally have always believed that they are a curse on the C language.

Myself I would use strtol

The *scanf functions do their job if used correctly; I agree, when used as interactive lexical analyzers they feel like acute diarrhea with no toilet in the vicinity.

kind regards,

Jos
Reply

Tags
bitwise, integer, shift, string