Thread: Split/select long hex to pairs

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    27

    Split/select long hex to pairs

    Hi all - its late and I really cannot figure this out!

    if unsigned char *s = 42006f, how can I split/select the hex value into its pairs i.e 42/00/6f so that I can manipulate the pair data rather than the large total
    using portable ansi c.
    thanks!!

  2. #2
    Registered User
    Join Date
    Apr 2008
    Posts
    396
    I'm not sure to understand what you ask.
    Generally you're expected to post some code showing you've done some work, but you're right, it's late.
    Code:
    #include <stdio.h>
    int main(void) {
    	size_t v = 0x42006f;
    	size_t v1 = v & 0xFF;
    	size_t v2 = (v >> 8) & 0xFF;
    	fprintf(stderr, "v=%#zx v1=%#zx v2=%#zx\n", v, v1, v2);
    	return 0;
    }
    If this is what you want, you can guess v3, the 3rd byte (0x42).

  3. #3
    Registered User
    Join Date
    Apr 2012
    Posts
    27
    Looks right in your example - ill give that a shot tomorrow, goodnight! and thanks

  4. #4
    Registered User
    Join Date
    Apr 2012
    Posts
    27
    hmmm doesn't seem to work I'm afraid. I'll provide some pseudo - probably best!

    v = s; /*S being the variable scanned in from user, for example 42006f*/

    /*The next bit I don't know how to do, but I know what I want out of it*/

    v1 = 42;
    v2 = 00;
    v3 = 6f

    Thanks!

  5. #5
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    hmmm doesn't seem to work I'm afraid.
    root4 gave you the code to split the int into bytes. It's your job to find out how to put them into the order that you think is right.
    Just to say it doesn't work is not enough.
    Show what you tried and explain why you think it's not right.
    Kurt

  6. #6
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751
    Quote Originally Posted by mrapoc View Post
    hmmm doesn't seem to work I'm afraid. I'll provide some pseudo - probably best!
    Actually no, what's probably best is for you to post code and show us you've actually took steps towards solving your problem.

    A quick check on your posts shows the majority of them related to this same problem. If you're a beginner you should be learing the language properly and from the beginning so you can understand how to actually solve your problem, and at least learn how to ask and present your problem so the board members can help you. And if this is homework related...well we have a a policy for that In fact, I'd suggest you read the announcements before continuing.
    Warning: Opinions subject to change without notice

    The C Library Reference Guide
    Understand the fundamentals
    Then have some more fun

  7. #7
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    A "union" is made for this purpose.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. split strings (and split thread)
    By mahi in forum C Programming
    Replies: 1
    Last Post: 10-31-2011, 06:56 AM
  2. Replies: 1
    Last Post: 10-11-2010, 01:53 AM
  3. stl for pairs
    By dpp in forum C++ Programming
    Replies: 14
    Last Post: 05-18-2009, 09:46 AM
  4. how to split long programe in small files
    By umeshjaviya in forum C Programming
    Replies: 11
    Last Post: 04-15-2008, 02:45 AM
  5. Pairs & Constructors
    By Nereus in forum C++ Programming
    Replies: 3
    Last Post: 04-04-2006, 10:55 AM