Like Tree4Likes
  • 2 Post By Salem
  • 1 Post By grumpy
  • 1 Post By c99tutorial

Array to string

This is a discussion on Array to string within the C Programming forums, part of the General Programming Boards category; HI Guys, I cant seem to find a reasonable answer to my question through search so i thought i would ...

  1. #1
    Registered User
    Join Date
    Jan 2013
    Posts
    5

    Array to string

    HI Guys,

    I cant seem to find a reasonable answer to my question through search so i thought i would post a new topic.

    I have an array:
    Code:
     int array1[]={0,1,1,0}
    i need to convert the entire array to Hexadecimal.

    My first thoughts to the appraoch was to convert the array to a string then strcmp but i cant seem to find how to connvert an array to a string either.

    so my question is can you tell me how to convert the array to hexadecimal directly? or, How to convert the array to a string to be strcmp'ed?

    Thanks in advance

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    So what is your string supposed to look like, when you've converted it?

    "0x00, 0x01, 0x01, 0x00"
    "0x00010100"
    Or whatever endless variation you can imagine.

    Perhaps start with sprintf() and the %x format.
    iMalc and Barney McGrew like this.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Jan 2013
    Posts
    5
    The output would be the hexadecimal value for example if the array consists of 1,0,1,1 then the output should be B.

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,208
    A simple for loop, multiplying each digit by a corresponding power of two and adding up in base 16 (hex) would do the trick.
    Right 98% of the time, and don't care about the other 3%.

  5. #5
    Registered User
    Join Date
    Jan 2013
    Posts
    5
    Thanks Grumpy however im not sure i know what you mean...."multiplying each digit " by this do you mean each array element? if so then this is not what i require. i require the entire array, so 1011, to be converted to hex. Not just a single element.

    Does that make sense? Am i misunderstanding your recommendation?

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,208
    Yes, you have misunderstood. The words "simple for loop" in my previous post expressed one possible mechanism of looping over elements.

    1011 in base 2 is equivalent to 1*8 + 0*4 + 1*2 + 1*1 which, in base 16, has value B. The values 8,4,2,1 are powers of 2. Work out a way to express that in a loop, that accesses the values (1's and 0's) in your array.

    Essentially, all you're asking for is converting a binary (base 2) representation to hex (base 16).
    iMalc likes this.
    Right 98% of the time, and don't care about the other 3%.

  7. #7
    Registered User
    Join Date
    Nov 2012
    Posts
    1,053
    Quote Originally Posted by weez View Post
    My first thoughts to the appraoch was to convert the array to a string then strcmp but i cant seem to find how to connvert an array to a string either.
    Anything that can be printed by a sequence of printf's can also be combined into a string using sprintf. Here is one way to do it:

    Suppose you can print this sequence
    Code:
    printf("Hello");
    printf(", ");
    printf("World");
    printf("!!");
    Then to put it in a string instead, do this:
    Code:
    char result[1000];
    char *s = result;
    s += sprintf(s, "Hello");
    s += sprintf(s, ", ");
    s += sprintf(s, "World");
    s += sprintf(s, "!!");
    The destination result must have enough room, and each of the sprintf calls must return no error.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Adding a string array to another string array?
    By vertigodown in forum C Programming
    Replies: 1
    Last Post: 04-20-2012, 09:56 AM
  2. Replies: 3
    Last Post: 06-10-2011, 07:47 PM
  3. Converting tchar array to string array
    By GeekInTraining in forum C++ Programming
    Replies: 2
    Last Post: 01-17-2011, 12:34 AM
  4. Replies: 1
    Last Post: 12-10-2008, 10:29 AM
  5. convert char** (c string array) to std::string[]
    By umen242 in forum C++ Programming
    Replies: 2
    Last Post: 11-11-2008, 04:52 AM

Tags for this Thread


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