Thread: Unsigned Short to Char[X]

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    11

    Unsigned Short to Char[X]

    I need to take a value out of an unsigned short and add it to a char array.

    ex.:

    unsigned short a = 6849;
    char b[20];

    any idea about how to place "6849" into b ?

    Thank you !

  2. #2
    Registered User
    Join Date
    Mar 2011
    Posts
    546
    your task doesn't really make sense. do you have a more complete explanation of what you are required to do?

    xxxxxxxxxan unsigned short is 2 bytes. you can do shifts and masks to get the LSB and MSB and put them in b[0] and b[1] if thats what you want.

    never mind. you probably want to turn 6849 into a printable string?

    one way is
    Code:
    count = snprintf(b,sizeof(b),"%u",a);
    Last edited by dmh2000; 03-28-2011 at 11:23 AM.

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    11
    I'm totally lost

    I'm sending a char[] through a socket and I want to send some numeric information.

    I guess you mean using those operators >> << ...

    I will go read about this !

    Thank you

  4. #4
    Registered User
    Join Date
    Mar 2011
    Posts
    546
    i was thinking too much. if you want to use iostreams, then using << is the way to go. but do you want to send it as binary data or as ascii string?
    Last edited by dmh2000; 03-28-2011 at 11:26 AM.

  5. #5
    Registered User
    Join Date
    Mar 2011
    Posts
    11
    Hummmm

    I don't know much about hidden functionnality behing sockets but I already send some text ("ABCDE") through a char and i can get it back on the other side ("ABCDE") .

    i guess it's called ascii string ?

    is count supposed to be a char[] ?

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I suggest you use stringstreams to convert data to a string before sending it:
    Code:
    std::stringstream stream;
    stream << my_integer;
    std::string mydata = stream.str();
    Now you can send the raw string by using the c_str() member function to get the string and size() to get the length.

    To extract the data on the other end, you can do:
    Code:
    std::stringstream stream(received_data);
    stream >> my_data;
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 12
    Last Post: 08-11-2008, 11:02 PM
  2. Replies: 16
    Last Post: 10-29-2006, 05:04 AM
  3. Obtaining source & destination IP,details of ICMP Header & each of field of it ???
    By cromologic in forum Networking/Device Communication
    Replies: 1
    Last Post: 04-29-2006, 02:49 PM
  4. Color Variety
    By Unregistered in forum C++ Programming
    Replies: 7
    Last Post: 10-23-2002, 09:17 AM