Hex digits to special chars

Discussion in 'C Programming' started by abhi147@gmail.com, Aug 1, 2006.

  1. Guest

    Hi ,

    I have been trying to convert a 32 bit string like
    "b2a4fdf15af7c4f215e1909be4707bdb" to convert to a string of special
    chars like "²¤ýñZ÷Äòá?›äp{Û"
    I have tried a lot of things .. but nothing works . I have no idea
    how to go further .
    Can anyone help me ?

    Thanks in advance.
     
    , Aug 1, 2006
    #1
    1. Advertising

  2. Richard Bos Guest

    wrote:

    > I have been trying to convert a 32 bit string like
    > "b2a4fdf15af7c4f215e1909be4707bdb" to convert to a string of special
    > chars like "=B2=A4=FD=F1Z=F7=C4=F2=E1?=9B=E4p{=DB"
    > I have tried a lot of things .. but nothing works . I have no idea
    > how to go further .=20
    > Can anyone help me ?


    Yes; undoubtedly the teacher who gave you this homework can, if you ask
    him civilly and show your work upto now.

    Richard
     
    Richard Bos, Aug 1, 2006
    #2
    1. Advertising

  3. Guest

    Richard Bos wrote:
    > wrote:
    >
    > > I have been trying to convert a 32 bit string like
    > > "b2a4fdf15af7c4f215e1909be4707bdb" to convert to a string of special
    > > chars like "=B2=A4=FD=F1Z=F7=C4=F2=E1?=9B=E4p{=DB"
    > > I have tried a lot of things .. but nothing works . I have no idea
    > > how to go further .=20
    > > Can anyone help me ?

    >
    > Yes; undoubtedly the teacher who gave you this homework can, if you ask
    > him civilly and show your work upto now.
    >
    > Richard


    I am not sure if the special chars got printed correctly . The string
    is
    " ²¤ýñZ÷Äòá›äp{Û" .
    Is it really that simple? if so ,,, then can you please tell me .. coz
    my teacher also doesn't know !
     
    , Aug 1, 2006
    #3
  4. wrote:
    >
    > I have been trying to convert a 32 bit string like
    > "b2a4fdf15af7c4f215e1909be4707bdb"


    What do you mean by a "32 bit string"? This looks like a
    straightforward C string.

    > to convert to a string of special
    > chars like "²¤ýñZ÷Äòá?›äp{Û"
    > I have tried a lot of things .. but nothing works . I have no idea
    > how to go further .
    > Can anyone help me ?


    We have no idea how to do this conversion. You have to tell us what the
    algorithm is. What is the relationship between the initial string and
    the final string? If you tell us what needs to be done to get from the
    original string to the final one. we can help you implement it in C.
     
    J. J. Farrell, Aug 1, 2006
    #4
  5. Guest

    J. J. Farrell wrote:
    > wrote:
    > >
    > > I have been trying to convert a 32 bit string like
    > > "b2a4fdf15af7c4f215e1909be4707bdb"

    >
    > What do you mean by a "32 bit string"? This looks like a
    > straightforward C string.
    >
    > > to convert to a string of special
    > > chars like "²¤ýñZ÷Äòá?›äp{Û"
    > > I have tried a lot of things .. but nothing works . I have no idea
    > > how to go further .
    > > Can anyone help me ?

    >
    > We have no idea how to do this conversion. You have to tell us what the
    > algorithm is. What is the relationship between the initial string and
    > the final string? If you tell us what needs to be done to get from the
    > original string to the final one. we can help you implement it in C.


    Thanks :)
    This is a hexadecimal number .
    [b2][a4][fd][f1][5a][f7][c4][f2][15][e1][90][9b][e4][70][7b][db]
    and the special chars are the ISO-8859-1conversion of those hexadecimal
    number .
     
    , Aug 1, 2006
    #5
  6. I guess he means that it's a string of hex chars... Like
    b2=178
    a4=164
    etc.
    So he wants to print the ascii value of 178, the ascii
    value of 164 etc ...

    So, how anybody can do this ? Well, i'm not sure if there's
    an easier way, but i'm thinking that a loop has to be written,
    to get characters in pairs as the above from the string and
    then convert each pair to an integer and print it's ascii value...

    This one may be helpful for that conversion
    http://www.thecodeproject.com/string/hexstrtoint.asp

    --
    Papastefanos Serafeim

    ? "J. J. Farrell" <> ?????? ??? ??????
    news:...

    wrote:
    >
    > I have been trying to convert a 32 bit string like
    > "b2a4fdf15af7c4f215e1909be4707bdb"


    What do you mean by a "32 bit string"? This looks like a
    straightforward C string.

    > to convert to a string of special
    > chars like "²¤ýñZ÷Äòá?>äp{Û"
    > I have tried a lot of things .. but nothing works . I have no idea
    > how to go further .
    > Can anyone help me ?


    We have no idea how to do this conversion. You have to tell us what the
    algorithm is. What is the relationship between the initial string and
    the final string? If you tell us what needs to be done to get from the
    original string to the final one. we can help you implement it in C.
     
    Papastefanos Serafeim, Aug 1, 2006
    #6
  7. Simon Biber Guest

    wrote:
    > J. J. Farrell wrote:
    >> wrote:
    >>> I have been trying to convert a 32 bit string like
    >>> "b2a4fdf15af7c4f215e1909be4707bdb"

    >> What do you mean by a "32 bit string"? This looks like a
    >> straightforward C string.
    >>
    >>> to convert to a string of special
    >>> chars like "²¤ýñZ÷Äòá?›äp{Û"
    >>> I have tried a lot of things .. but nothing works . I have no idea
    >>> how to go further .
    >>> Can anyone help me ?

    >> We have no idea how to do this conversion. You have to tell us what the
    >> algorithm is. What is the relationship between the initial string and
    >> the final string? If you tell us what needs to be done to get from the
    >> original string to the final one. we can help you implement it in C.

    >
    > Thanks :)
    > This is a hexadecimal number .
    > [b2][a4][fd][f1][5a][f7][c4][f2][15][e1][90][9b][e4][70][7b][db]
    > and the special chars are the ISO-8859-1conversion of those hexadecimal
    > number .
    >


    Making the compiler output those bytes is not difficult; but ensuring
    that they are interpreted as ISO-8859-1 is not possible in strictly
    portable C code.

    My hexbytes function below will convert your string containing
    hexadecimal representations of bytes into a string containing the bytes
    themselves.

    The print_iso88591_glibc function achieves the task of interpreting the
    bytes as ISO-8859-1, converting them to wide characters, and then
    outputting them to the stdout stream. The particular locale name used is
    one that exists on my system but may not exist on yours.

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <locale.h>
    #include <wchar.h>

    /* hexbytes function to interpret each two characters in src string
    as a hexadecimal byte and store the resulting string into dst */
    void hexbytes(char *dst, const char *src)
    {
    char tmp[3] = {0, 0, 0};
    const char *p;
    char *q;
    if(strlen(src) % 2)
    {
    fprintf(stderr, "Odd length is invalid input\n");
    exit(EXIT_FAILURE);
    }
    for(p = src, q = dst; *p; p += 2, q++)
    {
    tmp[0] = p[0];
    tmp[1] = p[1];
    *q = strtol(tmp, NULL, 16);
    }
    *q = 0;
    }

    void print_iso88591_glibc(const char *str)
    {
    wchar_t *dst = malloc((strlen(str) + 1) * sizeof *dst);
    if(!dst)
    {
    fprintf(stderr, "Unable to allocate memory\n");
    exit(EXIT_FAILURE);
    }
    if(!setlocale(LC_CTYPE, "en_US.ISO-8859-1"))
    {
    fprintf(stderr, "Unable to set ISO-8859-1 locale\n");
    exit(EXIT_FAILURE);
    }
    mbstowcs(dst, str, strlen(str));
    if(!setlocale(LC_CTYPE, ""))
    {
    fprintf(stderr, "Unable to set default locale\n");
    exit(EXIT_FAILURE);
    }
    fputws(dst, stdout);
    free(dst);
    }

    int main(void)
    {
    const char *src = "b2a4fdf15af7c4f215e1909be4707bdb";
    char *dst = malloc(strlen(src) / 2 + 1);
    if(!dst)
    {
    fprintf(stderr, "Unable to allocate memory\n");
    exit(EXIT_FAILURE);
    }
    hexbytes(dst, src);
    print_iso88591_glibc(dst);
    free(dst);
    putwchar(L'\n');
    return 0;
    }

    --
    Simon.
     
    Simon Biber, Aug 1, 2006
    #7
  8. Guest

    Simon Biber wrote:
    > wrote:
    > > J. J. Farrell wrote:
    > >> wrote:
    > >>> I have been trying to convert a 32 bit string like
    > >>> "b2a4fdf15af7c4f215e1909be4707bdb"
    > >> What do you mean by a "32 bit string"? This looks like a
    > >> straightforward C string.
    > >>
    > >>> to convert to a string of special
    > >>> chars like "²¤ýñZ÷Äòá?›äp{Û"
    > >>> I have tried a lot of things .. but nothing works . I have no idea
    > >>> how to go further .
    > >>> Can anyone help me ?
    > >> We have no idea how to do this conversion. You have to tell us what the
    > >> algorithm is. What is the relationship between the initial string and
    > >> the final string? If you tell us what needs to be done to get from the
    > >> original string to the final one. we can help you implement it in C.

    > >
    > > Thanks :)
    > > This is a hexadecimal number .
    > > [b2][a4][fd][f1][5a][f7][c4][f2][15][e1][90][9b][e4][70][7b][db]
    > > and the special chars are the ISO-8859-1conversion of those hexadecimal
    > > number .
    > >

    >
    > Making the compiler output those bytes is not difficult; but ensuring
    > that they are interpreted as ISO-8859-1 is not possible in strictly
    > portable C code.
    >
    > My hexbytes function below will convert your string containing
    > hexadecimal representations of bytes into a string containing the bytes
    > themselves.
    >
    > The print_iso88591_glibc function achieves the task of interpreting the
    > bytes as ISO-8859-1, converting them to wide characters, and then
    > outputting them to the stdout stream. The particular locale name used is
    > one that exists on my system but may not exist on yours.
    >
    > #include <stdio.h>
    > #include <stdlib.h>
    > #include <string.h>
    > #include <locale.h>
    > #include <wchar.h>
    >
    > /* hexbytes function to interpret each two characters in src string
    > as a hexadecimal byte and store the resulting string into dst */
    > void hexbytes(char *dst, const char *src)
    > {
    > char tmp[3] = {0, 0, 0};
    > const char *p;
    > char *q;
    > if(strlen(src) % 2)
    > {
    > fprintf(stderr, "Odd length is invalid input\n");
    > exit(EXIT_FAILURE);
    > }
    > for(p = src, q = dst; *p; p += 2, q++)
    > {
    > tmp[0] = p[0];
    > tmp[1] = p[1];
    > *q = strtol(tmp, NULL, 16);
    > }
    > *q = 0;
    > }
    >
    > void print_iso88591_glibc(const char *str)
    > {
    > wchar_t *dst = malloc((strlen(str) + 1) * sizeof *dst);
    > if(!dst)
    > {
    > fprintf(stderr, "Unable to allocate memory\n");
    > exit(EXIT_FAILURE);
    > }
    > if(!setlocale(LC_CTYPE, "en_US.ISO-8859-1"))
    > {
    > fprintf(stderr, "Unable to set ISO-8859-1 locale\n");
    > exit(EXIT_FAILURE);
    > }
    > mbstowcs(dst, str, strlen(str));
    > if(!setlocale(LC_CTYPE, ""))
    > {
    > fprintf(stderr, "Unable to set default locale\n");
    > exit(EXIT_FAILURE);
    > }
    > fputws(dst, stdout);
    > free(dst);
    > }
    >
    > int main(void)
    > {
    > const char *src = "b2a4fdf15af7c4f215e1909be4707bdb";
    > char *dst = malloc(strlen(src) / 2 + 1);
    > if(!dst)
    > {
    > fprintf(stderr, "Unable to allocate memory\n");
    > exit(EXIT_FAILURE);
    > }
    > hexbytes(dst, src);
    > print_iso88591_glibc(dst);
    > free(dst);
    > putwchar(L'\n');
    > return 0;
    > }
    >
    > --
    > Simon.


    Hi Simon ,

    I ran your code on my solaris machine , where when i used to
    run
    this code

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <locale.h>
    #include <wchar.h>

    #define BUF_SIZE 100

    int main(int argc, char* argv[])
    {
    int i;

    unsigned char
    mbBuf[BUF_SIZE]="\xb2\xa4\xfd\xf1\x5a\xf7\xc4\xf2\x15\xe1\x90\x9b\xe4\x70\x7b\xdb";
    printf(mbBuf);

    }

    I used to get the following o/p => ²¤ýñZ÷Äòá›äp{Û

    After running the code which had the system call
    setlocale(LC_CTYPE,"iso_8859_1")
    The character code of the machines seems to have changed .And it's not
    ISO-8859-1
    And i am unable to revert it back because I don't know what the
    previous
    settings were .
    Becuase now the same program shows the o/p as =>
    ¦ñ²±Z˜-=§ßÉ¢Sp{¦

    As you might have noticed ther earlier o/p were ascii equivalent of
    the hex digits . But now i don't even know what this current encoding
    is .

    The locale of my system shows :
    LANG=
    LC_CTYPE="C"
    LC_NUMERIC="C"
    LC_TIME="C"
    LC_COLLATE="C"
    LC_MONETARY="C"
    LC_MESSAGES="C"
    LC_ALL=

    Can you please help me as to what those previous settings should be to
    get
    the ascii equivalents ??
     
    , Aug 3, 2006
    #8
  9. On Thu, 3 Aug 2006 05:44:05 UTC, wrote:

    > Simon Biber wrote:
    > > wrote:
    > > > J. J. Farrell wrote:
    > > >> wrote:
    > > >>> I have been trying to convert a 32 bit string like
    > > >>> "b2a4fdf15af7c4f215e1909be4707bdb"
    > > >> What do you mean by a "32 bit string"? This looks like a
    > > >> straightforward C string.
    > > >>
    > > >>> to convert to a string of special
    > > >>> chars like "²ýñZ÷Äòá?äp{Û"
    > > >>> I have tried a lot of things .. but nothing works . I have no idea
    > > >>> how to go further .
    > > >>> Can anyone help me ?
    > > >> We have no idea how to do this conversion. You have to tell us what the
    > > >> algorithm is. What is the relationship between the initial string and
    > > >> the final string? If you tell us what needs to be done to get from the
    > > >> original string to the final one. we can help you implement it in C.
    > > >
    > > > Thanks :)
    > > > This is a hexadecimal number .
    > > > [b2][a4][fd][f1][5a][f7][c4][f2][15][e1][90][9b][e4][70][7b][db]
    > > > and the special chars are the ISO-8859-1conversion of those hexadecimal
    > > > number .

    >
    > unsigned char
    > mbBuf[BUF_SIZE]="\xb2\xa4\xfd\xf1\x5a\xf7\xc4\xf2\x15\xe1\x90\x9b\xe4\x70\x7b\xdb";
    > printf(mbBuf);
    >
    > }
    >
    > I used to get the following o/p => ²ýñZ÷Äòáäp{Û


    You tries to print some binary data. mbBuf contains a set of binary
    data but no printable chars. Anything that printf prints seems ok on
    that content. You would have to convert each digit to a printable char
    and print that.



    --
    Tschau/Bye
    Herbert

    Visit http://www.ecomstation.de the home of german eComStation
    eComStation 1.2 Deutsch ist da!
     
    Herbert Rosenau, Aug 4, 2006
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. aefxx
    Replies:
    1
    Views:
    616
    Virgil Green
    Dec 17, 2004
  2. Replies:
    10
    Views:
    6,393
    Neredbojias
    Aug 19, 2005
  3. Bengt Richter
    Replies:
    6
    Views:
    526
    Juha Autero
    Aug 19, 2003
  4. M.Posseth

    receiving ??? chars instead of "special" chars

    M.Posseth, Nov 15, 2004, in forum: ASP .Net Web Services
    Replies:
    3
    Views:
    285
    Dan Rogers
    Nov 16, 2004
  5. Jack
    Replies:
    4
    Views:
    164
    Ben Morrow
    Jul 14, 2006
Loading...

Share This Page