hex conversion madness

This is a discussion on hex conversion madness within the C Programming forums, part of the General Programming Boards category; After doing a little thinking about the "accessing memory" discussion, etc. I thought I had the solution (but you might ...

  1. #1
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300

    Cool hex conversion madness [SOLVED]

    After doing a little thinking about the "accessing memory" discussion, etc. I thought I had the solution (but you might be able to understand this without reading that).

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int translate_lilend (int LE, int size) {
            short int len=size*2, i, letr[size];
            char hex[len+1], chr[3];
            printf("%d\t%d\n",LE,size);
            sprintf(hex, "%x\n",LE);
            for (i=0;i<len;i+=2){
                    chr[0]=hex[len-(2+i)];
                    chr[1]=hex[len-(1+i)];
                    chr[3]=0;
                    puts(chr);
                    letr[i/2]=0;
                    letr[i/2]=strtol(chr,NULL,16);
                    printf("%d\t%d\n", i/2,letr[i/2]);
            }   
            for (i=0;i<size;i++) printf("letr:%d\n",letr[i]);
            for (i=0;i<size;i++) hex[i]=letr[i];
            hex[i+1]=0;
            puts(hex);
    }
                       
             
    int main(void) {
            translate_lilend(1952540788,4);  // a word is 4-bytes
    }
    Now, I would have thought the output from this would be consistent, even if I had some bit wrong. Instead,
    Code:
    1952540788      4
    74
    0       116
    68
    1       104
    61
    2       97
    74
    3       116             up to here, everything good, stays the same-- then
    letr:-4108           suddenly, the array has changed!  And it's different every time the 
    letr:18342          program is run!
    letr:-17248
    letr:18321
    ����6
    Nb. I've done very little work with numerical arrays thus far in my learning. What's wrong?
    Last edited by MK27; 09-17-2008 at 11:08 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Interesting. After I fix chr[3] = 0 to chr[2] = 0, I get:
    Code:
    1952540788      4
    74
    0       116
    68
    1       104
    61
    2       97
    74
    3       116
    letr:116
    letr:104
    letr:97
    letr:116
    that6
    which seems more consistent. (The last 6 is because you do a ++i in the for loop and then set the null character at i+1.) I don't see any reason to get what you get, but I'll keep thinking.

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by tabstop View Post
    fix chr[3] = 0 to chr[2] = 0
    Sometimes the answer is just too simple to find


    And I imagine since chr wasn't assigned 4 elements, chr[3] was overwriting some part of something else, variably and repeatedly...
    Last edited by MK27; 09-17-2008 at 11:17 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 18
    Last Post: 03-26-2008, 09:01 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. Replies: 11
    Last Post: 03-24-2006, 10:26 AM
  4. Hex to binary conversion and storing in nybble
    By shoobsie in forum C Programming
    Replies: 14
    Last Post: 10-25-2005, 02:51 AM
  5. Header File Question(s)
    By AQWst in forum C++ Programming
    Replies: 10
    Last Post: 12-23-2004, 10:31 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21