1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2008
    Rep Power

    Question Declaring a hex value or a byte

    I am transmitting raw bytes of data through a serial port and I need to specify them in hex characters. Basically they are command sequences with a start delimiter of 7E. Therefore, an example would be:
    7E 00 0A 01 01 50 01 00 48 65 6C 6C 6F B8

    How could I declare these and then send the whole thing through a Linux write() command? I have absolutely no idea of how to do this.

    Also, I would like to convert a string into hex in C, how can I do that? For example, the 48 65 6C 6C 6F above is the hex representation of "Hello". How can I encode that e.g. from a keyboard and decode it e.g. to standard output?

    All help appreciated, thanks.
  2. #2
  3. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Rep Power
    A byte would be declared as unsigned char.

    As for the rest, I'm not sure you know what you're asking for.

    The raw data streaming into the serial port would be in binary. So you just simply read each byte and store it in an unsigned char (normally, you would create a sizable-enough array of unsigned char to serve as a buffer). Similarly, to send binary data through the serial port you would simply write unsigned char data out.

    Internally, it's all binary. Computers know nothing about decimal nor hex nor characters. All computers know is binary. The different possible representations are purely for human use and consumption and are all ASCII strings (OK, ANSI strings also).

    So when you ask, "Also, I would like to convert a string into hex in C, how can I do that?", I have to counter with "What the hell are you talking about?" When you type "Hello" into the keyboard, the computer converts the keyboard's scan codes into ASCII data, ie 48 65 6C 6C 6F. That is actually 01001000 01100101 01101100 01101100 01101111, because all the computer knows and all internal values are in binary. We only will output data as hex strings, or as decimal strings, or as character strings purely for our own convenience. Bits is bits; there ain't no hex about it.

    So, if you're talking about typing something into the keyboard and having it transmitted across a serial port, then just do it. No conversion is necessary.

    However, your use of the term "standard output" is very confusing. Standard output is the stdout file, which is the console, or more specifically the computer monitor (stdin is also the console, but it's the keyboard). Are you talking about stdout there? Or the serial port? Or what?

    If it's stdout, then what the printf function does is to take the binary data that you feed it (in the form of variable names) and it formats that into a character string that it then outputs to stdout. stdin inputs character strings (so to get a binary value, you must convert the string into the value that it represents -- "123" is not the same as 123) and stdout accepts character strings.

    If you're wanting to display the hex representation of your data on the screen, then use the %X format flag in printf (%x if you want lower-case).

    There's an important issue that you seem to be neglecting. If your serial port stream contains multi-byte integer values, then you need to pay attention to byte order. Most serial protocols send the most significant byte first, but Intel stores data least significan byte first. So if you just sent or read an int blindly, it would be in reverse order.
    Last edited by dwise1_aol; April 1st, 2008 at 11:15 AM.
  4. #3
  5. Contributing User

    Join Date
    Aug 2003
    Rep Power
    In integer is an integer, hex, decimal, octal or any other representation are merely that - representations, they have no meaning other than for human readability (and sometimes ascii encoding of binary data or similar).

    In C/C++ hexadecimal literal constants are prefixed 0x, so for example:

    char txByte = 0xff ;  // same as 255
    char hello[] = { 0x48,0x65, 0x6C, 0x6C, 0x6F, 0x00 } ; // note the nul terminator, same as "Hello"
    Note that in the above examples if does not matter wheter you use 0xff or 255 or "Hello" or the bracketed initialiser, the variables hold exactly the same value.

    Integer values can be output or input in hexadecimal representation using the %x or %X format spacifiers in the stdio formatted I/O functions. e.g.:

    printf( "txByte = %2.2X", txByte ) ;
    unsigned char inByte ;
    int input ;
    int check ;
    printf( "Enter hex byte: " ) ;
    check = scanf( "%x", &input) ;
    inByte = (char)input ;
    if( check != 0 )
        printf("inbyte = %d = %2.2Xh", inByte, inByte ) ;
    Note that if you use %i for input formatting, the integer input will be accepted in decimal, octal (prefixed with zero), or hex (prefixed with 0x).

    If in your application you actually want to output a stream of ASCII/Hex representations of the binary data, then you can use fprintf() or sprintf() for example:

    char hexchar_string[3] ;
    char binary_value = 0xff ;
    sprintf( hexchar_string, "%X", binary_value ) ;
    printf( "Hex string = %s\n", hexchar_string ) ;
    Now whatever you are trying to do, that should get you going.


IMN logo majestic logo threadwatch logo seochat tools logo