Thread: how to convert a binary table to hexadecimal value

  1. #1
    Registered User
    Join Date
    Sep 2013
    Posts
    16

    how to convert a binary table to hexadecimal value

    i need to code a function that converts an array of 64 bits into a hexadecimal value, the one is tested gives me correct value except for the last hexadecimal letter, can anyone tell me why is this happening or provide me a solution ? thank you in advance
    Code:
    #include <iostream>
    #include <stdio.h>
    #include <conio.h>
    #include <fstream>
    #include <cstdlib>
    #include <string>
    typedef unsigned __int64 Ebyte;
    #define length 64
    
    
    void to_tab(Ebyte cle,int tab[])
    {
        for(int i=0;i<length;i++)
            tab[i]=(cle >> i) & 0x1;
    }
    
    
    Ebyte to_hex(int T[64])
    {
        Ebyte temp=0;
        for(int i=0;i<64;i++)
            temp+=T[i]*pow(2,i);
        return temp;
    }
    
    
    void main ()
    {Ebyte cle=0xabcdef2748493956,cle2;int test[64];
    to_tab(cle,test);printf("la table est :\n");
    for(int i=0;i<64;i++)
    {if(i%8==0) printf("\n");printf("%d\t",test[i]);}
    cle2=to_hex(test);
    printf("\nhex est :    %I64x",cle2);
    getch();
    }
    Attached Images Attached Images how to convert a binary table to hexadecimal value-last-jpg 

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Your indentation is horrible
    You still using pow when bit-operations are required
    You still mixing signed and unsigned types

    And you mixing C++ and C - you need stick to one languge

    Code:
    #include <stdio.h>
    
    typedef unsigned __int64 Ebyte;
    #define length 64
    
    void to_tab(Ebyte cle,unsigned int tab[])
    {
      int i;
      for(i=0;i<length;i++)
        tab[i]=(cle >> i) & 0x1;
    }
    
    
    Ebyte to_hex(unsigned int T[64])
    {
      Ebyte temp=0;
      int i;
      for(i=0;i<64;i++)
      {
        Ebyte t = T[i];
        t <<= i;
        temp |= t;
      }
      return temp;
    }
    
    
    int main (void)
    {
      Ebyte cle=0xabcdef2748493956,cle2;
      unsigned int test[64];
      int i;
    
      to_tab(cle,test);
      
      printf("la table est :\n");
      for(i=0;i<64;i++)
      {
        if(i%8==0) 
          printf("\n");
        printf("%u\t",test[i]);
      }
      cle2=to_hex(test);
      printf("\nhex est :    %llx",cle2);
      getchar();
      return 0;
    }
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Try using some kind of << operator instead of pow(2,i), in the same way you used >> to extract bits.

    pow() returns a floating point (or double) result, and that's going to start being an approximation when it passes 54 bits (the number of bits in the mantissa of a double).
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Sep 2013
    Posts
    16
    thank you very much

  5. #5
    Registered User
    Join Date
    Sep 2013
    Posts
    16
    ohh it did not work :/
    Attached Images Attached Images how to convert a binary table to hexadecimal value-why-no-jpg 

  6. #6
    Registered User
    Join Date
    Sep 2013
    Posts
    16
    i didnt quite know to to set bits using the <<, i'll keep trying bro, ty

  7. #7
    Registered User
    Join Date
    Sep 2013
    Posts
    16
    problem not solved, same problem :/

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Try copy/pasting your code, not a fuzzy jpeg we can hardly read (and which isn't your whole code anyway).

    Text would take up a lot less space as well.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #9
    11DE784A SirPrattlepod's Avatar
    Join Date
    Aug 2013
    Posts
    485
    That's not converting the number to hex though, it's converting an array with binary digits in it to a... binary representation :/

    I think you've probably misunderstood what you're trying to do

  10. #10
    Registered User
    Join Date
    Sep 2013
    Posts
    16
    Code:
    #include <iostream>
    #include <stdio.h>
    #include <conio.h>
    #include <fstream>
    #include <cstdlib>
    #include <string>
    typedef unsigned __int64 Ebyte;
    #define length 64
    
    
    void to_tab(Ebyte cle,int tab[])
    {
      int i;
      for(i=0;i<length;i++)
        tab[i]=(cle >> i) & 0x1;
    }
    
    
    Ebyte to_hex(int T[64])
    {
      Ebyte temp=0;
      int i;
      for(i=0;i<64;i++)
      {
        Ebyte t = T[i];
        t <<= i;
        temp |= t;
      }
      return temp;	// not working
    }
    
    
    void main()
    {Ebyte cle=0xabcdef2748493956,cle2;	// testing to_hex
      int test[64];
      int i;
     
      to_tab(cle,test);
       
      printf("la table est :\n");
      for(i=0;i<64;i++)
      {
        if(i%8==0) 
          printf("\n");
        printf("%u\t",test[i]);
      }
      cle2=to_hex(test);
      printf("\nhex est :    %llx",cle2);
      getchar();
    }

  11. #11
    Registered User
    Join Date
    Sep 2013
    Posts
    16
    any help ???

  12. #12
    11DE784A SirPrattlepod's Avatar
    Join Date
    Aug 2013
    Posts
    485
    I have no idea what you're trying to do... your current seems pretty pointless to me (even if it worked).

    Maybe make a post describing *exactly* what you want to achieve.

  13. #13
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    Quote Originally Posted by thefirstone92 View Post
    any help ?
    It's working when I compile it. What do you need help with? To fix a warning I changed: tab[i]=(cle >> i) & 0x1; to tab[i]=(int)((cle >> i) & 0x1); , but the code works as shown in post #10.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to convert hexadecimal into floating point?
    By zenniz in forum C Programming
    Replies: 4
    Last Post: 03-11-2013, 09:59 AM
  2. trying to convert hexadecimal to decimal
    By chrissy2860 in forum C Programming
    Replies: 7
    Last Post: 01-23-2012, 04:11 PM
  3. convert string to hexadecimal
    By nocturna_gr in forum C Programming
    Replies: 3
    Last Post: 12-11-2007, 04:45 AM
  4. ASCII convert to Hexadecimal value?
    By ashish.malviya in forum C Programming
    Replies: 2
    Last Post: 04-05-2006, 12:45 AM
  5. petition to convert the universe to hexadecimal...
    By doubleanti in forum A Brief History of Cprogramming.com
    Replies: 17
    Last Post: 10-01-2001, 07:05 PM