Solved

Unsigned Long Int to Byte?

Posted on 2009-05-03
6
756 Views
Last Modified: 2013-11-05
Hi, I have an unsigned long int number (only 0 and 1) and I would like to take the first 8 digits and put them in a byte. Example:

unsigned long int code = 01100011001110;   //13 digits

char byte = ---8 first digits from code---

---remove the 8 first digits from code and place the rest of them at the beggining---

printf("%ld", code);  //this will printf "001110"  (the last 6 digits)

Thanks
0
Comment
Question by:AL3X2
  • 4
  • 2
6 Comments
 
LVL 40

Expert Comment

by:evilrix
ID: 24291041
You mean, like this?
#include <stdio.h>
 

int main(int argc, char **argv)

{

	unsigned long ul = 0x18CE; // 01100011001110

	unsigned char uc = static_cast<unsigned char>(ul >> 6);

	

	printf("0x%04X\n", uc); // 01100011 (0x0063)

	printf("0x%04X\n", (ul & 0x3F)); // 001110 (0x000E)

}

Open in new window

0
 
LVL 40

Accepted Solution

by:
evilrix earned 50 total points
ID: 24291046
Sorry, same thing with a C and not C++ style cast :)
#include <stdio.h>
 

int main(int argc, char **argv)

{

	unsigned long ul = 0x18CE; // 01100011001110

	unsigned char uc = (unsigned char)(ul >> 6);

	

	printf("0x%04X\n", uc); // 01100011 (0x0063)

	printf("0x%04X\n", (ul & 0x3F)); // 001110 (0x000E)

}

Open in new window

0
 

Author Comment

by:AL3X2
ID: 24291061
Thanks for the fast reply!

But, this will work only when ul is 13 digits?
0
Swamped with email signature updates?

Have you been given a load of changes to make to your users’ email signatures? Having to manually implement multiple signatures for every department? Let Exclaimer save you from being swamped with email signature updates!

 

Author Comment

by:AL3X2
ID: 24291074
Nevermind, I think I found it ;)


Just one more thing.

If i have ul = 1001 (for example), how can I add digits ?

something like  ul = ul + 100, so printf("%ld", ul) will show "1001100" ?
0
 
LVL 40

Expert Comment

by:evilrix
ID: 24291075
>> But, this will work only when ul is 13 digits?
It is specific to the requires you stated. Line 6 cuts off the fist 6 bytes of the UL using the right shift operator and casts the return into a char and the result is printed at line 8.

01100011001110 >> 6 == 01100011

Line 9 uses the mask 111111 to ignore all but the first 6 bits of the UL

01100011001110 & 111111 == 001110

Once you understand how these 2 operations work you can tailor them to various needs.


0
 
LVL 40

Expert Comment

by:evilrix
ID: 24291090
>> If i have ul = 1001 (for example), how can I add digits ?
>> something like  ul = ul + 100, so printf("%ld", ul) will show "1001100" ?

Read up on the << and >>  and the & and | bitwise C operators

http://www.cplusplus.com/doc/tutorial/operators/


#include <stdio.h>
 

int main(int argc, char **argv)

{

	unsigned long ul = 0x09; // 1001

	ul <<= 3; // 1001000

	ul |= 4; // 1001100
 

	printf("0x%04X\n", ul); // 1001100 (0x4C)

}

Open in new window

0

Featured Post

ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How do I recover data from my iphone 3g 4 54
C++ vs C compilers 13 157
C simple state machine for client server handshake 4 269
valid enum? 6 73
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

861 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now